欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

GUI编程

程序员文章站 2022-03-01 13:05:32
...

GUI编程

  • GUI核心技术: Swing AWT

    • 界面不美观
    • 需要jre环境
  • AWT

    • 包含很多类和接口,GUI

    • 元素:窗口,按钮,文本框

    • java.awt

    • 组件和容器

    • [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WIDdI1Tq-1644918270526)(C:\Users\86177\AppData\Roaming\Typora\typora-user-images\1644888269680.png)]

    • 第一个Frame程序

      package base;
      
      import java.awt.*;
      
      public class MyFrame {
          public static void main(String[] args) {
              Frame frame = new Frame("窗口程序");
              //设置窗口可见性
              frame.setVisible(true);
              //设置窗口大小
              frame.setSize(400,400);
              //设置背景颜色
              frame.setBackground(new Color(110, 142, 246));
              //设置弹出位置
              frame.setLocation(200,200);
              //设置大小固定
              frame.setResizable(false);
          }
      }
      

      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-F8ZnKiou-1644918270528)(C:\Users\86177\AppData\Roaming\Typora\typora-user-images\1644891209041.png)]

      问题:窗口无法正常关闭,需要停止java程序

      尝试回顾封装:

      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rLQgLG6i-1644918270529)(C:\Users\86177\AppData\Roaming\Typora\typora-user-images\1644891437047.png)]

      package base;
      import java.awt.*;
      
      public class MyFrame2 {
          public static void main(String[] args) {
              MyFrame01 myFrame01 = new MyFrame01(100, 100, 200, 200, Color.BLACK);
              MyFrame01 myFrame02 = new MyFrame01(300, 100, 200, 200, Color.yellow);
              MyFrame01 myFrame03 = new MyFrame01(100, 300, 200, 200, Color.red);
              MyFrame01 myFrame04 = new MyFrame01(300, 300, 200, 200, Color.green);
      
          }
      }
      
      class MyFrame01 extends Frame{
          static int id = 0; //可能存在多个窗口,需要计数器
          public MyFrame01(int x,int y ,int w,int h,Color color){
              super("Myframe+"+ (++id));
              setBounds(x,y,w,h);
              setVisible(true);
              setBackground(color);
          }
      
      }
      
    • Panel面板

      • 添加frame窗口关闭监听事件,解决无法关闭窗口问题
      package base;
      
      import java.awt.*;
      import java.awt.event.WindowAdapter;
      import java.awt.event.WindowEvent;
      
      //panel 可以看成一个空间,但是不能单独存在
      public class MyPanel {
          public static void main(String[] args) {
              Frame frame = new Frame();
              //布局的概念
              Panel panel = new Panel();
              //设置布局
              //panel.setLayout(null);
              //坐标
              frame.setBounds(300,300,500,500);
              frame.setBackground(new Color(110, 142, 246));
              //panel 设置坐标,相对于frame
              panel.setBounds(50,50,400,400);
              panel.setBackground(new Color(69, 117, 128));
      
              frame.add(panel);
              frame.setVisible(true);
      
              //监听事件,监听窗口关闭事件 system.exit(0)
              frame.addWindowListener(new WindowAdapter() {
                  @Override
                  public void windowClosing(WindowEvent e) {
                      //结束程序
                      System.exit(0);
                  }
              });
      
      
          }
      }
      
    • 布局管理器

      • 流式布局

        package base;
        
        import java.awt.*;
        
        public class FlowLayout {
            public static void main(String[] args) {
                Frame frame = new Frame();
        
                //组件-按钮
                Button but1 = new Button("button");
                Button but2 = new Button("button");
                Button but3 = new Button("button");
        
                frame.setLayout(new java.awt.FlowLayout());
                frame.setSize(200,200);
                frame.setVisible(true);
                frame.add(but1);
                frame.add(but2);
                frame.add(but3);
            }
        }
        
      • 东西南北中

        [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ugup4I5s-1644918270529)(C:\Users\86177\AppData\Roaming\Typora\typora-user-images\1644895159860.png)]

        package base;
        
        import java.awt.*;
        
        public class Border {
            public static void main(String[] args) {
                Frame frame = new Frame();
                //组件-按钮
                Button but1 = new Button("east");
                Button but2 = new Button("west");
                Button but3 = new Button("south");
                Button but4 = new Button("north");
                Button but5 = new Button("center");
        
                frame.add(but1,BorderLayout.EAST);
                frame.add(but2,BorderLayout.WEST);
                frame.add(but3,BorderLayout.SOUTH);
                frame.add(but4,BorderLayout.NORTH);
                frame.add(but5,BorderLayout.CENTER);
        
                frame.setSize(500,400);
                frame.setVisible(true);
        
            }
        }
        
      • 表格布局

        • [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cyvznjSs-1644918270530)(C:\Users\86177\AppData\Roaming\Typora\typora-user-images\1644895599395.png)]

          package base;
          
          import java.awt.*;
          
          public class GridLayout {
              public static void main(String[] args) {
                  Frame frame = new Frame();
                  //组件-按钮
                  Button but1 = new Button("but1");
                  Button but2 = new Button("but2");
                  Button but3 = new Button("but3");
                  Button but4 = new Button("but4");
                  Button but5 = new Button("but5");
                  Button but6 = new Button("but6");
                  frame.setLayout(new java.awt.GridLayout(3,2));
                  frame.add(but1);
                  frame.add(but2);
                  frame.add(but3);
                  frame.add(but4);
                  frame.add(but5);
                  frame.add(but6);
                  frame.pack();
                  frame.setSize(400,500);
                  frame.setVisible(true);
              }
          
          }
          
    • 测试:实现如下布局

      • [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xVSDS7gB-1644918270530)(C:\Users\86177\AppData\Roaming\Typora\typora-user-images\1644897303899.png)]

        package base;
        
        import javafx.scene.layout.Pane;
        
        import java.awt.*;
        import java.awt.GridLayout;
        
        public class Border {
            public static void main(String[] args) {
                Frame frame = new Frame();
                /*
                1. panel panel2 分别将整体分割为上下两部分
                2. 上半部分可以看做左右按钮加中间一个panel存放上下两个按钮
                3. 下半部分可以看为左右按钮中加中间一个panel,panel存放四个按钮
                */
                
                Panel panel = new Panel();
                Panel panel2 = new Panel();
                panel.setBackground(new Color(1,1,1));
                panel2.setBackground(new Color(255,255,25));
                frame.setLayout(new java.awt.GridLayout(2,1));
                frame.add(panel);
                frame.add(panel2);
                Button but1 = new Button("east");
                Button but2 = new Button("west");
                Button but3 = new Button("south");
                Button but4 = new Button("north");
                Panel panel3 = new Panel();
                panel3.setLayout(new java.awt.GridLayout(2,1));
                panel3.add(but3);
                panel3.add(but4);
                panel.setLayout(new BorderLayout());
                panel.add(but1,BorderLayout.EAST);
                panel.add(but2,BorderLayout.WEST);
                panel.add(panel3,BorderLayout.CENTER);
        
        
        
                Button but5 = new Button("east");
                Button but6 = new Button("west");
                Button but7 = new Button("center");
                Button but8 = new Button("center");
                Button but9 = new Button("center");
                Button but10 = new Button("center");
        
                Panel panel4 = new Panel();
                panel4.setLayout(new GridLayout(2,2));
                panel4.add(but7);
                panel4.add(but8);
                panel4.add(but9);
                panel4.add(but10);
        
                panel2.setLayout(new BorderLayout());
                panel2.add(but5,BorderLayout.EAST);
                panel2.add(but6,BorderLayout.WEST);
                panel2.add(panel4,BorderLayout.CENTER);
        
        
                frame.setSize(500,400);
                frame.setVisible(true); //设置可见
                //添加窗口监听
                frame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }
                });
        
            }
        }
        
        

        总结:

        1. Frame是一个*窗口
        2. Panel无法单独显示,必须添加到某个容器中。
        3. 布局管理
          1. 流式
          2. 东西南北中
          3. 表格
        4. 大小,定位,背景颜色,可见性,监听

    事件监听

    package base;
    
    
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    public class ActionEvent {
        public static void main(String[] args) {
            //按下按钮触发事件
            Frame frame = new Frame();
            Button button = new Button("button");
            //因为addActionListener需要一个ActionListener,所有需要构造一个ActionListener
            MyActionListener myActionListener = new MyActionListener();
            button.addActionListener(myActionListener);
    
            frame.add(button,BorderLayout.CENTER);
            frame.setVisible(true);
            frame.pack();
            frame.setSize(400,400);
            windowClose(frame);
        }
    
        //关闭窗体的事件
        private static void windowClose(Frame frame){
            frame.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
    
        }
    }
    
    //按钮点击事件
    class MyActionListener implements ActionListener{
    
        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            System.out.println("aaa");
        }
    }
    
    • 多个按钮共享一个事件

      package base;
      
      import java.awt.*;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.event.WindowAdapter;
      import java.awt.event.WindowEvent;
      
      public class ActionEvent02 {
          public static void main(String[] args) {
              //两个按钮实现同一个监听
              //开始 停止
              Frame frame = new Frame("开始-停止");
              Button button = new Button("start");
              Button button2 = new Button("stop");
              MyMonitor myMonitor = new MyMonitor();
              button.addActionListener(myMonitor);
              button2.addActionListener(myMonitor);
              frame.add(button,BorderLayout.EAST);
              frame.add(button2,BorderLayout.WEST);
              frame.setSize(400,400);
              frame.setVisible(true);
              windowClose(frame);
      
          }
          //关闭窗体的事件
          private static void windowClose(Frame frame){
              frame.addWindowListener(new WindowAdapter() {
                  @Override
                  public void windowClosing(WindowEvent e) {
                      System.exit(0);
                  }
              });
      
          }
      
      }
      
      class MyMonitor implements ActionListener{
          @Override
          public void actionPerformed(ActionEvent e) {
              System.out.println("按钮被点击了:"+ e.getActionCommand());
          }
      }
      
  • Swing