GUI编程

GUI:图形用户编程

Awt
  • 介绍

    • 包含很多类和接口
    • 元素:窗口,按钮,文本框
    • java.awt
  • 窗口

    • public static void main(String[] args) {
          //Frame JDk  看源码
          Frame frame = new Frame("小胡的第一个图像界面窗口!");
      
          //需要设置可见性
          frame.setVisible(true);
      
          //设置窗口大小
          frame.setSize(400,400);
      
          //设置背景颜色
          frame.setBackground(Color.green);
      
          //弹出的初始位置
          frame.setLocation(150,150);
      
          //设置大小固定
          frame.setResizable(false);
      
      }
      
  • 回顾封装

    public static void main(String[] args) {
            MyFrame myFrame1=new MyFrame(100,100,400,400,Color.red);
            MyFrame myFrame2=new MyFrame(500,100,400,400,Color.green);
            MyFrame myFrame3=new MyFrame(100,500,400,400,Color.blue);
            MyFrame myFrame4=new MyFrame(500,500,400,400,Color.magenta);
        }
    
    }
    class MyFrame extends Frame{
        static int id=0;
        public MyFrame(int x,int y,int w,int h,Color color){
            super("MyFrame"+(++id));
            setVisible(true);
            setBounds(x,y,w,h);
            setBackground(color);
        }
       
        
         public static void main(String[] args) {
            //Frame JDk  看源码
            Frame frame = new Frame("小胡的第一个图像界面窗口!");
    
            //需要设置可见性
            frame.setVisible(true);
    
            //设置窗口大小
            frame.setSize(400,400);
    
            //设置背景颜色
            frame.setBackground(Color.green);
    
            //弹出的初始位置
            frame.setLocation(150,150);
    
            //设置大小固定
            frame.setResizable(false);
    
        }
    
  • 面板

    public class panel {
        public static void main(String[] args) {
    
            Frame frame = new Frame();
            Panel panel = new Panel();
            //设置布局
            frame.setLayout(null);
            //坐标
            frame.setBounds(200,200,300,300);
            frame.setBackground(Color.orange);
            //panel坐标 相对于frame
            panel.setBounds(50,50,200,200);
            panel.setBackground(Color.magenta);
            //添加panel
            frame.add(panel);
            //显示
            frame.setVisible(true);
    
            //监听事件 显示窗口关闭事件 System.exit(0)
            frame.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        }
    }
    
  • 布局

    • 流式布局

      public class button {
          public static void main(String[] args) {
              Frame frame = new Frame();
              Button button1 = new Button("button1");
              Button button2 = new Button("button2");
              Button button3 = new Button("button3");
      
              //设置流式布局
              //frame.setLayout(new FlowLayout());
              frame.setLayout(new FlowLayout(FlowLayout.LEFT)); //左对齐
      
              frame.setSize(200,200);
      
              //窗口添加按钮
              frame.add(button1);
              frame.add(button2);
              frame.add(button3);
      
              frame.setVisible(true);
          }
      }
      
      
    • 东南西北中

      public class Layout {
          public static void main(String[] args) {
              Frame frame = new Frame();
              Button btn1 = new Button("btn1");
              Button btn2 = new Button("btn2");
              Button btn3 = new Button("btn3");
              Button btn4 = new Button("btn4");
              Button btn5 = new Button("btn5");
      
              //东南西北中布局
              frame.add(btn1,BorderLayout.EAST);
              frame.add(btn2,BorderLayout.WEST);
              frame.add(btn3,BorderLayout.SOUTH);
              frame.add(btn4,BorderLayout.NORTH);
              frame.add(btn5,BorderLayout.CENTER);
      
              frame.setSize(200,200);
              frame.setVisible(true);
          }
      }
      
      
    • 表格布局

      public class Table {
          public static void main(String[] args) {
              Frame frame = new Frame();
              Button btn1 = new Button("btn1");
              Button btn2 = new Button("btn2");
              Button btn3 = new Button("btn3");
              Button btn4 = new Button("btn4");
              Button btn5 = new Button("btn5");
              Button btn6 = new Button("btn6");
      
              //表格布局
              frame.setLayout(new GridLayout(3,2));
      
              frame.add(btn1);
              frame.add(btn2);
              frame.add(btn3);
              frame.add(btn4);
              frame.add(btn5);
              frame.add(btn6);
      
              frame.pack();//Java函数 自动填充
              frame.setVisible(true);
          }
      }
      
      
  • 布局嵌套

    public class TestLayout {
        public static void main(String[] args) {
            Frame frame = new Frame();
    
            frame.setSize(800,600);
            frame.setLocation(300,300);
            frame.setVisible(true);
            frame.setLayout(new GridLayout(2,1));
            //四个面板
            Panel p1 = new Panel(new BorderLayout());
            Panel p2 = new Panel(new GridLayout(2,1));
            Panel p3 = new Panel(new BorderLayout());
            Panel p4 = new Panel(new GridLayout(2,2));
    
            //上面
            p1.add(new Button("esat1"),BorderLayout.EAST);
            p1.add(new Button("west1"),BorderLayout.WEST);
            p2.add(new Button("btn1"));
            p2.add(new Button("btn2"));
            p1.add(p2,BorderLayout.CENTER);
    
            //下面
            p3.add(new Button("esat2"),BorderLayout.EAST);
            p3.add(new Button("west2"),BorderLayout.WEST);
            p4.add(new Button("btn3"));
            p4.add(new Button("btn4"));
            p4.add(new Button("btn5"));
            p4.add(new Button("btn6"));
            p3.add(p4,BorderLayout.CENTER);
    
            frame.add(p1);
            frame.add(p3);
    
        }
    }
    
  • 总结

    • Frame是一个顶级窗口
    • Panel无法单独显示,必须添加到某个容器中
    • 布局
      • 流式
      • 东南西北
      • 表格
    • 大小、定位、背景、可见性

    关闭窗口的事件

    private static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter(){
            private void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
    }
    

    关闭窗口的事件

    private static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter(){
            private void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值