java-gui

简介

java-gui不流行的原因:界面不美观、需要jre环境

swing、AWT

AWT

abstract windows

java.awt

Frame

setBounds设置Location和长宽

setBackground设置Frame的背景颜色

setVisible设置可见性

Frame中可以添加panel

关闭窗口:addWindowListener

1
2
3
4
5
6
7
8
//适配器模式
frame.addWindowListener(new WindowAapter()){
@Override
public void windowClosing(WindowEvent e){
//退出程序
System.exit(0);
}
}

panel

三种布局管理器

流式布局

FlowLayout()

一排一排的摆放,第一行摆完了再摆第二行

边界布局

BorderLayout()

中间会比较大,其他的地方比较小

表格布局

GridLayout()

frame.pack()自动填充大小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.awt.*;

public class test {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setLayout(new GridLayout(2, 1));

frame.setVisible(true);
frame.setBounds(200, 200, 400, 400);
frame.setBackground(Color.BLACK);

Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new GridLayout(2, 2));
Panel p3 = new Panel(new BorderLayout());
Panel p4 = new Panel(new GridLayout(2, 1));

p1.add(new Button("WEST-1"), BorderLayout.WEST);
p1.add(new Button("EAST-1"), BorderLayout.EAST);

for(int i=0; i<4;i++){
p2.add(new Button("CENTER-"+(i+1)));
}

p1.add(p2, BorderLayout.CENTER);

// p2.add(new Button("CENTER-1"));
// p2.add(new Button("CENTER-2"));
// p2.add(new Button("CENTER-3"));
// p2.add(new Button("CENTER-4"));


p4.add(new Button("CENTER-5"));
p4.add(new Button("CENTER-6"));

p3.add(new Button("WEST-2"), BorderLayout.WEST);
p3.add(new Button("EAST-2"), BorderLayout.EAST);
p3.add(p4, BorderLayout.CENTER);

frame.add(p1);
frame.add(p3);
}
}

事件监听

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class testactionlisterner {
public static void main(String[] args){
Frame frame = new Frame();
Button button = new Button("test");

// 因为addActionListener需要一个ActionListener所以我们需要构造一个ActionListener
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) {
super.windowClosing(e);
System.exit(0);
}
});
}


}

class MyActionListener implements ActionListener{

@Override
public void actionPerformed(ActionEvent e){
System.out.println("abc");
}
}

输入框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Testtext1 {
public static void main(String[] args) {
new MyFrame();
}
}

class MyFrame extends Frame{
public MyFrame(){
//初始化方法
TextField textField = new TextField();
add(textField);
textField.setEchoChar('*');

//监听该文本框的输入文字
MyActionListener2 myActionListener2 = new MyActionListener2();
textField.addActionListener(myActionListener2);

pack();
setVisible(true);
}
}

class MyActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
//按下回车会获得文本框中的输入内容
// System.out.println(e.getSource());
TextField content = (TextField)e.getSource();
System.out.println(content.getText());
}
}

简易计算器

oop原则:优先使用组合,组合大于继承(继承会增加耦合性 )

1
2
3
4
5
6
7
8
9
//继承
class A extends B{

}

//组合
class A{
public B b;
}

初始代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestCalc {
public static void main(String[] args) {
new Calculator();
}
}

//计算器类
class Calculator extends Frame{
public Calculator(){
//3个文本框
TextField num1 = new TextField(10); //最大的字符数
TextField num2 = new TextField(10); //最大的字符数
TextField num3 = new TextField(20); //最大的字符数

//1个按钮
Button button = new Button("=");

//1个标签
Label label = new Label("+");

setLayout(new FlowLayout());

add(num1);
add(label);
add(num2);
add(button);
add(num3);



MyCalculatorListener myCalculatorListener = new MyCalculatorListener(num1, num2, num3);
button.addActionListener(myCalculatorListener);


pack();
setVisible(true);
}


//关闭窗体的事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}

//监听器类
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;
}

@Override
public void actionPerformed(ActionEvent e){
//获得加数和被加数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());

//将这个值加法运算后放在第三个框里
num3.setText(""+(n1+n2));

//清除前两个框
num1.setText("");
num2.setText("");

}
}

改为面向对象的写法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestCalc {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}

//计算器类
class Calculator extends Frame{
//属性
TextField num1, num2, num3;

//方法
public void loadFrame(){
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);

Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(this));

Label label = new Label("+");

setLayout(new FlowLayout());

add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
windowClose(this);

}

