本节内容视频链接:6、课堂练习讲解及总结_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1DJ411B75F?p=6&vd_source=b5775c3a4ea16a5306db9c7c1c1486b5
根据前三节学习到的Frame、Panel、Button知识,画出一下窗口界面:
实现代码如下:
public class ExDemo01 {public static void main(String[] args) {Frame frame = new Frame("窗口1");frame.setLayout(new GridLayout(2,1));frame.setVisible(true);frame.setBounds(200,200,400,300);frame.setBackground(Color.blue);Panel panel1 = new Panel(new BorderLayout());Panel panel2 = new Panel(new GridLayout(2,1));Panel panel3 = new Panel(new BorderLayout());Panel panel4 = new Panel(new GridLayout(2,2));panel1.add(new Button("East-1"), BorderLayout.EAST);panel1.add(new Button("West-1"), BorderLayout.WEST);panel2.add(new Button("P2-btn-1"));panel2.add(new Button("P2-btn-2"));panel1.add(panel2, BorderLayout.CENTER);panel3.add(new Button("East-2"), BorderLayout.EAST);panel3.add(new Button("West-2"), BorderLayout.WEST);for (int i = 0; i < 4; i++) {panel4.add(new Button("for-" + i));}panel3.add(panel4, BorderLayout.CENTER);frame.add(panel1);frame.add(panel3);frame.addWindowListener(new WindowAdapter() {//窗口点击关闭的时候需要做的事情@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);//结束程序}});}
}
GUI编程前三节内容总结:
1、Frame是一个顶级的窗口;
2、Panel(面板)无法单独显示,必须添加到某个容器中;
3、布局管理器
- 流式布局 FlowLayout
- 东西南北中布局 BorderLayout
- 网格布局 GridLayout
4、窗口大小,初始化位置、背景颜色、设置可见性、监听鼠标动作。