[Java]GUI编程

时间:2020-05-28
本文章向大家介绍[Java]GUI编程,主要包括[Java]GUI编程使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

[Java]GUI编程

一、什么是GUI编程?

  • 图形用户界面(Graphical User Interface,简称 GUI,又称图形用户接口)是指采用图形方式显示的计算机操作用户界面。
  • Java的GUI编程主要使用AWTSwing组件,Swing是对AWT的扩充。
  • AWT与Swing的区别:
    • AWT是在JDK 1.0版本时提出的;Swing是在AWT之后提出的(JAVA 2)。
    • AWT是重量级组件,因为用了大量的Windows函数, Swing是轻量级组件,压根没用Windows函数。
    • AWT只能在Windows平台下执行,Swing可以在任意平台执行。
    • AWT是基于本地方法的C/C++程序,其运行速度比较快;而swing是基于awt的Java程序,其运行速度比较慢。

二、AWT

1、Frame窗口

Frame frame = new Frame("窗口标题");//窗口标题

frame.setVisible(true);//窗口可见

frame.setSize(400,400);//窗口大小

frame.setBackground(new Color(0,0,0));//窗口背景颜色

frame.setLocation(200,200);//窗口的初始位置

frame.setResizable(false);//窗口为大小固定状态

frame.setBounds(200,200,200,200);//窗口的初始位置和窗口大小

2、Panel面板

Frame frame = new Frame("窗口标题");
Panel panel = new Panel();//创建一个面板

frame.setLayout(null);//设置布局

panel.setBounds(300,,300,500,500);
panel.setBackground(new Color(50,50,50));

frame.add(panel);//添加面板到窗口
frame.setVisible(true);

//窗口监听,关闭程序
frame.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
        System.exit(0);
    }
});

3、布局管理器

  • 流式布局

    Frame frame = new Frame("窗口标题");
    
    Buttion button1 = new Button("按钮1");
    Buttion button2 = new Button("按钮2");
    Buttion button3 = new Button("按钮3");
    
    frame.setLayout(new FlowLayout(Flowlayout.LEFT));//靠左流式布局
    frame.setSize(200,200);
    
    frame.add(button1);
    frame.add(button2);
    frame.add(button3);
    
    frame.setVisible(true);
    
  • 东西南北中

    Frame frame = new Frame("窗口标题");
    
    Buttion east = new Button("east");
    Buttion west = new Button("west");
    Buttion south = new Button("south");
    Buttion north = new Button("north");
    Buttion center = new Button("center");
    
    frame.add(east,BorderLayout.EAST);
    frame.add(west,BorderLayout.WEST));
    frame.add(east,BorderLayout.EAST));
    frame.add(north,BorderLayout.NORTH));
    frame.add(center,BorderLayout.CENTER));
    
    frame.setSize(200,200);
    frame.setVisible(true);
    
  • 表格布局

    Frame frame = new Frame("窗口标题");
    
    Buttion btn1 = new Button("btn1");
    Buttion btn2 = new Button("btn2");
    Buttion btn3 = new Button("btn3");
    Buttion btn4 = new Button("btn4");
    Buttion btn5 = new Button("btn5");
    Buttion btn6 = new Button("btn6");
    
    frame.setLayout(new GridLayout(3,2));//3行2列的表格
    
    frame.add(btn1);
    frame.add(btn2);
    frame.add(btn3);
    frame.add(btn4);
    frame.add(btn5);
    frame.add(btn6);
    
    frame.setSize(200,200);
    frame.pack();//使用此函数,就不用设置窗口大小
    frame.setVisible(true);
    

4、监听按钮

public class ActionEvent{
 public static void main(String[] args){
     Frame frame = new Frame();
     Button button = new Button();
     
     MyActionListener myActionListener = new MyActionListener();
     button.addActionListener(myActionListener);
     
     frame.add(button,BorderLayout.CENTER);
     frame.pack();
     
     frame.setVisable(true);
 }
}


//监听器
class MyActionListener implements ActionListener{
 public void actionPerformed(ActionEvent e){
     System.out.println("aaa");
 }
}

5、监听文本框

public class ActionEvent{
    public static void main(String[] args){
        Frame frame = new Frame();
        //按下回车就会触发输入框的时间
        TextFiled textField = new TExtField();
        frame.add(textFiled);
        
        MyActionListener myActionListener = new MyActionListener();
        textField.addActionListener(myActionListener);
        
        textField.setEchoChar('*');//设置替换编码
        
        frame.pack();
        
        frame.setVisable(true);
    }
}


