简介

AWT

AWT 是Java基本类库的一部分,它在你的Java程序中提供了图形化用户界面,并且能够接收来自键盘,鼠标和其他输入设备的用户输入。

使用AWT包,则需要import java.awt.;(用于支持GUI的包还有java.swing.。这里直接用java.awt.*导入,其实主要是两个包:java.awt和java.awt.event)

Swing

Swing是AWT的一个扩展。使用Swing包,则需要import Javax.swing.*;

Swing容器是java.awt.Container的子类,包含用于在容器中添加或产出组件、使用布局管理器来排列组件以及设置容器边框的方法。容器通常可放置到其他容器中。对象类层次结构如下

mark

界面介绍

注意窗口和面板的区别

mark

创建窗口

通过AWT

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

class MyFirstFrame extends Frame {
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 200;
private static final int FRAME_X_ORIGIN = 150;
private static final int FRAME_Y_ORIGIN = 250;

public MyFirstFrame() {
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setResizable(false);
setTitle("Program MyFirstFrame");
setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
}
}

class TestMyFirstFrame {
public static void main(String args[]) {
MyFirstFrame frame = new MyFirstFrame();
frame.setVisible(true);
}
}

通过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
import javax.swing.*;

public class SimpleFrame extends JFrame {//继承JFrame框架类
public SimpleFrame() { //创建构造方法
super("Frame Title");
setSize(300, 100); //设置框架大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLookAndFeel();
setVisible(true); //刚创建时框架是不可见的,可使用该方法使框架可见
}

private static void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
);
} catch (Exception exc) {
// ignore error
}
}

public static void main(String[] arguments) {
setLookAndFeel();
SimpleFrame sf = new SimpleFrame();
}
}

添加组件

通过AWT

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 java.awt.*;

class MyFirstFrame extends Frame {
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 200;
private static final int FRAME_X_ORIGIN = 150;
private static final int FRAME_Y_ORIGIN = 250;
private static final int BUTTON_WIDTH = 60;
private static final int BUTTON_HEIGHT = 30;
Button cancelButton, okButton;//添加两个按钮

public MyFirstFrame() {
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setResizable(false);
setTitle("Program MyFirstFrame");
setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
// add ok button
okButton = new Button("OK");
okButton.setBounds(100, 150, BUTTON_WIDTH, BUTTON_HEIGHT);
add(okButton);
// add cancel button
cancelButton = new Button("Cancel");
cancelButton.setBounds(170, 150, BUTTON_WIDTH, BUTTON_HEIGHT);
add(cancelButton);
}
}

class TestMyFirstFrame {
public static void main(String args[]) {
MyFirstFrame frame = new MyFirstFrame();
frame.setVisible(true);
}
}

通过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
29
30
31
32
33
34
import javax.swing.*;

public class ButtonFrame extends JFrame {
JButton load = new JButton("Load");
JButton save = new JButton("Save");
JButton unsubscribe = new JButton("Unsubscribe");

public ButtonFrame() {
super("Button Frame");
setSize(340, 170);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
pane.add(load);
pane.add(save);
pane.add(unsubscribe);
add(pane);
setVisible(true);
}

private static void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
);
} catch (Exception exc) {
System.out.println(exc.getMessage());
}
}

public static void main(String[] arguments) {
setLookAndFeel();
ButtonFrame bf = new ButtonFrame();
}
}

创建一个登陆界面

页面包含组件有:标签(JLabel)、文本框(JTextField、JPasswordField)、文本区域(JTextArea)、按钮(JButton)

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
import javax.swing.*;

public class Authenticator extends javax.swing.JFrame {
JTextField username = new JTextField(15);
JPasswordField password = new JPasswordField(15);
JTextArea comments = new JTextArea(4, 15);
JButton ok = new JButton("OK");
JButton cancel = new JButton("Cancel");

public Authenticator() {
super("Account Information");
setSize(300, 220);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel pane = new JPanel();
JLabel usernameLabel = new JLabel("Username: ");
JLabel passwordLabel = new JLabel("Password: ");
JLabel commentsLabel = new JLabel("Comments: ");
comments.setLineWrap(true);
comments.setWrapStyleWord(true);
pane.add(usernameLabel);
pane.add(username);
pane.add(passwordLabel);
pane.add(password);
pane.add(commentsLabel);
pane.add(comments);
pane.add(ok);
pane.add(cancel);
add(pane);
setVisible(true);
}

private static void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
);
} catch (Exception exc) {
System.out.println(exc.getMessage());
}
}

