GUI图形用户界面

本文最后更新于:1 年前

GUI

GUI全称Graphical User Interface,图形用户界面

组件

  • 窗口

  • 弹窗

  • 面板

  • 文本框

  • 列表框

  • 按钮

  • 图片

  • 监听事件

  • 鼠标

  • 键盘事件

简介

GUI核心技术:AWT、SWing

为什么不火

  1. 界面不美观

  2. 需要jre环境

为什么要学习GUI

  1. MVC,了解监听器
  2. 可以写出自己心中想要的工具
  3. 工作中可能维护到SWing界面

AWT

AWT介绍

AWT(Abstract Windows Toolkit)抽象窗口工具

包含很多类和接口

元素:窗口、按钮、文本框

Java.awt包的层次结构如下

image-20210511142206455

组件和容器Component and Container

容器

容器(Container)也是一个类,实际上是Component的子类,因此容器本身也是一个组件,具有组件的所有性质,但是它的主要功能是容纳其它组件和容器。容器可以简化图形化界面的设计,以整体结构来布置界面。所有的容器都可以通过add()方法向容器中添加组件。常用的容器3种:Panel, Frame, Applet。

来源:百度百科

框架Frame

构造方法:

1
Frame frame = new Frame("");

要生成一个窗口,通常使用Window的子类Frame类进行实例化,而不是直接使用Window 类,框架的外观就像平常Windows系统下的窗口,有标题、边框、菜单和大小等。setSize()方法可以设置框架尺寸的大小,setVisibe()方法可以设置窗口的可见性。

来源:百度百科

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package cn.linsip.lesson01;

import java.awt.*;

//第一个图形界面
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(1, 1, 1));
//设置弹出的初始位置
frame.setLocation(0, 0);
//设置大小固定
frame.setResizable(false);
}
}

窗口出现问题

  1. 窗口关闭不掉

  2. 解决方法:停止Java程序运行

回顾封装:

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
package cn.linsip.lesson01;

import java.awt.*;

//封装TestFrame
public class TestFrame02 {
public static void main(String[] args) {
//开启多个窗口
MyFrame myFrame1 = new MyFrame(200, 200, 200, 200);
MyFrame myFrame2 = new MyFrame(400, 200, 200, 200);
MyFrame myFrame3 = new MyFrame(200, 400, 200, 200);
MyFrame myFrame4 = new MyFrame(400, 400, 200, 200);
}
}
//自定义类来继承Frame实现封装
class MyFrame extends Frame {
//定义静态变量,用于区别多个窗口
static int id;

public MyFrame(int x, int y, int w, int h) {
//调用父类即Frame
super("MyFrame" + (++id));
//设置参数
setVisible(true);
setBackground(Color.black);
setBounds(x, y, w, h);
setResizable(false);
}
}

面板panel

框架一般用作Java应用程序的窗口,而Applet是Java小程序的窗口。与Frame不同,Applet是在网页中显示的,也可以通过添加Panel进行组件布局。

来源:百度百科

Panel一个空间,非独立存在

可以在panel中添加文本框、文本域、列表、单选按钮、复选按钮、画布、标签、字体大小、事件、滚动条

构造方法

1
Panel panel = new Panel();

示例

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
package cn.linsip.lesson01;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

//Panel
public class TestPanel {
public static void main(String[] args) {
Frame frame = new MyFrame(100, 100, 400, 400, Color.RED);
Panel panel = new Panel();
//设置布局
frame.setLayout(null);

//设置坐标
panel.setBounds(100, 100, 200, 200);//Panel的坐标是相对于Frame的
panel.setBackground(Color.white);
//panel加入frame
frame.add(panel);
//窗口监听WindowListener,实现关闭窗口功能,监听器参数是一个内部类WindowAdapter

//适配器模式
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

}
}

布局管理器

布局管理器(LayoutManager):每个容器都有一个布局管理器,当容器需要对某个组件进行定位或判断其大小、尺寸时,就会调用其对应的布局管理器。使用布局管理器可以实现跨平台的特性,并且获得动态的布局效果。布局管理器负责管理组件的排列顺序、大小和位置。不同的布局管理器使用不同的布局策略,容器可以通过选择不同的布局管理器来决定如何布局。