//监听器
class MyActionListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
       TextField field = (TextField)e.getSource();
        filed.getText();//获得输入框的文本
        filed.setText("");//清空输入框
    }
}

6、简易计算机

class Calculator extends Frame{
    TextField num1, num2, num3;
    
    public void loadFrame(){
        num1 = new TextField(10);//字符数
        num2 = new TextField(10);
        num3 = new TextField(20);
        
        Button button = new Button("=");
        Label label = new Label("+");
        
        button.addActionListener(new MyCalculatorListener());
        
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        
        pack();
        setVisible(true);
        
        
        //内部类(这是对代码更好的封装)
        private class MyCalculatorListener implements ActionListener{
            
            public void actionPerformed(ActionEvent e){
                int n1 = Integer.parseInt(num1.getText());
            	int n2 = Integer.parseInt(num2.getText());
            	num3.setText(""+(n1+n2));
            	num1.setText("");
            	num2.setText("");
            }
        }
        
    }
}

7、画笔

class MyPaint extends Frame{
    
    public void laodFrame(){
        setBounds(200,200,600,500);
        setVisible(true);
    }
    
    //画笔
    public void paint(Graphics g){
        //super.paint(g);
        
        g.setColor(Color.red);//画笔颜色
        g.fillOval(100,100,100,100);//画个圆
    }
}

8、鼠标监听

class MyFrame extends Frame {

    ArrayList points;//存储鼠标的位置

    public MyFrame(String title){
        super(title);
        setBounds(200,200,400,300);
        points = new ArrayList<>();
        this.addMouseListener(new MyMouseListener());

        setVisible(true);
    }

    public void paint(Graphics g){
        Iterator iterator = points.iterator();
        while(iterator.hasNext()){
            Point point = (Point)iterator.next();
            g.setColor(Color.BLUE);
            g.fillOval(point.x,point.y,10,10);

        }
    }

//    public void addPoint(Point point){
//        points.add(point);
//    }


    private class MyMouseListener extends MouseAdapter {

        public void mousePressed(MouseEvent e){
            MyFrame frame = (MyFrame)e.getSource();

            points.add(new Point(e.getX(),e.getY()));

            frame.repaint();//重画
        }
    }
}

9、窗口监听

class WindowFrame extends Frame{
    public WindowFrame(){
        
        setBackground(Color.blue);
        setBounds(100,100,200,200);
        setVisible(true);
        
        this.addWindowListener(new MyWindowListener());//也可以用匿名内部类
    }
    
    class MyWindowListener extends WindowAdapter{
        
        public void windowClosing(WindowEvent e){
            super.windowClosing(e);
            setVisible(false);//隐藏窗口
            System.exit(0);
        }
        
        //还有一个激活窗口windowActivated
        
    }
}

10、键盘监听

class KeyFrame extends Frame{
    public KeyFrame(){
        
        setBackground(Color.blue);
        setBounds(100,100,200,200);
        setVisible(true);
        
        this.addKeyListener(new MyKeyListener());//也可以用匿名内部类
    }
    
    class MyKeyListener extends KeyAdapter{
        
        public void KeyPressed(KeyEvent e){
            e.getKeyCode();//获取当前键盘的键,可以据此判断键盘输入了哪一个键
            //keyEvent.VK_UP:上键
        }
        
    }
}

三、Swing

  • 窗口JFrame类。

  • 面板JPanel类,滚动条面板用JScrollPane类。

  • 标签框JLabel类,居中用label.setHorizontalAlignment(SwingConstants.CENTER);

  • 按钮JButton类。

  • 弹窗JDialog类。

  • 关闭窗口事件用setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)函数。

  • JFrame是顶级窗口,其他的组件放到容器里Container container= this.getContentPane();

  • 图标实现Icon接口中的方法即可。

  • Jlabel label = new JLabel("ImageIcon");
    URL url = ImageIconDemo.class.getResource("hh.jpg");//通过类的反射获取图片
    
    ImageIcon imageIcon = new ImageIcon(url);
    label.setIcon(imageIcon);
    label.setHorizontalAlignment(SwingConstants.CENTER);
    
  • 单选框用JRadioButton类,由于单选框只能选择一个,所以要使用ButtonGroup类将单选框分组;复选框用JCheckBox类,不用进行分组。

  • 列表框使用JList类;下拉列表用JComboBox类。

  • 文本框用JTextFiled类;文本域用JTextArea类;密码框用JPasswordField类。

原文地址:https://www.cnblogs.com/baihan/p/12984305.html