GUI编程
GUI编程
本来听完面试的时候不会用到,就想跳过,结果说到为什么学习,又把我留住了。
简介
GUI的核心技术:Swing/AWT, java 做GUI没有流行起来,这两个技术也没有流行起来
- 因为页面不美观
- 需要jre环境 80兆左右大小
为什么要学习:了解mvc的思想,了解监听
AWT
Awt介绍
Awt(Abstract windows tool) 是Swing的前身
包含了很多类和接口
TestFrame.java
package com.example.demo_kuang.gui;
import java.awt.*;
public class TestFrame {
public static void main(String[] args) {
Frame frame = new Frame("第一个java图像界面窗口");
// 设置可见性
frame.setVisible(true);
// 设置大小
frame.setSize(224, 224);
// 设置背景颜色
frame.setBackground(new Color(0, 195, 254));
// 初始弹窗位置
frame.setLocation(800, 800);
// 设置窗口可变性
frame.setResizable(false);
}
}
TestFrame2.java
package com.example.demo_kuang.gui;
import java.awt.*;
public class TestFrame2 {
public static void main(String[] args) {
new MyFrame(100, 100, 200, 200, Color.BLUE);
new MyFrame(300, 100, 200, 200, Color.YELLOW);
new MyFrame(100, 300, 200, 200, Color.RED);
new MyFrame(300, 300, 200, 200, Color.GRAY);
}
}
class MyFrame extends Frame {
private static final long serialVersionUID = 4374413037835618570L;
static int id = 0;
public MyFrame(int x, int y, int width, int height, Color color) {
super("MyFrame" + (++id));
setVisible(true);
setBounds(x, y, width, height);
setBackground(color);
}
}
面板 Panel
package com.example.demo_kuang.gui;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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(16, 110, 206));
panel.setBounds(50, 50, 400, 400);
panel.setBackground(new Color(253, 1, 140));
frame.add(panel);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
布局管理器
流式布局
package com.example.demo_kuang.gui;
import java.awt.*;
public class TestFlowLayOut {
public static void main(String[] args) {
Frame frame = new Frame("TestFlowLayOut");
frame.setLayout(new FlowLayout(FlowLayout.LEFT)); // 设置为流式布局 默认是居中的
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
Button button4 = new Button("button4");
frame.setBounds(100, 100, 200, 200);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.setVisible(true);
}
}
东西南北中
package com.example.demo_kuang.gui;
import java.awt.*;
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestLayOut2");
frame.setLayout(new 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.setBounds(100, 100, 200, 200);
frame.setVisible(true);
}
}
表格布局
package com.example.demo_kuang.gui;
import java.awt.*;
public class TestGridLayOut {
public static void main(String[] args) {
Frame frame = new Frame("TestGridLayOut");
frame.setLayout(new GridLayout(3, 2));
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.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.pack(); // 自动显示大小,可以不手动设置窗口的大小和位置
frame.setVisible(true);
}
}
总结
- Frame是一个顶级窗口
- Panel无法单独显示,必须添加到某个容器中
- 布局管理器(流式/东西南北中/表格布局)
- 元素(颜色、大小、显示、布局...)
监听器
多个组件可以共用一个监听
package com.example.demo_kuang.gui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestActionListener {
public static void main(String[] args) {
MyFrame1 frame = new MyFrame1();
}
}
class MyFrame1 extends Frame {
private static final long serialVersionUID = 414120154449511005L;
public MyFrame1() {
super("MyFrame");
Button button = new Button("button");
TextField textField = new TextField();
textField.addActionListener(new MyActionListener());
add(textField, BorderLayout.NORTH);
textField.setEchoChar('*'); // 类似密码显示*
TextField textField1 = new TextField();
textField1.addActionListener(new MyActionListener());
add(textField1, BorderLayout.SOUTH);
pack();
setVisible(true);
}
}
class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
TextField textField = (TextField) e.getSource(); // 获取输入框的文字
System.out.println(textField.getText());
}
}
简单的计算器实现
package com.example.demo_kuang.gui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalculator {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
class Calculator extends Frame {
private static final long serialVersionUID = -3721907805256514190L;
private TextField textField1;
private TextField textField2;
private TextField textField3;
public void loadFrame() {
//元素
textField1 = new TextField(10);
textField2 = new TextField(10);
textField3 = new TextField(20);
Label label = new Label("+");
Button button = new Button("=");
// 添加监听
button.addActionListener(new MyActionListener());
//添加
setLayout(new FlowLayout());
add(textField1);
add(label);
add(textField2);
add(button);
add(textField3);
// 设置可见性
pack();
setVisible(true);
}
private class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
int num1 = Integer.parseInt(textField1.getText());
int num2 = Integer.parseInt(textField2.getText());
textField3.setText("" + (num1 + num2));
textField1.setText("");
textField2.setText("");
}
}
}