Chapter 10 Java GUI Programming (Part 1) - AWT
1. Basic concepts
2. Overview of AWT inheritance system
3. Basic use of container components
1. Commonly used methods
2. Display the first window (Frame)
package com. awt. java ;
import org. testng. annotations. Test ;
import java. awt. * ;
public class awt_Test {
@Test
public void windowTest ( ) {
Frame frame = new Frame ( "第一个窗口" ) ;
frame. setBounds ( 200 , 200 , 450 , 300 ) ;
frame. setBackground ( new Color ( 2 , 2 , 2 ) ) ;
frame. setVisible ( true ) ;
while ( true ) ;
}
}
3. Display the second window (Panel)
package com. awt. java ;
import org. testng. annotations. Test ;
import java. awt. * ;
public class awt_Test {
@Test
public void panellTest ( ) {
Frame frame = new Frame ( "第二个窗口" ) ;
Panel panel = new Panel ( ) ;
panel. add ( new TextField ( "文本框" ) ) ;
panel. add ( new Button ( "按钮" ) ) ;
frame. add ( panel) ;
frame. setBounds ( 200 , 200 , 350 , 300 ) ;
frame. setVisible ( true ) ;
while ( true ) ;
}
}
4. Display the third window (ScrollPane)
package com. awt. java ;
import org. testng. annotations. Test ;
import java. awt. * ;
public class awt_Test {
@Test
public void scrollPaneTest ( ) {
Frame frame = new Frame ( "第三个窗口" ) ;
ScrollPane scrollPane = new ScrollPane ( ScrollPane . SCROLLBARS_ALWAYS ) ;
scrollPane. add ( new TextField ( "文本框" ) ) ;
scrollPane. add ( new Button ( "按钮" ) ) ;
frame. add ( scrollPane) ;
frame. setBounds ( 200 , 200 , 350 , 300 ) ;
frame. setVisible ( true ) ;
while ( true ) ;
}
}
4. Layout Manager - LayoutManager
1. Flow layout - FlowLayout
package com. awt. java ;
import org. testng. annotations. Test ;
import java. awt. * ;
public class awt_Test {
@Test
public void FlowLayoutTest ( ) {
Frame frame = new Frame ( "测试FlowLayout" ) ;
Panel panel = new Panel ( new FlowLayout ( FlowLayout . LEFT , 20 , 20 ) ) ;
for ( int i = 0 ; i < 101 ; i++ ) {
panel. add ( new Button ( "按钮" + i) ) ;
}
frame. add ( panel) ;
frame. setBounds ( 300 , 200 , 850 , 500 ) ;
frame. setVisible ( true ) ;
while ( true ) ;
}
}
2. Border layout - BorderLayout
package com. awt. java ;
import org. testng. annotations. Test ;
import java. awt. * ;
public class awt_Test {
@Test
public void BorderLayoutTest ( ) {
Frame frame = new Frame ( "测试BorderLayout" ) ;
frame. setLayout ( new BorderLayout ( 10 , 10 ) ) ;
frame. add ( new Button ( "按钮北" ) , BorderLayout . NORTH ) ;
frame. add ( new Button ( "按钮南" ) , BorderLayout . SOUTH ) ;
frame. add ( new Button ( "按钮西" ) , BorderLayout . WEST ) ;
frame. add ( new Button ( "按钮东" ) , BorderLayout . EAST ) ;
frame. add ( new Button ( "按钮中" ) , BorderLayout . CENTER ) ;
frame. pack ( ) ;
frame. setVisible ( true ) ;
while ( true ) ;
}
}
3. Grid layout - GridBagLayout
package com. awt. java ;
import org. testng. annotations. Test ;
import javax. swing. * ;
import java. awt. * ;
public class awt_Test {
@Test
public void GridLayoutTest ( ) {
Frame frame = new Frame ( "计算器" ) ;
frame. add ( new TextField ( 30 ) , BorderLayout . NORTH ) ;
Panel panel = new Panel ( new GridLayout ( 3 , 5 , 5 , 5 ) ) ;
for ( int i = 0 ; i < 10 ; i++ ) {
panel. add ( new Button ( i+ "" ) ) ;
}
panel. add ( new Button ( "+" ) ) ;
panel. add ( new Button ( "-" ) ) ;
panel. add ( new Button ( "*" ) ) ;
panel. add ( new Button ( "/" ) ) ;
panel. add ( new Button ( "=" ) ) ;
frame. add ( panel) ;
frame. pack ( ) ;
frame. setVisible ( true ) ;
while ( true ) ;
}
}
4. Card layout - CardLayout
package com. awt. java ;
import org. testng. annotations. Test ;
import javax. swing. * ;
import java. awt. * ;
import java. awt. event. ActionEvent ;
import java. awt. event. ActionListener ;
public class awt_Test {
@Test
public void CardLayout ( ) {
Frame frame = new Frame ( "测试CardLayout卡片布局" ) ;
Panel p1 = new Panel ( ) ;
Panel p2 = new Panel ( ) ;
CardLayout cardLayout = new CardLayout ( ) ;
p1. setLayout ( cardLayout) ;
for ( int i = 0 ; i < 5 ; i++ ) {
p1. add ( "第" + ( i+ 1 ) + "张" , new Button ( "第" + ( i+ 1 ) + "张" ) ) ;
}
Button b1 = new Button ( "上一张" ) ;
Button b2 = new Button ( "下一张" ) ;
Button b3 = new Button ( "第一张" ) ;
Button b4 = new Button ( "最后一张" ) ;
Button b5 = new Button ( "第三张" ) ;
p2. add ( b1) ;
p2. add ( b2) ;
p2. add ( b3) ;
p2. add ( b4) ;
p2. add ( b5) ;
frame. add ( p1) ;
frame. add ( p2, BorderLayout . SOUTH ) ;
ActionListener listener = new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
String actionCommand = e. getActionCommand ( ) ;
switch ( actionCommand) {
case "上一张" :
cardLayout. previous ( p1) ;
break ;
case "下一张" :
cardLayout. next ( p1) ;
break ;
case "第一张" :
cardLayout. first ( p1) ;
break ;
case "最后一张" :
cardLayout. last ( p1) ;
break ;
case "第三张" :
cardLayout. show ( p1, "第3张" ) ;
break ;
}
}
} ;
b1. addActionListener ( listener) ;
b2. addActionListener ( listener) ;
b3. addActionListener ( listener) ;
b4. addActionListener ( listener) ;
b5. addActionListener ( listener) ;
frame. pack ( ) ;
frame. setVisible ( true ) ;
while ( true ) ;
}
}
5. Box layout - BoxLayout
package com. awt. java ;
import org. testng. annotations. Test ;
import javax. swing. * ;
import java. awt. * ;
import java. awt. event. ActionEvent ;
import java. awt. event. ActionListener ;
public class awt_Test {
@Test
public void BoxLayoutTest1 ( ) {
Frame frame = new Frame ( "测试BoxLayout" ) ;
BoxLayout boxLayout = new BoxLayout ( frame, BoxLayout . Y_AXIS ) ;
frame. add ( new Button ( "按钮1" ) ) ;
frame. add ( new Button ( "按钮2" ) ) ;
frame. setLayout ( boxLayout) ;
frame. setBounds ( 220 , 120 , 400 , 240 ) ;
frame. setVisible ( true ) ;
while ( true ) ;
}
@Test
public void BoxTest2 ( ) {
Frame frame = new Frame ( "测试容器Box" ) ;
Box box1 = new Box ( BoxLayout . Y_AXIS ) ;
Box box2 = new Box ( BoxLayout . X_AXIS ) ;
box1. add ( new Button ( "垂直按钮1" ) ) ;
box1. add ( new Button ( "垂直按钮2" ) ) ;
box2. add ( new Button ( "水平按钮1" ) ) ;
box2. add ( new Button ( "水平按钮2" ) ) ;
frame. add ( box1, BorderLayout . SOUTH ) ;
frame. add ( box2) ;
frame. setBounds ( 220 , 120 , 400 , 240 ) ;
frame. setVisible ( true ) ;
while ( true ) ;
}
@Test
public void BoxTest3 ( ) {
Frame frame = new Frame ( "测试Box的分割" ) ;
Box box1 = new Box ( BoxLayout . X_AXIS ) ;
Box box2 = new Box ( BoxLayout . Y_AXIS ) ;
box1. add ( new Button ( "水平按钮1" ) ) ;
box1. add ( Box . createHorizontalGlue ( ) ) ;
box1. add ( new Button ( "水平按钮2" ) ) ;
box1. add ( Box . createHorizontalStrut ( 20 ) ) ;
box1. add ( new Button ( "水平按钮3" ) ) ;
box2. add ( new Button ( "垂直按钮1" ) ) ;
box2. add ( Box . createVerticalGlue ( ) ) ;
box2. add ( new Button ( "垂直按钮2" ) ) ;
box2. add ( Box . createVerticalStrut ( 20 ) ) ;
box2. add ( new Button ( "垂直按钮3" ) ) ;
frame. add ( box1, BorderLayout . NORTH ) ;
frame. add ( box2) ;
frame. setBounds ( 220 , 120 , 400 , 240 ) ;
frame. setVisible ( true ) ;
while ( true ) ;
}
}
五、AWT组件的使用
1、基本组件的使用
组件名
组件功能
Button
按钮
Frame
窗口
Panel
面板,只能放置在其他容器中(如:Frame),不能单独存在
Scrollbar
滚动条
TextArea
多行文本域
TextFirld
单行文本框
Label
标签类,用于放置提示性文本
Canvas
用于绘图的画布
Checkbox
复选框组件(也可以当做单选框组件)
CheckboxGroup
多个Checkbox组成一组CheckboxGroup
Choice
下拉选择框
List
列表框,可添加多条条目
package com. awt. java ;
import javax. swing. * ;
import java. awt. * ;
public class ComponentTest {
Frame frame = new Frame ( "测试基本组件" ) ;
Box box1 = new Box ( BoxLayout . X_AXIS ) ;
Box box2 = new Box ( BoxLayout . X_AXIS ) ;
Box box3 = new Box ( BoxLayout . Y_AXIS ) ;
Box box4 = new Box ( BoxLayout . X_AXIS ) ;
TextArea textArea = new TextArea ( 5 , 12 ) ;
TextField textField = new TextField ( 30 ) ;
Choice choice = new Choice ( ) ;
CheckboxGroup checkboxGroup = new CheckboxGroup ( ) ;
Checkbox checkbox1 = new Checkbox ( "男" , checkboxGroup, true ) ;
Checkbox checkbox2 = new Checkbox ( "女" , checkboxGroup, false ) ;
Checkbox checkbox3 = new Checkbox ( "是否已婚?" ) ;
Button button = new Button ( "确认" ) ;
List list = new List ( 5 , false ) ;
public void Init ( ) {
box1. add ( textField) ;
box1. add ( button) ;
frame. add ( box1, BorderLayout . SOUTH ) ;
choice. add ( "红色" ) ;
choice. add ( "绿色" ) ;
choice. add ( "黄色" ) ;
box2. add ( choice) ;
box2. add ( checkbox1) ;
box2. add ( checkbox2) ;
box2. add ( checkbox3) ;
box3. add ( textArea) ;
box3. add ( box2) ;
list. add ( "张三" ) ;
list. add ( "李四" ) ;
list. add ( "王五" ) ;
box4. add ( box3) ;
box4. add ( list) ;
frame. add ( box4, BorderLayout . NORTH ) ;
frame. pack ( ) ;
frame. setVisible ( true ) ;
}
public static void main ( String [ ] args) {
new ComponentTest ( ) . Init ( ) ;
}
}
2、普通对话框Dialog的使用
package com. awt. java ;
import java. awt. * ;
import java. awt. event. ActionEvent ;
import java. awt. event. ActionListener ;
public class DialogTest {
public static void main ( String [ ] args) {
Frame frame = new Frame ( "测试对话框Dialog" ) ;
Button button1 = new Button ( "打开模式对话框" ) ;
Button button2 = new Button ( "打开非模式对话框" ) ;
Dialog nonModalDailog = new Dialog ( frame, "模式对话框" , false ) ;
Dialog modalDailog = new Dialog ( frame, "模式对话框" , true ) ;
frame. add ( button1, BorderLayout . NORTH ) ;
frame. add ( button2, BorderLayout . SOUTH ) ;
nonModalDailog. setBounds ( 20 , 30 , 300 , 150 ) ;
modalDailog. setBounds ( 20 , 30 , 300 , 150 ) ;
button1. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
modalDailog. setVisible ( true ) ;
}
} ) ;
button2. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
nonModalDailog. setVisible ( true ) ;
}
} ) ;
frame. pack ( ) ;
frame. setVisible ( true ) ;
}
}
3、文件对话框FileDialog的使用
package com. awt. java ;
import javax. swing. * ;
import java. awt. * ;
import java. awt. event. ActionEvent ;
import java. awt. event. ActionListener ;
public class FileDialogTest {
public static void main ( String [ ] args) {
Frame frame = new Frame ( "测试文件对话框" ) ;
Box box = new Box ( BoxLayout . Y_AXIS ) ;
Button btnOpen = new Button ( "打开文件" ) ;
Button btnSave = new Button ( "保存文件" ) ;
box. add ( btnOpen) ;
box. add ( btnSave) ;
frame. add ( box) ;
FileDialog fileOpen = new FileDialog ( frame, "打开文件" , FileDialog . LOAD ) ;
FileDialog fileSave = new FileDialog ( frame, "保存文件" , FileDialog . SAVE ) ;
btnOpen. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
fileOpen. setVisible ( true ) ;
System . out. println ( fileOpen. getDirectory ( ) + fileOpen. getFile ( ) ) ;
}
} ) ;
btnSave. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
fileSave. setVisible ( true ) ;
System . out. println ( fileSave. getDirectory ( ) + fileSave. getFile ( ) ) ;
}
} ) ;
frame. pack ( ) ;
frame. setVisible ( true ) ;
}
}
4、事件处理机制
(1)基本概念和使用
package com. awt. java ;
import javax. swing. * ;
import java. awt. * ;
import java. awt. event. ActionEvent ;
import java. awt. event. ActionListener ;
public class EventHandingTest {
private static class MyListener implements ActionListener {
private TextField tf;
public MyListener ( TextField tf) {
this . tf = tf;
}
@Override
public void actionPerformed ( ActionEvent e) {
tf. setText ( "Hello World" ) ;
}
}
public static void main ( String [ ] args) {
TextField tf = new TextField ( ) ;
Button btn = new Button ( "确认" ) ;
Frame frame = new Frame ( "测试事件处理" ) ;
frame. add ( tf, BorderLayout . NORTH ) ;
frame. add ( btn, BorderLayout . SOUTH ) ;
MyListener myListener = new MyListener ( tf) ;
btn. addActionListener ( myListener) ;
frame. pack ( ) ;
frame. setVisible ( true ) ;
}
}
(2) Commonly used events
low level incident
listener
Trigger mode
ComponentEvent
ComponentListener
Component size, position or show/hide state changes
ContainerEvent
ContainerListener
Triggered when a component is added or removed from the container
WindowEvent
WindowListener
When the window state changes (open, close, maximize, minimize)
FocusEvent
FocusListener
Component gets focus or loses focus
KeyEvent
KeyListener
Triggered when a keyboard key is pressed, released, or clicked
MouseEvent
MouseListener
Triggered when mouse clicks, moves, etc.
PaintEvent
PaintListener
Component drawing events, special event types (*)
Advanced events
listener
Trigger mode
ActionEvent
ActionListener
Action event, when a button or menu item is clicked or the Enter key is pressed in a text box
AdjustmentEvent
AdjustmentListener
Adjustment event, triggered when the slider is moved on the slider to adjust the value
ItemEvent
ItemListener
Option event, triggered when an item is selected or unchecked
TextEvent
TextListener
Text events, when changes occur in text boxes and text fields
(3)Use cases
package com. awt. java ;
import javax. swing. * ;
import java. awt. * ;
import java. awt. event. * ;
public class ListenerTest {
private Frame f = new Frame ( "测试监听器" ) ;
private Box box = new Box ( BoxLayout . X_AXIS ) ;
private Choice choice = new Choice ( ) ;
private TextField tf = new TextField ( 25 ) ;
public class ColorListener implements ItemListener {
@Override
public void itemStateChanged ( ItemEvent e) {
System . out. println ( "当前选中的是:" + e. getItem ( ) ) ;
}
}
public void init ( ) {
choice. add ( "红色" ) ;
choice. add ( "绿色" ) ;
choice. add ( "黄色" ) ;
box. add ( choice) ;
box. add ( tf) ;
f. add ( box) ;
ColorListener colorListener = new ColorListener ( ) ;
choice. addItemListener ( colorListener) ;
tf. addTextListener ( new TextListener ( ) {
@Override
public void textValueChanged ( TextEvent e) {
System . out. println ( "当前文本框内容是:" + tf. getText ( ) ) ;
}
} ) ;
f. addWindowListener ( new WindowAdapter ( ) {
@Override
public void windowClosing ( WindowEvent e) {
System . exit ( 0 ) ;
}
} ) ;
f. pack ( ) ;
f. setVisible ( true ) ;
}
public static void main ( String [ ] args) {
new ListenerTest ( ) . init ( ) ;
}
}
5. Use of menu components
(1) Common menu component system
(2)Use cases
package com. awt. java ;
import java. awt. * ;
import java. awt. event. KeyEvent ;
public class MenuCompTest {
public static void main ( String [ ] args) {
Frame f = new Frame ( "测试菜单" ) ;
TextArea ta = new TextArea ( 15 , 60 ) ;
f. add ( ta, BorderLayout . SOUTH ) ;
MenuBar menuBar = new MenuBar ( ) ;
Menu fileMenu = new Menu ( "文件" ) ;
Menu editMenu = new Menu ( "编辑" ) ;
Menu formatMenu = new Menu ( "格式" ) ;
CheckboxMenuItem autoMenu = new CheckboxMenuItem ( "自动换行" ) ;
MenuItem copyMenu = new MenuItem ( "复制" ) ;
MenuItem pasteMenu = new MenuItem ( "粘贴" ) ;
MenuItem commentMenu = new MenuItem ( "注释" , new MenuShortcut ( KeyEvent . VK_Q , true ) ) ;
MenuItem cancelCommentMenu = new MenuItem ( "取消注释" ) ;
formatMenu. add ( commentMenu) ;
formatMenu. add ( cancelCommentMenu) ;
editMenu. add ( autoMenu) ;
editMenu. add ( copyMenu) ;
editMenu. add ( pasteMenu) ;
editMenu. add ( new MenuItem ( "-" ) ) ;
editMenu. add ( formatMenu) ;
menuBar. add ( fileMenu) ;
menuBar. add ( editMenu) ;
f. setMenuBar ( menuBar) ;
f. pack ( ) ;
f. setVisible ( true ) ;
}
}
6. AWT drawing (temporarily omitted)