来源:百度百科

  1. 流式布局Flowlayout

    FlowLayout是Panel 和 Applet 的默认布局管理器。在该布局管理器中,组件在容器中按照从上到下,从左到右的顺序进行排列,行满后则换行。

    构造方法

    1
    2
    3
    4
    5
    6
    //默认构造方法,设置居中对齐方式,横向和纵向间隔均为默认5像素
    FlowLayout();
    //设置居左对齐,横向和纵向间隔均为默认5像素
    FlowLayout(new FlowLayout.LEFT);
    //设置居右对齐,横向间隔20像素,纵向间隔20像
    FlowLayout(new FlowLayout.RIGHT, 20, 20);

    示例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    package cn.linsip.lesson01;

    import java.awt.*;

    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));
    frame.setVisible(true);
    frame.setBounds(100,100,200,200);
    //添加Buttun到Frame
    frame.add(button1);
    frame.add(button2);
    frame.add(button3);
    }
    }

  2. 边界布局管理器BorderLayout

    BorderLayout是Window、Frame和Dialog的默认布局管理器,其将容器分成North、South、East、West和Center 5个区域,每个区域只能放置一个组件。在使用add()方法添加组件到容器时,必须指定将其放置在哪个区域中。使用BorderLayout时,如果容器大小发生变换,组件的相对位置不变。

    来源:百度百科

    构造方法

    1
    BorderLayout bl = new BorderLayout();

    示例

    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
    package cn.linsip.lesson01;

    import java.awt.*;

    public class TestBorderTest {
    public static void main(String[] args) {
    //一个Frame
    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
    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属性
    frame.setVisible(true);
    frame.setBounds(100,100,200,200);


    }
    }

  3. 网格布局管理器GridLayout

    GridLayout 可使容器中的各个组件呈网格状布局,平局占据容器的空间,即使容器的大小发生变化,每个组件还是平均占据容器的空间。和FlowLayout一样,GridLayout也是按照从上到下,从左到右的规律进行排列的。

    来源:百度百科

    构造方法

    1
    GridLayout = new GridLayout();

    示例

    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
    package cn.linsip.lesson01;

    import java.awt.*;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;

    public class TestGridLayout {
    public static void main(String[] args) {
    Frame frame = new Frame();
    frame.setVisible(true);
    Button button1 = new Button("butten1");
    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.setBounds(100,100,200,200);
    frame.add(button1);
    frame.add(button2);
    frame.add(button3);
    frame.add(button4);
    frame.add(button5);
    frame.add(button6);
    frame.pack();//Java函数,自动匹配大小
    frame.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    }
    }

练习

image-20210511150249932

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
package cn.linsip.lesson01;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class FrameDemo {
public static void main(String[] args) {
Frame frame = new Frame("练习");
Panel panel1 = new Panel(new BorderLayout());
Panel panel2 = new Panel(new GridLayout(2, 1));
Panel panel3 = new Panel(new BorderLayout());
Panel panel4 = new Panel(new GridLayout(2, 2));
frame.setLayout(new GridLayout(2, 1));
frame.setSize(400, 400);
frame.setVisible(true);

panel1.add(new Button("east"), BorderLayout.EAST);
panel1.add(new Button("west"), BorderLayout.WEST);
panel2.add(new Button("p2-1"));
panel2.add(new Button("p2-2"));

panel1.add(panel2, BorderLayout.CENTER);
frame.add(panel1);

panel3.add(new Button("east"), BorderLayout.EAST);
panel3.add(new Button("west"), BorderLayout.WEST);
panel4.add(new Button("p4-1"));
panel4.add(new Button("p4-2"));
panel4.add(new Button("p4-3"));
panel4.add(new Button("p4-4"));

panel3.add(panel4, BorderLayout.CENTER);
frame.add(panel3);

frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

}
}

总结:

  1. Frame是一个顶级窗口
  2. Panel无法单独显示,必须添加其到某个容器中
  3. 掌握3种布局管理器
  4. Frame设置大小、定位、背景颜色、可见性、监听。

事件监听

Java事件监听器是由事件类和监听接口组成,自定义一个事件前,必须提供一个事件的监听接口以及一个事件类。JAVA中监听接口是继承java.util.EventListener的类,事件类继承java.util.EventObject的类。

来源:百度百科

事件发生的时候,该干什么

