如何学习?
GUI核心技术:Swing 丶 AWT
一丶因为界面不美观;
二丶需要jre环境
1.为什么我们要学习?
关闭事件的解决
import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; // Panel 可以看成是一个空间,但是不能单独存在 public class TestPanel { public static void main(String[] args) { Frame frame = new Frame(); // 布局的概念 Panel panel = new Panel(); //设置布局 frame.setLayout(null); // 初始坐标 frame.setBounds(300,300,500,500); frame.setBackground(new Color(40,161, 35)); // panel 设置坐标,相对于frame panel.setBounds(50,50,400,400); panel.setBackground(new Color(0xC20303)); // frame.add(panel) frame.add(panel); frame.setVisible(true); // 监听事件, 监听窗口关闭事件 // 适配器模式: 就是写其中的某一种父类方法进行重写 // 一种是WindowListener这种模式,是直接去重写所有父类的方法 frame.addWindowListener(new WindowAdapter() { // 窗口关闭所需要做的事情 @Override public void windowClosing(WindowEvent e) { System.exit(0); // 0是正常退出,1是有异常的退出 } }); } }
关闭图片
练习题1
package ex01; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class Testlayout { public static void main(String[] args) { Frame frame = new Frame("TestBorderLayout"); Panel panel1 = new Panel(); Panel panel2 = new Panel(); Panel panel3 = new Panel(); Panel panel4 = new Panel(); 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"); Button btn7 = new Button("btn7"); Button btn8 = new Button("btn8"); Button btn9 = new Button("btn9"); Button btn10 = new Button("btn10"); frame.setBackground(Color.black); frame.setLocation(500, 400); frame.setSize(700,500); frame.setLayout(new GridLayout(2,1)); frame.add(panel1, BorderLayout.NORTH); frame.add(panel2, BorderLayout.SOUTH); frame.setVisible(true); panel1.setLayout(new GridLayout(1,3)); panel1.add(btn1, BorderLayout.WEST); panel1.add(panel3); panel1.add(btn2, BorderLayout.EAST); panel3.setLayout(new GridLayout(2,1)); panel3.add(btn3, BorderLayout.NORTH); panel3.add(btn4, BorderLayout.SOUTH); // System.out.println("==========上半部分=========="); panel2.setLayout(new GridLayout(1,3)); panel2.add(btn5, BorderLayout.WEST); panel2.add(panel4); panel2.add(btn6, BorderLayout.EAST); panel4.setLayout(new GridLayout(2,2)); panel4.add(btn7); panel4.add(btn8); panel4.add(btn9); panel4.add(btn10); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }