实验1容器布局管理器和边框Word格式文档下载.docx
- 文档编号:20176744
- 上传时间:2023-01-17
- 格式:DOCX
- 页数:20
- 大小:110.93KB
实验1容器布局管理器和边框Word格式文档下载.docx
《实验1容器布局管理器和边框Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《实验1容器布局管理器和边框Word格式文档下载.docx(20页珍藏版)》请在冰豆网上搜索。
软件:
JDK7.0;
Eclipse
五、知识准备
前期要求掌握的知识:
掌握容器和三种基本的布局管理器。
实验相关理论或原理:
CardLayout布局管理器将组件像卡片一样放置在容器中,每次仅有一张卡片是可见的,容器的作用就像一叠卡片。
这些卡片的顺序是有容器中组件的内部顺序决定的。
可以指定一叠组件周围的水平空隙和垂直空隙的大小。
布局管理器GridBagLayout是最灵活、最复杂的布局管理器。
以网格的形式组织组件,组件可以改变大小,还可以按任意的顺序添加组件。
不使用布局管理器:
Container.setLayout(null);
javax.swing.BoxLayout在一行或一列中摆放组件,构造方法:
publicBoxlayLayout(Container
target,intaxis)。
OverlayLayout将组件相互覆盖叠放到一起,构造方法:
publicOverlayLayout(Container
target)。
JTabbedPane提供一组相互排斥的卡片来访问多个组件,通常在JTabbedPane中放置一些面板,每个面板对应一张卡片。
JSplitPane包含用分隔栏分隔开的两个组件,可以用分隔栏水平或垂直地分隔容器,并且拖动分隔栏可以改变每个组件所占空间的大小。
六、验证性实验
验证性实验1
1.实验要求
使用CardLayout布局管理器编写程序,布局如下图10.1所示界面。
图10.1CardLayout布局管理器
2.程序参考代码
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassShowCardLayoutextendsJApplet
implementsActionListener,ItemListener{
(1)privateCardLayoutcardLayout=newCardLayout();
//定义CardLayout布局管理器
privateJPanelcardPanel=newJPanel(cardLayout);
privateJButtonjbtFirst,jbtNext,jbtPrevious,jbtLast;
privateJComboBoxjcboImage;
privatefinalintNUM_OF_FLAGS=9;
publicShowCardLayout(){
cardPanel.setBorder(
newjavax.swing.border.LineBorder(Color.red));
//Add9labelsfordisplayingimagesintocardPanel
for(inti=1;
i<
=NUM_OF_FLAGS;
i++){
JLabellabel=
newJLabel(newImageIcon("
image/flag"
+i+"
.gif"
));
cardPanel.add(label,String.valueOf(i));
}
//Panelptoholdbuttonsandacombobox
JPanelp=newJPanel();
p.add(jbtFirst=newJButton("
首页"
p.add(jbtNext=newJButton("
下一页"
p.add(jbtPrevious=newJButton("
上一页"
p.add(jbtLast=newJButton("
尾页"
p.add(newJLabel("
图片"
p.add(jcboImage=newJComboBox());
//Initializecomboboxitems
i++)
jcboImage.addItem(String.valueOf(i));
//Placepanelsintheframe
getContentPane().add(cardPanel,BorderLayout.CENTER);
getContentPane().add(p,BorderLayout.SOUTH);
//Registerlistenerswiththesourceobjects
jbtFirst.addActionListener(this);
jbtNext.addActionListener(this);
jbtPrevious.addActionListener(this);
jbtLast.addActionListener(this);
jcboImage.addItemListener(this);
/**Handlebuttonactions*/
publicvoidactionPerformed(ActionEvente){
StringactionCommand=e.getActionCommand();
if(e.getSource()instanceofJButton)
if("
.equals(actionCommand))
//ShowthefirstcomponentincardPanel
(2)cardLayout.first(cardPanel);
elseif("
//ShowthelastcomponentincardPanel
(3)cardLayout.last(cardPanel);
//ShowthepreviouscomponentincardPanel
(4)cardLayout.previous(cardPanel);
//ShowthenextcomponentincardPanel
(5)cardLayout.next(cardPanel);
/**Handleselectionofcomboboxitem*/
publicvoiditemStateChanged(ItemEvente){
if(e.getSource()==jcboImage)
//Showthecomponentatspecifiedindex
(6)cardLayout.show(cardPanel,(String)e.getItem());
//Mainmethod
publicstaticvoidmain(String[]args){
ShowCardLayoutapplet=newShowCardLayout();
JFrameframe=newJFrame();
//EXIT_ON_CLOSE==3
frame.setDefaultCloseOperation(3);
frame.setTitle("
ShowCardLayout"
);
frame.getContentPane().add(applet,BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400,320);
Dimensiond=Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width-frame.getSize().width)/2,
(d.height-frame.getSize().height)/2);
frame.setVisible(true);
}
验证性实验2
使用BoxLayout布局管理器编写程序。
publicclassShowBoxLayoutextendsJApplet
implementsActionListener{
//Createtwoboxcontainers
privateBoxbox1=
(1)Box.createHorizontalBox();
//创建水平Box
privateBoxbox2=
(2)Box.createVerticalBox();
//创建垂直Box
//Createalabeltodisplayflags
privateJLabeljlblFlag=newJLabel();
//Createimageiconsforflags
privateImageIconimageIconUS=
newImageIcon(getClass().getResource("
/image/us.gif"
privateImageIconimageIconCanada=
/image/ca.gif"
privateImageIconimageIconNorway=
/image/norway.gif"
privateImageIconimageIconGermany=
/image/germany.gif"
privateImageIconimageIconPrint=
/image/print.gif"
privateImageIconimageIconSave=
/image/save.gif"
//Createbuttonstoselectimages
privateJButtonjbtUS=newJButton("
US"
privateJButtonjbtCanada=newJButton("
Canada"
privateJButtonjbtNorway=newJButton("
Norway"
privateJButtonjbtGermany=newJButton("
Germany"
publicShowBoxLayout(){
box1.add(newJButton(imageIconPrint));
box1.add(Box.createHorizontalStrut(20));
box1.add(newJButton(imageIconSave));
box2.add(jbtUS);
box2.add((3)Box.createVerticalStrut(8));
//添加支柱
box2.add(jbtCanada);
box2.add((4)Box.createGlue());
//添加胶条
box2.add(jbtNorway);
box2.add(Box.createRigidArea((5)newDimension(10,8)));
//添加刚性块
box2.add(jbtGermany);
box1.setBorder(newjavax.swing.border.LineBorder(Color.red));
box2.setBorder(newjavax.swing.border.LineBorder(Color.black));
getContentPane().add(box1,BorderLayout.NORTH);
getContentPane().add(box2,BorderLayout.EAST);
getContentPane().add(jlblFlag,BorderLayout.CENTER);
//Registerlisteners
jbtUS.addActionListener(this);
jbtCanada.addActionListener(this);
jbtNorway.addActionListener(this);
jbtGermany.addActionListener(this);
if(e.getSource()==jbtUS)
jlblFlag.setIcon(imageIconUS);
elseif(e.getSource()==jbtCanada)
jlblFlag.setIcon(imageIconCanada);
if(e.getSource()==jbtNorway)
jlblFlag.setIcon(imageIconNorway);
if(e.getSource()==jbtGermany)
jlblFlag.setIcon(imageIconGermany);
ShowBoxLayoutapplet=newShowBoxLayout();
ShowBoxLayout"
七、设计性实验
(1)(使用CardLayout管理器)编写一个applet,进行整数和有理数的算术运算。
程序创建俩个使用CardLayout管理器的面板,一个用于整数的算术运算,另一个用于有理数的算术计算。
程序提供一个组合框,它具有俩个选项Integer和Rational。
当用户选择Integer选项时,激活整数面板;
当用户选择有理数选项时,激活有理数面板,如图10.3和10.4所示。
图10.3整数运算
图10.4有理数运算
(3)(使用选项卡)修改设计性实验1,使用选项卡代替CardLayout管理器.如下图10.6所示.
图10.5选项卡
(4)(使用分隔窗格JSplitPane)编写程序,在分隔窗格中显示四个图形,如下图10.7所示。
使用FigurePanel类显示图形。
图10.6分隔窗格JSplitPane
1.代码:
packagefirst;
publicclassShowCardLayout2extendsJApplet{
privateCardLayoutcardLayout=newCardLayout();
privateJButtonjbtIAdd,jbtISubract,jbtIMultiply,jbtIDivide;
privateJButtonjbtRAdd,jbtRSubract,jbtRMultiply,jbtRDivide;
privateJComboBoxjcbo=newJComboBox();
privateString[]optionMessage={"
IntegerOperation"
"
RationalOperation"
};
privateJLabelInteger,Rational,Number1,Number2,Result;
privateJTextFieldjtf1=newJTextField(3),jtf2=newJTextField(3),jtf3=newJTextField(3);
privateJTextFieldj1=newJTextField(3),j2=newJTextField(3),j3=newJTextField(3);
publicShowCardLayout2(){
cardPanel.setBorder(newjavax.swing.border.LineBorder(Color.RED));
JPanel[]p=newJPanel[2];
JPanelpI3=newJPanel();
pI3.add(Integer=newJLabel("
IntegerCalculation"
JPanelpI4=newJPanel();
pI4.add(Number1=newJLabel("
Number1"
pI4.add(jtf1);
pI4.add(Number2=newJLabel("
Number2"
pI4.add(jtf2);
pI4.add(Result=newJLabel("
Result"
pI4.add(jtf3);
JPanelpI2=newJPanel();
pI2.add(jbtIAdd=newJButton("
Add"
pI2.add(jbtISubract=newJButton("
Subract"
pI2.add(jbtIMultiply=newJButton("
Multiply"
pI2.add(jbtIDivide=newJButton("
Divide"
p[0]=newJPanel();
p[0].setLayout(newBorderLayout());
p[0].add(pI3,BorderLayout.NORTH);
p[0].add(pI4,BorderLayout.CENTER);
p[0].add(pI2,BorderLayout.SOUTH);
JPanelpR3=newJPanel();
pR3.add(Rational=newJLabel("
RationalCalculation"
JPanelpR4=newJPanel();
pR4.add(Number1=newJLabel("
pR4.add(j1);
pR4.add(Number2=newJLabel("
pR4.add(j2);
pR4.add(Result=newJLabel("
pR4.add(j3);
JPanelpR2=newJPanel();
pR2.add(jbtRAdd=newJButton("
pR2.add(jbtRSubract=newJButton("
pR2.add(jbtRMultiply=newJButton("
pR2.add(jbtRDivide=newJButton("
p[1]=newJPanel();
p[1].setLayout(newBorderLayout());
p[1].add(pR3,BorderLayout.NORTH);
p[1].add(pR4,BorderLayout.CENTER);
p[1].add(pR2,BorderLayout.SOUTH);
for(inti=0;
=1;
i++)
cardPanel.add(p[i],optionMessage[i]);
add(cardPanel,BorderLayout.CENTER);
for(inti=0;
=1;
j
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验 容器 布局 管理器 边框