Java之GUI编程基础

参考狂神b站视频,希望大家多多关注狂神呀

Frame

public class TestFrame {
    public static void main(String[] args) {

        //Frame

        //设置弹窗标题
        Frame frame = new Frame("我的第一个Java图形界面窗口");

        //设置可见性
        frame.setVisible(true);

        //设置弹窗大小
        frame.setSize(400,400);

        //设置背景颜色
        frame.setBackground(Color.WHITE);

        //弹出初始位置
        frame.setLocation(200,200);

        //设置大小固定
        frame.setResizable(false);
    }
}
public class TestFrame02 {
    public static void main(String[] args) {
        //展示多个窗口
        new MyFrame(100,100,100,100,Color.blue);
        new MyFrame(200,100,100,100,Color.yellow);
        new MyFrame(100,200,100,100,Color.red);
        new MyFrame(200,200,100,100,Color.white);
    }
}

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);
    }

}

TestFrame02弹出效果

面板Panel

/**
 * @Description: panel可以看成是一个空间 但不能单独存在 解决了关闭事件
 * @Date: 2020-05-17 11:37
 **/
public class TestPanel {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Panel panel = new Panel();


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

        frame.setBounds(200, 200, 200, 200);
        frame.setBackground(new Color(40, 161, 35));

        panel.setBounds(50, 50, 100, 100);
        panel.setBackground(new Color(248, 0, 0));

        frame.add(panel);

        frame.setVisible(true);
		        //监听事件 监听窗口关闭事件 System.exit(0)
        //适配器模式
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

Panel关闭

布局管理器

  • 流式布局
  • 东西南北中
  • 表格布局

/**
 * @Description: 流式布局
 * @Date: 2020-05-17 11:48
 **/
public class TestFlowLayout {
    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.setSize(200,200);

        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        frame.setVisible(true);
    }
}

流式布局

/**
 * @Description: 东西南北中
 * @Author: wangyinghao_sx
 * @Date: 2020-05-17 11:48
 **/
public class TestBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();

        Button east = new Button("East");
        Button west = new Button("West");
        Button south = new Button("South");
        Button north = new Button("North");
        Button center = new Button("Center");

        frame.add(east,BorderLayout.EAST);
        frame.add(west,BorderLayout.WEST);
        frame.add(south,BorderLayout.SOUTH);
        frame.add(north,BorderLayout.NORTH);
        frame.add(center,BorderLayout.CENTER);

        frame.setSize(200,200);

        frame.setVisible(true);
    }
}

东西南北中

/**
 * @Description:表格布局
 * @Date: 2020-05-17 11:48
 **/
public class TestGridLayout {
    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.setSize(200,200);

        frame.pack();

        frame.setVisible(true);
    }
}

表格布局

public class TestLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();

        frame.setBounds(100, 100, 200, 200);
        frame.setBackground(Color.WHITE);
        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("East-1"), BorderLayout.EAST);
        p1.add(new Button("West-1"), BorderLayout.WEST);

        p2.add(new Button("p2-1"));
        p2.add(new Button("p2-2"));

        p1.add(p2, BorderLayout.CENTER);

        p3.add(new Button("East-2"), BorderLayout.EAST);
        p3.add(new Button("West-2"), BorderLayout.WEST);

        for (int i = 0; i < 4; i++) {
            p4.add(new Button("p4-" + i + 1));
        }
        p3.add(p4, BorderLayout.CENTER);

        frame.add(p1);
        frame.add(p3);

        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. 大小 定位 背景颜色 可见性 监听
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章