//关闭窗体的事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}

//监听器类
class MyCalculatorListener implements ActionListener {
Calculator calculator = null;

public MyCalculatorListener(Calculator calculator){
this.calculator = calculator;
}

@Override
public void actionPerformed(ActionEvent e){

int n1 = Integer.parseInt(calculator.num1.getText());
int n2 = Integer.parseInt(calculator.num2.getText());
calculator.num3.setText(""+(n1+n2));
calculator.num1.setText("");
calculator.num2.setText("");
}
}

使用内部类进行优化:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestCalc {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}

//计算器类
class Calculator extends Frame{
//属性
private TextField num1, num2, num3;

//方法
public void loadFrame(){
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);

Button button = new Button("=");
button.addActionListener(new MyCalculatorListener());

Label label = new Label("+");

setLayout(new FlowLayout());

add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
windowClose(this);

}

//关闭窗体的事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}

//监听器类
private class MyCalculatorListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e){

int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
num3.setText(""+(n1+n2));
num1.setText("");
num2.setText("");
}
}
}

画笔

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.awt.*;


public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}

class MyPaint extends Frame{

public void loadFrame(){
setBounds(200,200,600,500);
setVisible(true);

}

@Override
public void paint(Graphics g){
g.setColor(Color.BLUE);
g.drawOval(100, 100, 100, 100); //空心的圆
g.fillOval(100,100,100,100); //实心的圆

}
}

鼠标监听

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;

public class TestMouseListener {
public static void main(String[] args) {
new MyFrame2("Drawing");
}
}

class MyFrame2 extends Frame{
// 需要监听鼠标来获得鼠标当前位置,并且用list来存储这些点

ArrayList points;

public MyFrame2(String title){
super(title); // super的用法复习一下
setBounds(200, 200, 400, 300);

//存放鼠标点击获得的点
points = new ArrayList<>();

//鼠标监听器
this.addMouseListener(new MyMouseListener());
setVisible(true);
}

@Override
public void paint(Graphics g){
//监听鼠标事件,将获得的点传入list中
Iterator iterator = points.iterator();
while(iterator.hasNext()){
Point point = (Point) iterator.next();
g.setColor(Color.RED);
g.fillOval(point.x, point.y, 10, 10);
}

}

//添加一个点到界面上
public void addPaint(Point point){
points.add(point);
}

//适配器模式
private static class MyMouseListener extends MouseAdapter{
//只需要按压
@Override
public void mousePressed(MouseEvent e){
MyFrame2 frame = (MyFrame2) e.getSource();
//我们通过这个frame获得x,y
frame.addPaint(new Point(e.getX(),e.getY()));

//每次点击鼠标都需要重新画一遍
frame.repaint();

}
}
}

窗口监听

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestWindowListener {
public static void main(String[] args) {
new WindowFrame();
}
}

class WindowFrame extends Frame {
public WindowFrame(){
setVisible(true);
setBounds(200,200,200,200);
//匿名内部类
addWindowListener(
new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("closed");
System.exit(0);
}

@Override
public void windowActivated(WindowEvent e) {
System.out.println("activated");
}
}
);
}
}

键盘监听

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Testkeylistener {
public static void main(String[] args) {
new Mykeylistener();
}
}

class Mykeylistener extends Frame {
public Mykeylistener(){
setVisible(true);
setBounds(10,10,100,100);
addKeyListener(
new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int keycode = e.getKeyCode();
if(keycode == KeyEvent.VK_UP){
System.out.println("up up up");
}
}
}
);
}
}

Swing

窗口、面板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import javax.swing.*;
import java.awt.*;

public class JFrameDemo1 {
public static void main(String[] args) {
new MyJrame1().init();
}
}

class MyJrame1 extends JFrame{
public void init(){
setBounds(10,10,100,100);
setVisible(true);

JLabel label = new JLabel("hello world");
//设置标签居中
label.setHorizontalAlignment(SwingConstants.CENTER);
add(label);


//JFrame中设置颜色需要设置容器的颜色
Container container = getContentPane();
container.setBackground(Color.RED);

//关闭事件
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}

弹窗

新建一个弹窗类,然后用事件监听去绑定这个弹窗,在点击按钮之后new我们新写的弹窗类

标签

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import javax.swing.*;
import java.awt.*;

public class TestIcon extends JFrame implements Icon{
private int width;
private int height;

public TestIcon(){} // 无参构造

public TestIcon(int width, int height){
this.width = width;
this.height = height;
} //有参构造

public void init(){
TestIcon icondemo = new TestIcon(15, 15);
// icon可以放在标签上,也可以放在按钮上
JLabel icon = new JLabel("icon", icondemo, SwingConstants.CENTER);

Container container = getContentPane();
container.add(icon);


this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
new TestIcon().init();
}

@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x, y, width, height); //画笔可以画一个圆
}

