简述Java图形用户界面设计SwingWord格式.docx
《简述Java图形用户界面设计SwingWord格式.docx》由会员分享,可在线阅读,更多相关《简述Java图形用户界面设计SwingWord格式.docx(22页珍藏版)》请在冰豆网上搜索。
8
9importjavax.swing.JButton;
10importjavax.swing.JDesktopPane;
11importjavax.swing.JFrame;
12importjavax.swing.JInternalFrame;
13importjavax.swing.JPanel;
14
15publicfinalclassAwtSwing{
16
17publicstaticvoidmain(String[]args){
18AwtSwingas=newAwtSwing();
19as.show();
20}
21
22JFrameframe=newJFrame("
TestAWTandSWING"
);
23
24JDesktopPanejdp=newJDesktopPane();
25
26JInternalFramejif1=newJInternalFrame("
controls"
27
28JInternalFramejif2=newJInternalFrame("
cover"
29
30publicAwtSwing(){
31frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
32frame.getContentPane().add(jdp);
33
34jif1.setContentPane(newJPanel());
35jif2.setContentPane(newJPanel());
36
37jif1.getContentPane().setLayout(newBorderLayout());
38jif1.getContentPane().add(newButton("
AWTButton"
),BorderLayout.WEST);
39jif1.getContentPane().add(newJButton("
SwingButton"
),
40BorderLayout.EAST);
41
42jif1.setSize(200,100);
43jif2.setSize(200,100);
44
45jdp.add(jif1);
46jdp.add(jif2);
47
48frame.setSize(240,140);
49}
50
51publicvoidshow(){
52frame.setVisible(true);
53jif1.setVisible(true);
54jif2.setVisible(true);
55}
56
57}
运行这个程序,并用鼠标拖动那个名为“cover”的子窗口,我们会发现一个非常有趣的现象,如图:
显然cover子窗口是在controls子窗口之上的,但是它只罩盖住了SwingButton,没有罩盖住AWTButton。
再看一会儿,你是不是有这样一种感觉:
SwingButton是“画”上去的,而AWTButton则是“贴”上去的。
这就是二者混用造成层次错乱的一个例子。
Swing组件有美观、易用、组件量大等特点,也有缺点——使用Swing组件的程序通常会比使用AWT组件的程序运行更慢。
但是大家都还是更喜欢用Swing组件,原因何在?
因为随着计算机硬件的升级,一点点速度已经不是问题。
相反的,用户更需要美观的用户界面,开发人员则更需要易用的开发组件。
框架(Frame,JFrame)是Java图形用户界面的基础,它就是我们通常所说的窗口,是Windows/XWindow应用程序的典型特征。
说到Windows/XWindow,大家很容易联想到“事件(Event)驱动”。
Java的图形用户界面正是事件驱动的,并且由各种各样的监听器(Listener)负责捕捉各种事件。
如果我们需要对某一个组件的某种事件进行捕捉和处理时,就需要为其添加监听器。
比如,我们要在一个窗口(JFrame)激活时改变它的标题,我们就需要为这个窗口(JFrame对象)添加一个可以监听到“激活窗口”这一事件的监听器——WindowListener。
怎么添加监听器呢?
这通常由组件类提供的一个addXxxxxListener的方法来完成。
比如JFrame就提供有addWindowListener方法添加窗口监听器(WindowListener)。
一个监听器常常不只监听一个事件,而是可以监听相关的多个事件。
比如WindowListener除了监听窗口激活事件(windowActivate)之外,还可以监听窗口关闭事件(windowClosing)等。
那么这些事件怎么区分呢?
就靠重载监听器类(Class)的多个方法(Method)了。
监听器监听到某个事件后,会自动调用相关的方法。
因此我们只要重载这个方法,就可以处理相应的事件了。
不妨先看一个例子:
58/**
59*@(#)TestFrame.java
60*@authorJames
61*/
62
63importjavax.swing.*;
64importjava.awt.event.*;
65
66publicclassTestFrameextendsJFrame{
67
68privateintcounter=0;
69
70publicTestFrame(){
71/*使用匿名类添加一个窗口监听器*/
72addWindowListener(newWindowAdapter(){
73publicvoidwindowClosing(WindowEvente){
74System.out.println(
75"
ExitwhenClosedevent"
76//退出应用程序
77System.exit(0);
78}
79
80publicvoidwindowActivated(WindowEvente){
81//改变窗口标题
82setTitle("
TestFrame"
+counter++);
83}
84});
85
86//设置窗口为固定大小
87setResizable(false);
88setSize(200,150);
89}
90
91publicstaticvoidmain(String[]args){
92TestFrametf=newTestFrame();
93tf.show();
94}
95
96}
这个例子中,我们设计了一个窗口类(publicclassTestFrameextendsJFrame{...}),并且为这个窗口添加了一个窗口监听器(addWindowListener(newWindowAdapter()...)。
而我们添加的这个窗口监听器主要监听了两个事件:
窗口关闭(publicvoidwindowClosing(WindowEvente)...)和窗口激活(publicvoidwindowActivated(WindowEvente)...)。
在窗口关闭事件中我们退出了整个应用程序(System.exit(0);
),而在窗口激活事件中,我们改变了窗口的标题(setTitle("
)。
最后,我们在main方法中显示了这窗口类的一个实例,运行得到下图所示的结果:
这个程序的运行结果就是一个什么东西都没有加的框架,也就是一个空窗口。
那么,你知道显示一个窗口最主要的几句代码吗?
不知道没关系,我来告诉你,显示一个窗口只需要做三件事:
生成实例(对象)→设置大小→显示,相应的,就是下面的三句代码:
97JFrameframe=newJFrame("
Frame'
sTitle"
98frame.setSize(400,300);
99frame.setVisible(true);
也许你会说:
第一句的意思我清楚,第三句的意思我也明白,为什么一定要第二句呢?
其实想想也就明白了,叫你画一个没法有大小的矩形你能画出来吗?
不能。
同样,没有大小的窗口,怎么显示?
所以我们需要用setSize(intwidth,intheight)方法为其设置大小。
我们还有另一种方法:
用JFrame的pack()方法让它自己适配一个大小。
pack()在多数时候是令人满意的,但有时,它也会让你哭笑不得——多试试就知道了。
在JFrame中,我们使用addWindowListener方法加入一个监听器WindowListener(addWindowListener(newWindowAdapter()...)去监听发生在JFrame上的窗口事件。
WindowListener是一个接口,在java.awt.event这个包中,但是上例中好象并没有使用WindowListener,而是使用的WindowsAdapter吧,这是怎么回事?
WindowAdapter是WindowsListener接口的一个最简单的实现,也在java.awt.event包中。
如果我们直接使用WindowListener产生一个类,需要实现它的每一个方法(一共7个)。
但WindowAdapter作为WindowListener最简单的实现,已经实现了它的每一个方法为空方法(即只包含空语句,或者说没有语句的方法)。
用WindowAdapter就只需要重载可能用到的方法(上例中只有2个)就行了,而不需要再去实现每一个方法。
优点显而易见——减少编码量。
在JFrame上发生的窗口事件(WindowEvent)包括:
windowActivated(WindowEvente)
窗口得到焦点时触发
windowClosed(WindowEvente)
窗口关闭之后触发
windowClosing(WindowEvente)
窗口关闭时触发
windowDeactivated(WindowEvente)
窗口失去焦点时触发
windowDeiconified(WindowEvente)
windowIconified(WindowEvente)
windowOpened(WindowEvente)
窗口打开之后触发
上例重载了其中两个方法。
如果在上例运行产生的窗口和另外一个应用程序窗口之间来回切换(在Windows操作系统中你可以使用Alt+Tab进行切换)……试试看,你发现了什么?
有没有现我们的示例窗口标题上的数字一直在增加,这便是在windowActivated事件中setTitle("
+counter++)的功劳。
而另一个事件处理函数windowClosing中的System.exit(0)则保证了当窗口被关闭时退出当前的Java应用程序。
如果不作这样的处理会怎样呢?
试验之后你会发现,窗口虽然关闭了,但程序并没有结束,但此时,除了使用Ctrl+C强行结束之外,恐怕也没有其它办法了。
所以,这一点非常重要:
你想在关闭窗口的时候退出应用程序,那就需要处理windowClosing事件。
……也不尽然,其实还有另外一个更简单的办法,让JFrame自己处理这件事——你只需要如下调用JFrame的setDefaultCloseOperation即可:
100frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
在产生JFrame对象之后执行上述语句,就可以不用处理windowsClosing事件来退出程序了。
我们可以在JFrame对象中添加AWT或者Swing组件。
但是,虽然它有add方法,却不能直接用于添加组件,否则崤壮鲆斐!
恍啪褪允浴T斐烧飧鱿窒蟮脑蛑挥幸桓鼋馐停篔Frame不是一个容器,它只是一个框架。
那么,应该怎么添加组件呢?
JFrame有一个ContentPane,窗口是显示的所有组件都是添加在这个ContentPane中。
JFrame提供了两个方法:
getContentPane和setContentPane就是用于获取和设置其ContentPane的。
通常我们不需要重新设置JFrame的ContentPane,只需要直接获取默认的ContentPane来添加组件等。
如:
101(newJFrame()).getContentPane().add(newButton("
testbutton"
))
按钮……就是按钮,不会连按钮都不知道吧?
切换按钮,有两种状态的按钮,即按下状态和弹起状态,若称为选择状态或未选择状态。
复选按钮,又叫复选框,用一个小方框中是否打勾来表示两种状态。
单选按钮,又叫收音机按钮,以小圆框打点表示被选中。
常成组出现,一组单选按钮中只有一个能被选中。
发现什么了吗?
——对了,这一部分是在讲各种各样的按钮,而且后三种按钮都有两种状态。
先看看这些按钮都长成什么样:
上图中,从上到下,依次就是按钮、切换按钮、复选按钮和单选按钮。
图示的窗口,就是下面这个例子的运行结果:
102/*
103*TestButtons.java
104*@authorFancy
105*/
106
107importjava.awt.event.ActionEvent;
108importjava.awt.event.ActionListener;
109importjava.awt.event.ItemEvent;
110importjava.awt.event.ItemListener;
111
112importjavax.swing.ButtonGroup;
113importjavax.swing.JButton;
114importjavax.swing.JCheckBox;
115importjavax.swing.JFrame;
116importjavax.swing.JLabel;
117importjavax.swing.JRadioButton;
118importjavax.swing.JToggleButton;
119
120publicfinalclassTestButtons{
121
122publicstaticvoidmain(String[]args){
123TestButtonstb=newTestButtons();
124tb.show();
125}
126
127JFrameframe=newJFrame("
TestButtons"
128
129JButtonjButton=newJButton("
JButton"
//按钮
130
131JToggleButtontoggle=newJToggleButton("
ToggleButton"
//切换按钮
132
133JCheckBoxcheckBox=newJCheckBox("
CheckBox"
//复选按钮
134
135JRadioButtonradio1=newJRadioButton("
RadioButton1"
//单选按钮
136
137JRadioButtonradio2=newJRadioButton("
RadioButton2"
138
139JRadioButtonradio3=newJRadioButton("
RadioButton3"
140
141JLabellabel=newJLabel("
HereisStatus,lookhere."
//不是按钮,是静态文本
142
143publicTestButtons(){
144frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
145frame.getContentPane().setLayout(newjava.awt.FlowLayout());
146
147//为一般按钮添加动作监听器
148jButton.addActionListener(newActionListener(){
149publicvoidactionPerformed(ActionEventae){
150label.setText("
YouclickedjButton"
151}
152});
153
154//为切换按钮添加动作监听器
155toggle.addActionListener(newActionListener(){
156publicvoidactionPerformed(ActionEventae){
157JToggleButtontoggle=(JToggleButton)ae.getSource();
158if(toggle.isSelected()){
159label.setText("
YouselectedToggleButton"
160}else{
161label.setText("
YoudeselectedToggleButton"
162}
163}
164});
165
166//为复选按钮添加条目监听器
167checkBox.addItemListener(newItemListener(){
168publicvoiditemStateChanged(ItemEvente){
169JCheckBoxcb=(JCheckBox)e.getSource();
170label.setText("
SelectedCheckBoxis"
+cb.isSelected());
171}
172});
173
174//用一个按钮组对象包容一组单选按钮
175ButtonGroupgroup=newButtonGroup();
176//生成一个新的动作监听器对象,备用
177ActionListeneral=newActionListener(){
178publicvoidactionPerformed(ActionEventae){
179JRadioButtonradio=(JRadioButton)ae.getSource();
180if(radio==radio1){
181label.setText("
YouselectedRadioButton1"
182}elseif(radio==radio2){
183label.setText("
YouselectedRadioButton2"
184}else{
185label.setText("
YouselectedRadioButton3"
186}
187}
188};
189//为各单选按钮添加动作监听器
190radio1.addActionListener(al);
191radio2.addActionListener(al);
192radio3.addActionListener(al);
193//将单选按钮添