GUI编程
GUI
-
窗口
-
弹窗
-
面板
-
文本框
-
列表框
-
按钮
-
图片
-
监听事件
-
鼠标
-
键盘事件
-
破解工具
一 简介
- GUI核心技术:Swing AWT
界面不美观
需要jre环境 - 为什么要学习?
可以写一些小工具 - 工作中,可能会维护到swing界面,概率不大!
了解MVC架构,了解监听!
二 AWT
AWT介绍
- 包含了很多类和接口
- 元素:窗口/按钮/文本框
- java.awt 包
![]()
组件和容器
1、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(new Color(85,150,68));
//弹出初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
}

- Frame 封装
public class TestFrame2 {
public static void main(String[] args) {
MyFrame myFrame = new MyFrame(200, 200, 200, 200, Color.red);
MyFrame myFrame2 = new MyFrame(200, 300, 200, 200, Color.black);
MyFrame myFrame3 = new MyFrame(300, 300, 200, 200, Color.yellow);
}
}
class MyFrame extends Frame{
//可能存在多个窗口,需要一个计数器
static int id = 0;
public MyFrame(int x,int y,int w,int h,Color color){
super("Myframe+"+(++id));
setBackground(color);
setBounds(x,y,w,h);
setVisible(true);
}
}

2、panel(面板)
package com.kuang.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;
public class TestPanel {
//panel可以看做是窗口中的一个空间,不能单独存在
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(19, 94, 42));
//panel坐标,相对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(161, 31, 36));
//frame.add(panel)
frame.add(panel);
frame.setVisible(true);
//监听事件,监听窗口关闭事件
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭时执行的动作
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}

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());
//布局 左
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setSize(200,200);
//把按钮添加到窗口
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}

- 东西南北中
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(300,300);
frame.setVisible(true);
}
}

- 表格布局
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame();
Button bt1 = new Button("bt1");
Button bt2 = new Button("bt2");
Button bt3 = new Button("bt3");
Button bt4 = new Button("bt4");
Button bt5 = new Button("bt5");
frame.setLayout(new GridLayout(3,2));
frame.add(bt1);
frame.add(bt2);
frame.add(bt3);
frame.add(bt4);
frame.add(bt5);
// frame.setSize(400,400);
frame.pack();//自动布局
frame.setVisible(true);
}
}

- 布局练习:

public static void main(String[] args) {
Frame frame = new Frame();
//设置表格布局为两行一列
frame.setLayout(new GridLayout(2,1));
frame.setVisible(true); //设置窗口可见
//4个面板
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-bt-1"));
p2.add(new Button("p2-bt-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("for-"+i));
}
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
}
总结:
- Frame是一个顶级窗口
- Panel无法单独显示,必须添加到容器中
- 布局管理器
- 流式
- 东西南北中
- 表格
- 大小,定位,背景颜色,可见性,监听。
4、事件监听
事件监听:当某个事情发生的时候,干什么?
public class TestAction {
public static void main(String[] args) {
//按下按钮时触发事件
Frame frame = new Frame();
Button button = new Button();
myActionListener myActionListener = new myActionListener();
button.addActionListener(myActionListener);
frame.add(button,BorderLayout.CENTER);
frame.pack();
windowClose(frame);
frame.setVisible(true);
}
private 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("点击监听测试");
}
}
监听文本框内容,获取文本框输入内容,回车后,替换为 * 并打印到控制台
public class TestText01 {
public static void main(String[] args) {
//启动!
MyFrame myFrame = new MyFrame();
TestAction testAction = new TestAction();
testAction.windowClose(myFrame);
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
add(textField);
//监听文本框输入的文字
MyActionListener2 myActionListener2 = new MyActionListener2();
textField.addActionListener(myActionListener2);
//设置替换编码
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionListener2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField) e.getSource();
System.out.println(field.getText());
field.setText("");
}
}


浙公网安备 33010602011771号