GUI编程
Graphical User Interface 图形用户界面
组件:
- 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听事件
- 鼠标
- 键盘事件
- 破解工具
- …
1、简介
Gui的核心技术:Swing AWT
why不常用了?
- 界面不美观
- 需要jre环境 大
why学?
- 写小工具
- 工作可能需要维护到swing界面 但概率极小
- 了解MVC架构 了解监听
2、AWT
2.1、AWT介绍
- 包含了很多类和接口
- 元素:窗口 按钮 文本框 …
- java.awt包
2.2、组件和容器
1. Frame
public class TestFrame {
public static void main(String[] args) {
Frame frame = new Frame("我的第一个Java图形界面窗口");
//设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色
frame.setBackground(new Color(85,150,68));
//弹出初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
}
问题:窗口关不掉。
解决:停止java运行
回顾封装 展示多个窗口:
public class TestFrame02 {
public static void main(String[] args) {
//展示多个窗口
Frame frame1 = new MyFrame(100,100,200,200,Color.blue);
Frame frame2 = new MyFrame(300,100,200,200,Color.yellow);
Frame frame3 = new MyFrame(100,300,200,200,Color.red);
Frame frame4 = new MyFrame(300,300,200,200,Color.green);
}
}
class MyFrame extends Frame{
static int id=0;//计数器
public MyFrame(int x,int y,int w,int h,Color color){
super("MyFrame+"+(++id));
setBounds(x,y,w,h);
setBackground(color);
setVisible(true);
}
}
2. Panel
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame();
//布局的概念
Panel panel = new Panel();
frame.setLayout(null);
frame.setBackground(Color.green);
frame.setBounds(300,30,500,500);
//panel设置坐标
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(106, 18, 36, 12));
frame.add(panel);
frame.setVisible(true);
//监听事件,监听窗口关闭事件
//适配器模式:23种
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭的时候需要做的事情
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
解决了窗口关闭问题。
2.3、 布局管理器
-
流式布局
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(FlowLayout.LEFT));//默认 center frame.setSize(200,200); frame.add(button1); frame.add(button2); frame.add(button3); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { //窗口点击关闭的时候需要做的事情 @Override public void windowClosing(WindowEvent e) { //结束程序 System.exit(0); } }); } }
-
东西南北中
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); } }
-
表格布局
public class TestGridLayout { 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"); Button button4 = new Button("button4"); Button button5 = new Button("button5"); Button button6 = new Button("button6"); frame.setLayout(new GridLayout(3,2)); frame.add(button1); frame.add(button2); frame.add(button3); frame.add(button4); frame.add(button5); frame.add(button6); frame.pack();//自动选择合适的位置 frame.setVisible(true); } }
布局是可以嵌套的
练习:
public class TestLayout {
public static void main(String[] args) {
Frame frame = new Frame("练习");
//四个面板
Panel panelUp = new Panel(new BorderLayout());
Panel panelUpCenter = new Panel(new GridLayout(2,1));
Panel panelDown = new Panel(new BorderLayout());
Panel panelDownCenter = new Panel(new GridLayout(2,2));
frame.setSize(400,400);
frame.setLocation(300,300);
frame.setVisible(true);
//先分两行一列的表格 上下
frame.setLayout(new GridLayout(2,1));
//上面放一个面板分east center west
panelUp.add(new Button("UpEast"),BorderLayout.EAST);
panelUp.add(new Button("UpWest"),BorderLayout.WEST);
// center里放面板分两行一列的表格 放button
panelUpCenter.add(new Button("button1"));
panelUpCenter.add(new Button("button2"));
panelUp.add(panelUpCenter,BorderLayout.CENTER);
//下面放一个面板分east center west
panelDown.add(new Button("DownEest"),BorderLayout.EAST);
panelDown.add(new Button("DownWest"),BorderLayout.WEST);
// center里放面板分两行两列的表格 放button
for (int i = 0; i < 4; i++) {
panelDownCenter.add(new Button("button"+i));
}
panelDown.add(panelDownCenter,BorderLayout.CENTER);
frame.add(panelUp);
frame.add(panelDown);
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭的时候需要做的事情
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
总结
- Frame是一个顶级窗口
- Panel无法单独显示,必须添加到某个容器中
- 布局管理器
- 流式
- 东西南北中
- 表格
- 大小、位置、背景颜色、可见性、监听
2.4、事件监听
当某个事情发生时,该做什么?
public class TestActionEvent {
public static void main(String[] args) {
Frame frame = new Frame();
Button button = new Button();
//按下按钮 触发一些事件
button.addActionListener(new MyActionListener());
frame.add(button);
frame.pack();
frame.setVisible(true);
windowClose(frame);
}
//关闭窗口
public static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//事件监听
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaaa");
}
}
多个按钮共享一个事件
public class TestActionEventTwo {
public static void main(String[] args) {
Frame frame = new Frame();
Button bStart = new Button("start");
Button bStop = new Button("stop");
MyListener myListener = new MyListener();
bStart.addActionListener(myListener);
bStop.addActionListener(myListener);
//显式定义触发按钮后的行为
bStart.setActionCommand("start button");
bStop.setActionCommand("stop button");
frame