@Override
public int getIconWidth() {
return this.width;
}

@Override
public int getIconHeight() {
return this.height;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class ImageIconDemo extends JFrame {
public ImageIconDemo(){
//获取该类同级目录下的图片
JLabel label = new JLabel("ImageIcon");
URL url = ImageIconDemo.class.getResource("timg.jpg");

ImageIcon imageIcon = new ImageIcon(url);

label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);

Container container = getContentPane();
container.add(label);

this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(100,100,200,200);
}

public static void main(String[] args) {
new ImageIconDemo();
}
}
// 改代码遇到了空指针的问题,图片要放在src文件夹下,这里才是和类在同一文件夹的地方

JScroll面板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import javax.swing.*;
import java.awt.*;

public class JScollDemo extends JFrame {
public JScollDemo() {
Container container = getContentPane();

JTextArea textarea = new JTextArea(20,50);
textarea.setText("jscoll");
JScrollPane scollpane = new JScrollPane(textarea);
container.add(scollpane);

this.setVisible(true);
this.setBounds(100,100,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
new JScollDemo();
}
}

图片按钮、单选框、多选框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class IconButtonDemo extends JFrame{
public IconButtonDemo() {
URL url = IconButtonDemo.class.getResource("timg.jpg");
Icon icon = new ImageIcon(url);

JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("image_button");

Container container = getContentPane();
container.add(button);
this.setVisible(true);
this.setBounds(100,100,100,100);


}

public static void main(String[] args) {
new IconButtonDemo();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import javax.swing.*;
import java.awt.*;

public class JRadioButtonDemo extends JFrame {
public JRadioButtonDemo(){
Container container = this.getContentPane();

//创建单选按钮
JRadioButton rb1 = new JRadioButton("rb1");
JRadioButton rb2 = new JRadioButton("rb2");
JRadioButton rb3 = new JRadioButton("rb3");

//创建按钮组,来实现单选功能,因为同一个组中只能有一个按钮被选择
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(rb1);
buttonGroup.add(rb2);
buttonGroup.add(rb3);

container.add(rb1, BorderLayout.NORTH);
container.add(rb2, BorderLayout.CENTER);
container.add(rb3, BorderLayout.SOUTH);


this.setVisible(true);
this.setBounds(100,100,100,100);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

public static void main(String[] args) {
new JRadioButtonDemo();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import javax.swing.*;
import java.awt.*;

public class JCheckBoxDemo extends JFrame {
public JCheckBoxDemo() throws HeadlessException {
Container container = getContentPane();

JCheckBox check1 = new JCheckBox("check1");
JCheckBox check2 = new JCheckBox("check2");

container.add(check1, BorderLayout.NORTH); //假如不写不布局可能只出现check2
container.add(check2, BorderLayout.SOUTH);

this.setVisible(true);
this.setBounds(100,100,100,100);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
new JCheckBoxDemo();
}
}

下拉框、列表框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import javax.swing.*;
import java.awt.*;

public class ComboboxDemo extends JFrame {
public ComboboxDemo() throws HeadlessException {
Container container = getContentPane();

JComboBox box = new JComboBox();
box.addItem(null);
box.addItem("1");
box.addItem("2");
box.addItem("3");

container.add(box);

this.setVisible(true);
this.setSize(200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
new ComboboxDemo();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import javax.swing.*;
import java.awt.*;
import java.util.Vector;

public class JListDemo extends JFrame {
public JListDemo() throws HeadlessException {
Container container = getContentPane();

// String[] content = {"1", "2", "3"};
// JList<String> jList = new JList<String>(content);

Vector contents = new Vector();
JList jList = new JList(contents);

container.add(jList);

contents.add("1");
contents.add("2");
contents.add("3");



this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(100,100);
}

public static void main(String[] args) {
new JListDemo();
}
}

文本框、密码框、文本域

TextArea文本域可以换行,TextField文本框不能换行

1
2
JPasswordField pass = new JPasswordField();
pass.setEchoChar("*");

SnakeGame

https://github.com/risuxx/SnakeGame