示例:

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
package cn.linsip.lesson02;

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 TestActionEvent01 {
public static void main(String[] args) {
//按下按钮,触发事件
Frame frame = new Frame();
Button button = new Button("say hello");
//给按钮添加一个事件监听,ActionListener是一个接口,需要一个ActionListener参数,我们自定义一个类来实现这个接口
MyActionListener myActionListener = new MyActionListener();
//按钮添加监听
button.addActionListener(myActionListener);
//按钮加入frame
frame.add(button, BorderLayout.CENTER);
//设置窗口属性
frame.pack();
frame.setVisible(true);
//我们这里重写以下关闭窗口的方法
windowClosing(frame);
}
//关闭窗口的方法
private static void windowClosing(Frame frame) {
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//自定义类来实现接口ActionListener
class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Hello World");
}
}

image-20210511152225598

两个按钮共用一个事件

示例

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
package cn.linsip.lesson02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestActionEvent02 {
public static void main(String[] args) {
//俩按钮实现一个事件
Frame frame = new Frame();
Button button1 = new Button("start");
Button button2 = new Button("stop");
//自定义类来实现事件监听
MyMonitor myActionListener = new MyMonitor();
//按钮添加监听
button1.addActionListener(myActionListener);
button2.addActionListener(myActionListener);
//frame添加button
frame.add(button1, BorderLayout.NORTH);
frame.add(button2, BorderLayout.SOUTH);

//frame属性
frame.pack();
frame.setVisible(true);
windowClosing(frame);
}
private static void windowClosing(Frame frame) {
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}

class MyMonitor implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按钮getCommand:" + e.getActionCommand());
}
}

image-20210511152612642

注:

优先用setActionCommand(),其次才是Button的标签

1
button1.setActionCommand("s");

输入框监听TextField

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
package cn.linsip.lesson02;

import cn.linsip.lesson01.TestFrame;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestText {
public static void main(String[] args) {
//启动
new MyFrame();
}
}
//我们用自定义类来封装Frame
class MyFrame extends Frame {
public MyFrame() {
//添加文本框
TextField textField = new TextField();
add(textField);

//监听文本框输入的文字
MyMonitor2 myMonitor2 = new MyMonitor2();
textField.addActionListener(myMonitor2);

//设置替换编码
//textField.setEchoChar('*');

//设置大小,可见性等等
pack();
setVisible(true);
window
}
private static void windowClosing(Frame frame) {
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}

class MyMonitor2 implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
TextField testField = (TextField) e.getSource();//事件获得的资源返回的是一个对象,强转为文本域
System.out.println(testField.getText());//获得文本框内容并打印
testField.setText("");//设置文本框内容为空
}
}

image-20210511171032146

简易的计算器,组合+内部类回归复习

OOP原则:组合,大于继承

组合

1
2
3
4
5
6
7
public class A extends B{

}

public class A{
private B b;
}

练习

加法计算器

如图

image-20210511171601617

