前言
万字深入总结JAVA中的swing编程
1.简介
-
GUI的核心技术
- Swing和AWT
- 界面不美观
- 需要jre环境!
-
可以写出自己心中想要的小工具
-
工作的时候,也可能需要维护到Swing界面,概率极小!
-
了解MVC架构,了解监听!
-
组件:
-
窗口
-
弹出
-
面板
-
文本框
-
列表框
-
按钮
-
图片
-
监听事件
-
鼠标事件
-
键盘事件
-
2、AWT
2.1、AWT介绍
- new 类!
- 包含了很多类和接口!
- 用于GUI编程图像用户界面
- 元素:窗口、按钮、文本框
- java.awt包
2.2 、组件和容器
Frame
- 单窗口示例
public static void main(String[] args) {
Frame frame = new Frame("我的第一个Java图形界面窗口");
//设置窗口可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置窗口颜色
frame.setBackground(Color.CYAN);
//设置窗口的出现位置
frame.setLocation(300,300);
//是否可调整大小
frame.setResizable(false);
}
- 多窗口示例
public class OneFrame {
public static void main(String[] args) {
MyFrame l1 = new MyFrame(100, 100, 200, 200, Color.yellow);
MyFrame l2 = new MyFrame(300, 100, 200, 200, Color.CYAN);
MyFrame l3 = new MyFrame(100, 300, 200, 200, Color.black);
MyFrame l4 = new MyFrame(300, 300, 200, 200, Color.blue);
}
}
class MyFrame extends Frame {
static int n = 0;
public MyFrame(int x, int y, int w, int h, Color color) {
super("l" + (n++));
setVisible(true);
setBounds(x, y, w, h);
setBackground(color);
setResizable(false);
}
}
面板Panel(解决关闭问题)
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//坐标
frame.setBounds(100,100,100,100);
frame.setBackground(new Color(40,40,40));
//panel设置坐标,相对于frame
panel.setBounds(50,50,50,50);
panel.setBackground(new Color(199,199,199));
frame.add(panel);
frame.setVisible(true);
//监听事件,监听创口关闭事件,System.exit(0);适配器模式
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭的时候需要做的事情
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
布局管理器
- 流式布局
public static void main(String[] args) {
Frame frame = new Frame("FlowLayout");
//组件——>按钮
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.setLayout(new FlowLayout(FlowLayout.RIGHT));//右
frame.setSize(400,400);
//把按钮添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
- 东西南北中布局
public static void main(String[] args) {
Frame frame = new Frame("BorderLayout");
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.pack();
frame.setVisible(true);
}
- 表格布局
public static void main(String[] args) {
Frame frame = new Frame("GridLayout");
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 static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
frame.setBackground(Color.WHITE);
frame.setBounds(600,600,200,200);
frame.setLayout(new GridLayout(2,1));
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"),BorderLayout.EAST);
panel1.add(new Button("West"),BorderLayout.WEST);
panel2.add(new Button("panel2"));
panel2.add(new Button("panel2"));
panel1.add(panel2,BorderLayout.CENTER);
panel3.add(new Button("East"),BorderLayout.EAST);
panel3.add(new