GUI编程
-
组件:
-
窗口
-
弹窗
-
面板
-
文本框
-
-
按钮
-
图片
-
监听事件
-
鼠标事件、键盘事件
-
破解工具
-
1、简介
-
GUI的核心技术:Swing AWT
1.界面不美观
2.需要jre环境
-
用处:
-
可以写一些自己的小工具
-
工作时候可能需要维护到swing界面,概率极小
-
了解MVC架构,了解监听
-
2、AWT
2.1 AWT介绍
-
包含了很多类和接口:GUI
-
元素,窗口,按钮,文本框
-
java.awt
2.2组件和容器
2.3布局管理器
1.Frame
/**
* GUI的第一个界面
*/
public class TestFrame {
public static void main(String[] args) {
//Frame JDK
Frame frame = new Frame("第一个java图像界面窗口");
//需要设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色
frame.setBackground(new Color(18, 175, 238));
//弹出的初始位置
frame.setLocation(20,20);
//设置大小固定
frame.setResizable(false);
}
}
问题:弹窗关不掉;解决:直接停止运行
回顾封装
import java.awt.*;
//封装
//展示多个窗口 new
public class TestFrame2 {
public static void main(String[] args) {
//new
new MyFrame(100,100,200,200,Color.blue);
new MyFrame(100,200,200,200,Color.yellow);
new MyFrame(200,100,200,200,Color.green);
new MyFrame(200,200,200,200,Color.white);
}
}
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);//可见性
}
}
-
Panel
//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,600,800);
//背景
frame.setBackground(new Color(252, 114, 114));
//panel设置坐标,相对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(52, 170, 201, 141));
//frame.add()
frame.add(panel);
//需要设置可见性
frame.setVisible(true);
// panel.setVisible(true);
//监听事件:监听窗口关闭事件,System.exit(0)
//适配器模式:
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭的时候需要做的事
解决了关闭问题
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.RIGHT));//居右
//frame.setLayout(new FlowLayout());
frame.setSize(200,300);
//把按钮添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
//监听事件:监听窗口关闭事件,System.exit(0)
//适配器模式:
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭的时候需要做的事
-
东西南北中布局
//东西南北中布局
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()
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);
//适配器模式:
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭的时候需要做的事
-
表格布局
/**
* 表格布局
*/
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestGridLayout");
Button btn1= new Button("btn1");
Button btn2= new Button("btn2");
Button btn3= new Button("btn3");
Button btn4= new Button("btn4");
frame.setLayout(new GridLayout(2,2));
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.pack();//java函数 自适应
frame.setVisible(true);
}
}
2.4监听事件
public class TestActionEvent {
public static void main(String[] args) {
//按下按钮,触发一些事件
Frame frame = new Frame();
Button button = new Button();
//因为addActionListener需要一个ActionListener,所以需要构造一个
MyActionListen myActionListen = new MyActionListen();
button.addActionListener(myActionListen );
frame.add(button,BorderLayout.CENTER);
frame.pack();//自适应
frame.setVisible(true);//可见性;
windowClose(frame);
}
//关闭窗体的事件
public static void windowClose(Frame frame) {
frame.addWindowListener(new WindowAdapter() {
-
两个按钮实现同一个监听
**
* 两个按钮 实现同一个监听
*/
public class TestActionTwo {
public static void main(String[] args) {
Frame frame = new Frame("开始-停止");
Button button1 = new Button("START");
Button button2 = new Button("STOP");
MyMonitor myMonitor = new MyMonitor();
button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor);
//可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值
//可以多个按钮只写一个监听类
button1.setActionCommand("shen");
button2.setActionCommand("shen");
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
windowClose(frame);
frame.pack();
frame.setVisible(true);
}
}
//监听类
class MyMonitor implements ActionListener{
2.5输入框TextField监听
public class TestText01 {
public static void main(String[] args) {
//主类只干一件事情,启动
MyFrame myFrame = new MyFrame();
windowClose(myFrame);//这里可以关闭窗口呀,啊哈哈,但是在MyFrame中就不可以哦
}
}
class MyFrame extends Frame {
public MyFrame(){//构造器
TextField textField = new TextField();//文本框
add(textField);
//监听这个文本框输入的文字
MyActionListener2 listener2 = new MyActionListener2();
//按下enter, 就会触发这个输入框的事件
textField.addActionListener(listener2);
//设置替换编码
textField.setEchoChar('*');
pack();
setVisible(true);
}
}
class MyActionListener2 implements ActionListener{
2.6简易计算器,组合+内部类回顾学习
-
组合
当前代码:
/**
* 简易计算器
*/
public class TestCalc {
public static void main(String[] args) {
new Calculator();
windowClose(new Calculator());
}
}
//计算器类
class Calculator extends Frame {
public Calculator() {
//3个文本框
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(10);
//一个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(num1,num2,num3));//实现等号
//1个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器类
class MyCalculatorListener implements ActionListener{
//获取三个变量
private TextField num1,num2,num3;
public MyCalculatorListener(TextField num1, TextField num2, TextField num3) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
改进之后:完全面向对象的写法
/**
* 简易计算器
*/
public class TestCalc {
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.loadFrame();
windowClose(calculator);
}
}
//计算器类
class Calculator extends Frame {
//属性
TextField num1,num2,num3;
//方法
public void loadFrame(){
//3个文本框
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(10);
Button button = new Button("=");
Label label = new Label("+");
button.addActionListener(new MyCalculatorListener(this));//实现等号
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器类
class MyCalculatorListener implements ActionListener{
//获取三个变量
Calculator calculator=null;//组合,在一个类中组合另一个类
public MyCalculatorListener( Calculator calculator) {
this.calculator=calculator;
}
最终版本:改进为内部类
/**
* 简易计算器
*/
public class TestCalc {
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.loadFrame();
windowClose(calculator);
}
}
//计算器类
class Calculator extends Frame {
//属性
TextField num1,num2,num3;
//方法
public void loadFrame(){
//3个文本框
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(10);
Button button = new Button("=");
Label label = new Label("+");
button.addActionListener(new MyCalculatorListener());//实现等号
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
//监听器类
//内部类:最大的好处就是畅通无阻的使用外部类的属性和方法
private class MyCalculatorListener implements ActionListener{
//获取三个变量
Calculator calculator=null;//组合,在一个类中组合另一个类
public MyCalculatorListener() {
this.calculator=calculator;
}
2.7画笔Paint
//画笔
public class TestPaint {
public static void main(String[] args) {
MyPaint myPaint = new MyPaint();
myPaint.loadFrame();
windowClose(myPaint);
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(200,200,600,500);
setVisible(true);
}
//画笔
2.8鼠标监听事件
/**
* 鼠标监听事件
*/
public class TestMouseListener {
public static void main(String[] args) {
MyFrame2 myFrame = new MyFrame2("画图");
}
}
//自己的类
class MyFrame2 extends Frame{
//画画需要画笔,需要监听鼠标当当前的位置,需要集合来存储这个点
ArrayList points;
public MyFrame2(String title){
super(title);
setBounds(200,200,400,400);
//存鼠标点击的点
points=new ArrayList<>();
setVisible(true);
//鼠标监听器,正对这个窗口
this.addMouseListener(new MyMouseListener());
}
2.9窗口监听
/**
* 监听窗口
*/
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame {
public WindowFrame(){
setBackground(Color.blue);
setBounds(200,200,200,200);
setVisible(true);
//第一种方法 内部类
//addWindowListener(new MyWindowListener());
//第二种方法:匿名内部类
this.addWindowListener(new WindowAdapter(){
// @Override
// public void windowClosing(WindowEvent e) {
// setVisible(false);//隐藏窗口,通过按钮隐藏当前窗口
// System.exit(0);//正常退出
// }
//重写一些方法
2.10键盘监听
/**
* 键盘监听
*/
public class TestKey {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame() {
setBounds(200,200,200,200);
setVisible(true);
//匿名内部类
this.addKeyListener(new KeyAdapter() {
3.swing
3.1窗口、标签
//窗口、标签
public class JFrameDemo {
//init();初始化
public void init(){
//顶级窗口
JFrame jf = new JFrame("JFrame窗口");
jf.setVisible(true);
jf.setBounds(200,200,200,200);
jf.setBackground(Color.blue);
//设置文字JLable
JLabel label = new JLabel("狂神小课堂");
jf.add(label);
//关闭事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
JFrameDemo demo = new JFrameDemo();
demo.init();
}
标签居中
public class JFrameDemo02 {
public static void main(String[] args) {
new MyJFrame().init();
}
}
class MyJFrame extends JFrame{
public void init(){
setVisible(true);
setBounds(200,200,200,200);
//设置文字JLable
JLabel label = new JLabel("狂神小课堂");
add(label);
//让文本标签居中
label.setHorizontalAlignment(SwingConstants.CENTER);
//获得一个容器
Container container = this.getContentPane();
container.setBackground(Color.YELLOW);
}
}
3.2弹窗
/**
*弹窗,JDialog 默认有关闭事件
*/
//主窗口
public class DialogDemo extends JFrame {
public DialogDemo(){
this.setVisible(true);
this.setSize(200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame 放东西 容器
Container container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton button = new JButton("点击弹出一个对话框");
button.setBounds(500,300,300,620);
//点击这个按钮的时候,弹出一个弹窗
button.addActionListener(new ActionListener() {//监听器
3.3标签
-
label
new Label("xxx")
-
icon
-
图标Icon
//图标 ,需要实现类 Frame继承
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
public IconDemo(){
}
public IconDemo(int width, int height){
this.width = width;
this.height = height;
}
public void init(){//初始化
IconDemo iconDemo=new IconDemo(15,15);
//图标放在标签,也可以放在按钮上
JLabel label = new JLabel("icontest",iconDemo,SwingConstants.CENTER);
Container container=this.getContentPane();
container.add(label);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new IconDemo().init();
}
-
图片Icon
public class ImageIconDemo extends JFrame {
public ImageIconDemo(){
//获取图片的地址
JLabel label = new JLabel("ImageIcon");
URL url = ImageIconDemo.class.getResource("IMG_1639.JPG");//获取当前类下的资源的地址
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100,100,200,200);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
-
3.4面板
/**
* 面板
*/
public class JPanelDemo extends JFrame {
public JPanelDemo(){
Container container = getContentPane();
container.setLayout(new GridLayout(1,1,10,10));
JPanel panel = new JPanel(new GridLayout(2,1));
Component component = panel.add(new JButton("1"));
container.add(component);
setVisible(true);
setBounds(100,100,100,100);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
/**
* 有滚动条
*/
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("学Java");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
3.5按钮
-
图片按钮
/**
* 按钮
*/
public class JButtonDemo01 extends JFrame {
public JButtonDemo01(){
Container container = getContentPane();
//获取图片资源
URL url = JButtonDemo01.class.getResource("2.png");
Icon icon = new ImageIcon(url);
//把图标放在按钮上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮");
//add
container.add(button);
this.setVisible(true);
this.setBounds(100,100,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
} -
单选按钮
/**
*单选按钮
*/
public class JButtonDemo02 extends JFrame{
public JButtonDemo02(){
Container container = getContentPane();
//获取图片资源
URL url = JButtonDemo01.class.getResource("2.png");
Icon icon = new ImageIcon(url);
//单选框
JRadioButton jRadioButton1 = new JRadioButton("JRadioButton1");
JRadioButton jRadioButton2 = new JRadioButton("JRadioButton2");
JRadioButton jRadioButton3 = new JRadioButton("JRadioButton3");
//把框放在按钮上,由于只能选择一个,所以要建一个组
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
buttonGroup.add(jRadioButton3);
//add
container.add(jRadioButton1,BorderLayout.SOUTH);
container.add(jRadioButton2,BorderLayout.NORTH);
container.add(jRadioButton3,BorderLayout.CENTER);
this.setVisible(true);
this.setBounds(100,100,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02();
}
} -
复选按钮
/**
* 多选框
*/
public class JButtonDemo03 extends JFrame{
public JButtonDemo03(){
Container container = getContentPane();
//获取图片资源
URL url = JButtonDemo01.class.getResource("2.png");
Icon icon = new ImageIcon(url);
//多选框
JCheckBox checkBox1 = new JCheckBox("JCheckBox1");
JCheckBox checkBox2 = new JCheckBox("JCheckBox2");
JCheckBox checkBox3 = new JCheckBox("JCheckBox3");
container.add(checkBox1,BorderLayout.SOUTH);
container.add(checkBox2,BorderLayout.NORTH);
container.add(checkBox3,BorderLayout.CENTER);
this.setVisible(true);
this.setBounds(100,100,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}
3.6列表
-
下拉框
/**
* 下拉框JComboBox
*/
public class TestComboboxDemo extends JFrame {
public TestComboboxDemo() {
Container container = getContentPane();
//code
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("正在上映");
status.addItem("敬请期待");
status.addItem("已下架");
container.add(status);
setVisible(true);
setSize(300,400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo();
}
} -
列表框
/**
* 列表框JList
*/
public class TestComboboxDemo02 extends JFrame {
public TestComboboxDemo02() {
Container container = getContentPane();
//code
//生成列表的内容
//String[] contents = {"1","2","3"};
Vector contents = new Vector();
//列表中需要放内容
JList list = new JList(contents);
contents.add("熊大");
contents.add("熊二");
contents.add("光头强");
container.add(list);
setVisible(true);
setSize(300,400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo02();
}
} -
用途
1、选择地区,或者一个单个选项
2、列表,展示信息,一般是动态扩容
3.7文本框
-
文本框
/**
* 文本框
*/
public class TestTextDemo01 extends JFrame {
public TestTextDemo01() {
Container container = getContentPane();
//code
JTextField textField1 = new JTextField("熊大");
JTextField textField2 = new JTextField("熊二");
container.add(textField1,BorderLayout.CENTER);
container.add(textField2,BorderLayout.NORTH);
setVisible(true);
setSize(500,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo01();
}
} -
密码框
/**
* 密码框
*/
public class TestTextDemo02 extends JFrame {
public TestTextDemo02() {
Container container = getContentPane();
//code
JPasswordField field = new JPasswordField();
field.setEchoChar('*');
container.add(field);
setVisible(true);
setSize(500,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo02();
}
} -
文本域
/**
* 有滚动条
* 文本域
*/
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("学Java");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
} -
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?