思路:

  1. 根据拿到前两个数值,返回第三个数值
  2. 如何拿到前两个数值
  3. 如何返回第三个数值
  4. 进一步优化代码
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
package cn.linsip.lesson02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//main函数用于启动或者测试程序
public class TestCalc {
public static void main(String[] args) {
Calc calc = new Calc();

}
}
//自定义一个计算器类
class Calc extends Frame {
public Calc() {
//3个文本框
TextField text1 = new TextField(20);
TextField text2 = new TextField(20);
TextField text3 = new TextField(20);
//一个按钮+事件
Button button = new Button("=");
button.addActionListener(new MyMonitor3(text1, text2, text3));

//一个标签
Label label = new Label("+");
//Flowlayout布局,
setLayout(new FlowLayout());
add(text1);
add(label);
add(text2);
add(button);
add(text3);

pack();
setVisible(true);

}
}
//自定义一个监听器
class MyMonitor3 implements ActionListener {
//构造方法拿到这三个值
private TextField text1, text2, text3;

public MyMonitor3(TextField text1, TextField text2, TextField text3) {
this.text1 = text1;
this.text2 = text2;
this.text3 = text3;

}

@Override
public void actionPerformed(ActionEvent e) {
//按下按钮事件e,1获取前两个文本框内容,
int num1 = Integer.parseInt(text1.getText());
int num2 = Integer.parseInt(text2.getText());

//2计算返回值到第三个文本框
text3.setText("" + (num1 + num2));

//,3清除前两个框
text1.setText("");
text2.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
package cn.linsip.lesson02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestCalc2 {
public static void main(String[] args) {
new Calc2().loadCalc();
}
}

class Calc2 extends Frame {
TextField text1, text2, text3;
Button button;
Label label;

public void loadCalc() {
//3个文本框
text1 = new TextField(20);
text2 = new TextField(20);
text3 = new TextField(20);
//一个按钮+事件
button = new Button("=");
button.addActionListener(new MyMonitor4(this));
//一个标签
label = new Label("+");


//Flowlayout布局,
setLayout(new FlowLayout());
add(text1);
add(label);
add(text2);
add(button);
add(text3);

pack();
setVisible(true);

}
}

class MyMonitor4 implements ActionListener {
//构造方法拿到这个对象
private Calc2 calc2 = null;

public MyMonitor4(Calc2 calc2) {
this.calc2 = calc2;

}

@Override
public void actionPerformed(ActionEvent e) {
//1获取前两个文本框内容,
//2计算返回值到第三个文本框
//3清除前两个框
int num1 = Integer.parseInt(calc2.text1.getText());
int num2 = Integer.parseInt(calc2.text2.getText());

calc2.text3.setText("" + (num1 + num2));

calc2.text1.setText("");
calc2.text2.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
package cn.linsip.lesson02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestCalc2 {
public static void main(String[] args) {
new Calc2().loadCalc();
}
}

class Calc2 extends Frame {
TextField text1, text2, text3;
Button button;
Label label;

public void loadCalc() {
//3个文本框
text1 = new TextField(20);
text2 = new TextField(20);
text3 = new TextField(20);
//一个按钮+事件
button = new Button("=");
button.addActionListener(new MyMonitor4());
//一个标签
label = new Label("+");


//Flowlayout布局,
setLayout(new FlowLayout());
add(text1);
add(label);
add(text2);
add(button);
add(text3);

pack();
setVisible(true);

}
//内部类
private class MyMonitor4 implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
//1获取前两个文本框内容,
//2计算返回值到第三个文本框
//3清除前两个框
int num1 = Integer.parseInt(calc2.text1.getText());
int num2 = Integer.parseInt(calc2.text2.getText());

calc2.text3.setText("" + (num1 + num2));

calc2.text1.setText("");
calc2.text2.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
package cn.linsip.lesson03;

import java.awt.*;

public class TestPaint {
public static void main(String[] args) {

new Paint().loadFrame();
}
}

class Paint extends Frame {
public void loadFrame() {
setBounds(200, 200, 400, 400);
setVisible(true);
}

@Override
public void paint(Graphics g) {
//g.setColor(Color.ORANGE);
g.drawOval(100, 100, 200, 200);
g.fillOval(100, 100, 200, 200);
//还原画笔默认颜色
}
}

鼠标监听

目的:想要实现鼠标画画

思路:image-20210506113625442

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
package cn.linsip.lesson03;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;

/*
鼠标监听事件
*/
public class TestMouseListener {
public static void main(String[] args) {
new Myframe("画图");
}
}

class Myframe extends Frame {

private ArrayList points;
//画画需要画笔,需要监听鼠标位置,需要用集合存储


public Myframe(String title) {
super(title);
setBounds(100, 100, 400, 400);
//存鼠标点击的位置
points = new ArrayList<>();
setVisible(true);
//鼠标监听器针对这个窗口
addMouseListener(new MyMouseListener());
}

@Override
public void paint(Graphics g) {
//画画,监听鼠标的事件
Iterator iterator = points.iterator();
while (iterator.hasNext()) {
Point point = (Point) iterator.next();
g.setColor(Color.BLUE);
g.fillOval(point.x, point.y, 10, 10);
}
}

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

}

//适配器模式
private class MyMouseListener extends MouseAdapter {
//鼠标 按下 弹起 按住不放
@Override
public void mousePressed(MouseEvent e) {

Myframe myframe = (Myframe) e.getSource();
//我们点击的时候就会产生一个点
//这个点就是鼠标点击的点
myframe.addPoint(new Point(e.getX(), e.getY()));
//每次点击鼠标都要重画一次
myframe.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
32
33
34
35
36
37
package cn.linsip.lesson03;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBackground(Color.BLUE);
setBounds(100,100,400,400);
setVisible(true);

this.addWindowListener(
new WindowAdapter() {
//关闭窗口
@Override
public void windowClosing(WindowEvent e) {
System.out.println("正在关闭");
System.exit(0);
}

//激活窗口
@Override
public void windowActivated(WindowEvent e) {
System.out.println("已进入当前窗口");
}

}
);
}

}

键盘监听

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
package cn.linsip.lesson03;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class TestKeyListener {
public static void main(String[] args) {
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
setBounds(0, 0,400,400);
setBackground(Color.yellow);
setVisible(true);

this.addKeyListener(new KeyAdapter() {
//键盘按压
@Override
public void keyPressed(KeyEvent e) {
//获得按压的键
int keyCode = e.getKeyCode();
System.out.println(keyCode);
if (keyCode == KeyEvent.VK_UP) {
System.out.println("UP");
}
}
});
}
}

Swing

窗口、面板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package cn.linsip.lesson04;

import javax.swing.*;
import java.awt.*;

public class JFrameDemo01 {
public void init(){
JFrame jFrame = new JFrame("第一个JFrame窗口");
jFrame.setBounds(0,0,400,400);
jFrame.setVisible(true);
JLabel jLabel = new JLabel("我的第一个JFrame窗口",SwingConstants.CENTER);//建一个label放在容器中间
jFrame.add(jLabel);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//添加窗口关闭,默认隐藏
jFrame.getContentPane().setBackground(Color.cyan);
}

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

弹窗

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
package cn.linsip.lesson04;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DialogDemo01 extends JFrame {
public DialogDemo01() {
this.setBounds(0, 0, 200, 200);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

Container contentPane = this.getContentPane();
contentPane.setLayout(null);

JButton jButton = new JButton("弹窗");
jButton.setBounds(10, 10, 80, 50);

jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new MyDialog();
}
});
contentPane.add(jButton);

}

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

class MyDialog extends JDialog {
public MyDialog() {
this.setVisible(true);
this.setBounds(100, 100, 200, 200);

Container contentPane = this.getContentPane();
//contentPane.setLayout(null);

contentPane.add(new JLabel("弹窗警告"));

}
}

标签

构造方法

1
new JLabel("xxx")

Icon

如何画一个标签

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
package cn.linsip.lesson04;

import javax.swing.*;
import java.awt.*;

/*
图标,继承Frame类,实现Icon接口

*/
public class IconDemo01 extends JFrame implements Icon {
//属性:宽高
private int width;
private int height;

//构造器
public IconDemo01() {
}

public IconDemo01(int width, int height) {
this.width = width;
this.height = height;
}
//初始化方法
public void init() {
setBounds(0, 0, 800, 600);
//图标放在标签上
IconDemo01 iconDemo01 = new IconDemo01(15,15);
JLabel jLabel = new JLabel("图标", iconDemo01 , SwingConstants.CENTER);
Container container = getContentPane();
container.add(jLabel);

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

}

//main方法
public static void main(String[] args) {
//通过调用new IconDemo01()的init()方法来
new IconDemo01().init();
}
//重写接口Icon的方法
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
//画图标,画一个圆
g.fillOval(x, y, width, height);
}

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

@Override
public int getIconHeight() {
return height;
}
}

图片标签ImageIcon

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
package cn.linsip.lesson04;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class ImageIconDemo extends JFrame {

public ImageIconDemo() {
//获取图片的地址
URL url = ImageIconDemo.class.getResource("Hush a by little girl.jpg");
ImageIcon imageIcon = new ImageIcon(url);

//new JLabel标签
JLabel jLabel = new JLabel();
jLabel.setIcon(imageIcon);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
//把JLabel标签放面板上
Container container = getContentPane();
container.add(jLabel);


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

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

}

面板

JPanel

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
package cn.linsip.lesson05;

import javax.swing.*;
import java.awt.*;

public class JPanelDemo extends JFrame {

public JPanelDemo() {
//容器
Container container = getContentPane();
container.setLayout(new GridLayout(2,2, 10, 10));
//面板
JPanel jPanel1 = new JPanel(new GridLayout(1, 3));
JPanel jPanel2 = new JPanel(new GridLayout(1, 2));
JPanel jPanel3 = new JPanel(new GridLayout(2, 1));
JPanel jPanel4 = new JPanel(new GridLayout(3, 2));
//添加jButton按钮到JPanel面板
jPanel1.add(new JButton("1"));
jPanel1.add(new JButton("1"));
jPanel1.add(new JButton("1"));
jPanel2.add(new JButton("2"));
jPanel2.add(new JButton("2"));
jPanel3.add(new JButton("3"));
jPanel3.add(new JButton("3"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
jPanel4.add(new JButton("4"));
//添加jPanel面板到容器中
container.add(jPanel1);
container.add(jPanel2);
container.add(jPanel3);
container.add(jPanel4);

setBounds(0, 0, 400, 400);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public static void main(String[] args) {

new JPanelDemo();
}
}

JScroll面板

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
package cn.linsip.lesson01;


import javax.swing.*;
import java.awt.*;

public class JScrollDemo extends JFrame {

public JScrollDemo() {

Container container = getContentPane();
//文本域
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("Hello World!");

JScrollPane jScrollPane = new JScrollPane(jTextArea);
container.add(jScrollPane);


setBounds(0, 0, 400, 400);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

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

按钮

图片按钮

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
package cn.linsip.lesson05;

import javax.swing.*;
import java.awt.*;
import java.net.URL;


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

URL resource = JButtonDemo01.class.getResource("wechat.jpg");

Icon icon = new ImageIcon(resource);

JButton jButton = new JButton();
jButton.setIcon(icon);
jButton.setToolTipText("图片按钮");

container.add(jButton);

setVisible(true);
setSize(400,400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

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

单选按钮

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
package cn.linsip.lesson05;

import javax.swing.*;
import java.awt.*;


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

//URL resource = JButtonDemo01.class.getResource("wechat.jpg");

//Icon icon = new ImageIcon(resource);

JRadioButton jRadioButton1 = new JRadioButton("jRB1");
JRadioButton jRadioButton2 = new JRadioButton("jRB2");
JRadioButton jRadioButton3 = new JRadioButton("jRB3");

ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
buttonGroup.add(jRadioButton3);

container.add(jRadioButton1,BorderLayout.CENTER);
container.add(jRadioButton2,BorderLayout.NORTH);
container.add(jRadioButton3,BorderLayout.SOUTH);

setSize(400,400);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

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

复选按钮

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
package cn.linsip.lesson05;

import javax.swing.*;
import java.awt.*;

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

JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1");
JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2");
JCheckBox jCheckBox3 = new JCheckBox("jCheckBox3");

container.add(jCheckBox1,BorderLayout.NORTH);
container.add(jCheckBox2,BorderLayout.CENTER);
container.add(jCheckBox3,BorderLayout.SOUTH);


setSize(400,400);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

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

列表

下拉框

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
package cn.linsip.lesson06;

import javax.swing.*;
import java.awt.*;

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

JComboBox jComboBox = new JComboBox();

jComboBox.addItem("1");
jComboBox.addItem("2");
jComboBox.addItem("3");
jComboBox.addItem(null);

container.add(jComboBox);
setSize(100,100);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public static void main(String[] args) {

new TestComboboxDemo01();
}
}

列表框

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
package cn.linsip.lesson06;

import javax.swing.*;
import java.awt.*;
import java.util.Vector;

public class TestComboboxDemo02 extends JFrame {
public TestComboboxDemo02() {
Container container = getContentPane();
//生成列表内容
//String[] strings = {1 ,2 , 3};

Vector contents = new Vector();

JList jList = new JList(contents);
contents.add("111");
contents.add("222");
contents.add("333");

container.add(jList);

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

public static void main(String[] args) {

new TestComboboxDemo02();
}
}


应用场景

  • 选择地区,一般是单个选项
  • 列表展示信息,一般是动态扩容

文本框

文本框

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
package cn.linsip.lesson06;

import javax.swing.*;
import java.awt.*;


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

JTextField jTextField1 = new JTextField("jTextField1");
JTextField jTextField2 = new JTextField("jTextField2");

container.add(jTextField1, BorderLayout.NORTH);
container.add(jTextField2, BorderLayout.SOUTH);


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

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

密码框

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
package cn.linsip.lesson06;

import javax.swing.*;
import java.awt.*;


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

JPasswordField jPasswordField = new JPasswordField();
jPasswordField.setEchoChar('*');

container.add(jPasswordField);

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


public static void main(String[] args) {

new TestTextDemo02();
}
}

文本域

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
package cn.linsip.lesson06;

import javax.swing.*;
import java.awt.*;

public class TestTextDemo03 extends JFrame {

public TestTextDemo03() {

Container container = getContentPane();

JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("Hello World!");

JScrollPane jScrollPane = new JScrollPane(jTextArea);
container.add(jScrollPane);

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

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


博主码字不易,本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!