public static void main(String[] arguments) {
Authenticator.setLookAndFeel();
Authenticator auth = new Authenticator();
}
}

运行结果:

mark

添加单选框或复选框

  • 单选框: JRadioButton
  • 复选框: JCheckBox
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
import javax.swing.*;

public class FormatFrame extends JFrame {
JRadioButton[] teams = new JRadioButton[4];

public FormatFrame() {
super("Choose an Output Format");
setSize(320, 120);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
teams[0] = new JRadioButton("Atom");
teams[1] = new JRadioButton("RSS 0.92");
teams[2] = new JRadioButton("RSS 1.0");
teams[3] = new JRadioButton("RSS 2.0", true);
JPanel panel = new JPanel();
JLabel chooseLabel = new JLabel(
"Choose an output format for syndicated news items.");
panel.add(chooseLabel);
ButtonGroup group = new ButtonGroup();
for (JRadioButton team : teams) {
group.add(team);
panel.add(team);
}
add(panel);
setVisible(true);
}

private static void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
);
} catch (Exception exc) {
System.out.println(exc.getMessage());
}
}

public static void main(String[] arguments) {
FormatFrame.setLookAndFeel();
FormatFrame ff = new FormatFrame();
}
}

运行结果:

mark

事件

当用户移动鼠标、单击按钮时,一个事件就产生了

一个对象既可以是事件源对象,也可以是事件侦听对象,或二种角色兼有

mark

如果界面使用Swing实现,则若要进行界面的事件操作,应该引入三个包:

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

设置组件

将类用作时间监听器是,必须首先设置它要监听的时间类型。如果不进行第二步操作——将匹配的监听器加入到GUI组建中,这种情况将不会发生。组件被使用时,该监听器将激发相应的事件。

创建组件后,可以调用组件的下述方法之一将监听器与组件关联起来。

addActionListener()、addFocusListener()…其实就是在上面所给图中的接口的前面加一个”add”就行了。

事件处理方法

将接口与类关联起来是,这个类必须处理接口包含的所有方法。

以接口ActionListener为例,其只有一个方法actionPerformed()。所有实现ActionListener的类都必须有一个结构与下面类似的方法:

1
2
3
public void actionPerformed(ActionEvent event){
//handle event here
}

事件创建举例

各个事件的操作都差不多,这里只先提供焦点事件操作的过程。其它的时间操作先留个坑,以后再补。

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
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class Calculator extends JFrame implements FocusListener {
JTextField value1, value2, sum;
JLabel plus, equals;

public Calculator() {
super("Add Two Numbers");
setSize(350, 90);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLookAndFeel();
FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
setLayout(flow); //设置框架展示在什么位置
// create components
value1 = new JTextField("0", 5);
plus = new JLabel("+");
value2 = new JTextField("0", 5);
equals = new JLabel("=");
sum = new JTextField("0", 5);
// add listeners
value1.addFocusListener(this);//表示当前类就是事件监听器
value2.addFocusListener(this);
// set up sum field
sum.setEditable(false);
// add components
add(value1);
add(plus);
add(value2);
add(equals);
add(sum);
setVisible(true);
}

public void focusGained(FocusEvent event) {
try {
float total = Float.parseFloat(value1.getText()) +
Float.parseFloat(value2.getText());
sum.setText("" + total);
} catch (NumberFormatException nfe) {
value1.setText("0");
value2.setText("0");
sum.setText("0");
}
}

public void focusLost(FocusEvent event) {
focusGained(event);
}

private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
);
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception exc) {
System.err.println("Couldn't use the system "
+ "look and feel: " + exc);
}
}

public static void main(String[] arguments) {
Calculator frame = new Calculator();
}
}

运行结果:

mark