第8章 用户界面.docx
- 文档编号:17293878
- 上传时间:2023-07-23
- 格式:DOCX
- 页数:139
- 大小:759.40KB
第8章 用户界面.docx
《第8章 用户界面.docx》由会员分享,可在线阅读,更多相关《第8章 用户界面.docx(139页珍藏版)》请在冰点文库上搜索。
第8章用户界面
第8章用户界面
考点01布局管理器
规则01:
在Java语言中共定义了5种布局管理器,分别为FlowLayout、BorderLayout、GridLayout、CardLayout和GridBagLayout。
规则02:
在java.awt.Container类中定义的setLayout()方法用来为容器设置布局管理器,该方法语法定义为publicvoidsetLayout(LayoutManagermgr)。
规则03:
FlowLayout布局管理器。
语法格式:
publicclassFlowLayoutextendsObjectimplementsLayoutManager,Serializable
构造器列表:
publicFlowLayout()
构造一个新的FlowLayout布局管理器实例,其默认组件排列方式为居中排列,组件间的水平和垂直间隙为5个像素。
PublicFlowLayout(intalign){}
构造一个新的指定组件排列方式的FlowLayout布局管理器实例,其默认组件间的水平和垂直间隙为5个像素。
构造一个新的指定组件排列方式和组件间的水平和垂直间隙的FlowLayoiut布局管理实例。
布局策略:
FlowLayout布局管理器在添加组件到容器中时,依据添加组件的次序,按照从左到右,从上到下的原则来排列组件,以满足组件本身的尺寸来考虑布局。
当一行排满后,会自动从第二行开始,依此类推,直到放置完所有组件。
FlowLayout布局管理器具有维持组件尺寸不变的特性要求,即布局管理器使用组件本身大小来放置组件,组件的尺寸不会受到容器大小的影响,只是控件的相对位置会有所改变。
当容器控件无法容纳所有组件时,组件并不通过改变大小来适应容器空间,而只是显示出部分来。
规则04:
FlowLayout布局管理器是Applet和Panel容器的默认布局管理器。
示例代码如下:
importjava.awt.*;
publicclassFlow
extendsFrame{
publicstaticvoidmain(Stringargs[]){
Flowf=newFlow();
f.setSize(300,200);
f.setVisible(true);
}
publicFlow(){
setLayout(newFlowLayout());
add(newButton(“First”));
add(newButton(“Second”));
add(newButton(“Third”));
}
}
布局视图如图8-1所示。
图8-1窗口布局视图
规则04:
BorderLayout布局管理器。
语法格式:
publicclassBorderLayoutextendsObjectimplementsLayoutManeger2,Serializable
构造器列表:
publicBorderLyout()
构造一个新的BorderLayout布局管理器实例,其默认组件间无空隙。
PublicBorderLayout(inthgap,intvgap){}
构造一个新的指定组件间的水平和垂直间隙的BorderLayout布局管理实例。
布局策略:
BorderLayout布局管理器把整个显示区域分为5个部分,分别为center、north、south、west和east。
BorderLayout布局管理器添加组件时,须明确指定需添加到的区域。
如果未指定区域,则自动将center区域作为添加到的指定区域,并且当其他区域无组件放置时,则占据整个容器区域。
有两种方式用于添加组件,一种采用常量代表区域,方法的语法形式为add(Componentcomp,intindex),指定区域参数用定义在BorderLayout类上的5个常量来标识,5个常量分别为BorderLayout.NORTH、BorderLayout.SOUTHerLayout.WESTderLayout.EAST和Layout.CENTER。
另一种方式采用字符串代表区域,方法的语法形式为add(Stringname,Componentcomp),指定区域参数用5个字符串来标识,5个字符串分别为north、south、west、east和center,注意,两种方法参数顺序相反。
BorderLayout布局管理器的一个区域中只能放置一个组件。
如果同一个区域中添加了多个组件,则只有最后一个添加的组件被显示出来,但可以通过添加中间容器来实现在一个区域中同时添加多个组件。
BorderLayout布局管理器中的组件会自动改变原有的尺寸,充满容器的全部空间。
当其中有些区域未放置组件时,其区域会被其他区域中的组件扩充占有。
放置在north和south区域中的组件,其高度同组件原有尺寸,而宽度自动与容器一致;而放置在west和eas区域中的组件则正好相反,其宽度同组件原有尺寸,而高度自动与区域高度一致,当north和south区域中无组件时,高度自动与宽度高度一致。
示例代码如下:
importjava.awt.*;
publicclassBorder
extendsFrame{
publicstaticvoidmain(Stringargs[]){
Borderb=newBorder();
b.setSize(300,200);
b.setVisible(true);
}
publicBorder(){
setLayout(newBorderLayout());
add(newButton(“North”),BorderLayout.NORTH);
add(newButton(“South”),BorderLayout.SOUTH);
add(newButton(“East”),BorderLayout.EAST);
add(newButton(“West”),BorderLayout.WEST);
add(newButton(“Center”),BorderLayout.CENTER);
}
}
布局视图如图8-2所示。
图8-2窗体视图
规则05:
GridLayout布局管理器。
语法格式:
publicclassGridLayoutextendsimplementsLayoutManegen,Serializable
构造器列表:
publicGridLayout()
构造一个新的GridLayout布局管理器实例,其默认行数和列数为1。
PublicGridLayout(introws,intcols)
构造一个新的指定行数和列数的GridLayout布局管理实例。
PublicGridLayout(introws,intcols,inthgap,intvgap)
构造一个新的指定行数、列数和组件间的水平和垂直间隙的GridLayout布局管理器实例。
布局策略:
GridLayout布局管理器通过指定的行、列数把整个布局区域划分为大小相同的网格区域,
依据从左到右,从上到下的顺序在网格中排列组件,总是通过改变组件的原有尺寸来满足网格单元区域的大小要求,因此添加进的组件尺寸会被统一。
GridLayout布局管理器中当放置的组件数超过指定的单元格数,GridLayout布局管理器自动通过增加列数来扩充单元格数以容纳多出的组件。
GridLayout布局管理器是Frame容器的默认布局管理器。
示例代码如下:
importjava.awt.*;
publicclassGrid
extendsFrame{
publicstaticvoidmain(Stringargs[]){
Gridg=newGrid();
g.setSize(300,200);
g.setVisible(true);
}
publicGrid(){
setLayout(newGridLayout(3,2));
add(newButton(“1”));
add(newButton(“2”));
add(newButton(“3”));
add(newButton(“4”));
add(newButton(“5”));
add(newButton(“6”));
}
}
布局视图如图8-3所示。
8-3布局视图
规则06:
GridBagLayout布局管理器。
语法格式:
publicclassGridBagLayoutextendsObjectimplementsLayoutManeger2,Serializable
构造器列表:
publicGridBagLayout()
构造一个新的GridBagLayout布局管理器实例。
构造策略:
GridBagLayout布局管理器自身不直接管理组件在容器中的布局,而是通过一个帮助类GridBagConstraints把容器分成不同大小的区域,允许一个区域中放置多个组件,也允许一个组件占据多个区域。
GridBagLayout布局管理器在添加组件到容器中时,需同时传进与该组件联系的一个GridBagConstraints类实例来定位组件在容器中的位置。
注意,同一个GridBagConstraints类实例可以被所有添加的组件公用。
GridBagConstraints类主要通过4个属性域gridx、gridy、gridwidth、gridheigth来布局组件。
gridx和gridy属性域用来控制组件占据的空间,属性域gridwidth用来指定组件显示区域横向包含的单元个数,即列数;属性gridheigth用来指定组件显示区域纵向包含的单元个数,即行数。
示例代码如下:
importjava.awt.*;
publicclassGridBag
extendsPanel{
privatelPaneltallPanel()=newPanel();
privatelPaneltallPanel2()=newPanel();
publicGridBag(){
tallPanel.setLayout(newGridLayout(3,1));
tallPanel.add(newButton(Press”));
tallPanel.add(newButton(“Any”));
tallPanel2.setLayout(newGridLayout(3,1));
tallPanel2.add(newButton(“Don’t”));
tallPanel2.add(newButton(“Press”));
tallPanel2.add(newButton(“These”));
setLayout(newGridLayout());
GridBagConstraintsc=newGridBagConstraints();
c.gridx=0;
c.gridy=0;
add(newButton(“topleft”),c);
c.gridx=1;
add(newButton(“topmiddle”),c);
c.gridx=2;
add(newButton(“topleft”),c);
c.gridx=0;
c.gridy=1;
add(newButton(“lefthandsidemiddle”),c);
c.gridx=1;
add(tallPanel,c);
c.gridy=2;
add(newButton(“bottomcenter”),c);
c.gridy=2;
add(tallPanel2,c);
}
Publicstaticvoidmain(Stringargs[]){
framef=newFrame(“GridBag1example”);
f.add{newGridBag()};
f.pack();
f.setSize(300,200);
f.setVisible(true);
}
}
布局视图如图8-4所示。
图8-4窗体布局
规则07:
CardLayout布局管理器。
语法格式:
publicclassCardLayoutextendsObjectimplementsLayoutManager2,Serializable
构造器列表:
publicCardLayout()
构造一个新的组件。
PublicCardLayout(inthgap,intvgap)
布局策略:
CardLayout布局管理器把组件安排在不同的“卡片容器”中,这些卡片容器相互重叠,一次只能显示一个。
CardLayout布局管理器中的组件会自动改变原有尺寸,充满全部空间。
CardLayout布局管理器在添加组件时,需要为每个组件起个名称,以便于以后可直接通过名称调用组件。
CardLayout布局管理器提供两种方式显示组件,一种形式是通过组件名称调用show()方法显示指定的组件,show()方法的语法形式为publicvoidshow(Containerparent,Stringname);另一种形式根据组件添加的顺序直接调用first(Containerparent)、next(Containerparent)、previous(Containerparent)、last(Containerparent)四个方法显示组件。
示例代码如下:
importjava.awt.*;
importjava.awt.event.*;
publicclassCard
extendsPanel{
privatePanelcardPanel=newpanel();
privateCardLayout=newCardLayout();
privatePanelcontrolPanel=newpanel();
privateButtonfirstButton=newButton("First");
privateButtonlastButton=newButton("Last");
privateButtonnextButton=newButton("Next");
privateButtonprevButton=newButton("Prev");
privateTextFieldselectText=newTextField();
publicCard(){
setLayout(newBorderLayout());
cardPanel.setLayout(cardLayout);
Panelp=newPanel();
p.setLayout(newBorderLayout());
p.add(newLabel("ThisispanelOne"),BorderLayout.NORTH);
p,add(newButton("I"),BorderLayout.CENTER);
cardPanel.add(p,"I");
p=newPanel();
p.setLayout(newBorderLayout());
p.add(newLabel("ThisispanelTwo"),BorderLayout.NORTH);
p.add(newButton("2"),BorderLayout.CENTER);
cardPanel.add(p,"2");
p=newPanel();
p.setLayout(newBorderLayout());
p.add(newLabel("ThisispanelThree"),BorderLayout.NORTH);
p.add(newButton("3"),BorderLayout.CENTER);
cardPanel.add(p,"3");
p=newPanel();
p.setLayout(newBorderLayout());
p.add(newLabel("ThisispanelFour"),BorderLayout.NORTH);
p.add(newButton("4"),BorderLayout.CENTER);
cardPanel.add(p,"4");
add(cardPanel,BorderLayout.CENTER);
firstButton.addActionListener(
newActionListener(){
publicvoidactionPerformed(ActionEvente){
cardLayout.last(cardPanel);
}
}
);
lastButton.addActionListener(
newActionListener(){
publicvoidactionPerformed(ActionEvente){
cardLayout.next(cardPanel);
}
}
);
prevButton.addActionListener(
newActionListener(){
publicvoidactionPerformed(ActionEvente){
cardLayout.previous(cardPanel);
}
}
);
selectText.addActionListener(
newActionListener(){
publicvoidactionPerformed(ActionEvente){
cardLayout.previous(cardPanel,selectText.getText());
}
}
);
Panelcp1=newPanel();
Panelcp2=newPanel();
cp1.add(firstButton);
cp1.add(prevButton);
cp1.add(nextButton);
cp1.add(lastButton);
cp2.add(newLabel("EnterPanelNumber."));
cp2.add(selectText);
controlPanel.setLayout(newBorderLayout());
controlPanel.add(cp1,BorderLayout.NORTH);
controlPanel.add(cp2,BorderLayout.SOUTH);
add(control,BorderLayout.SOUTH);
}
publicstaticvoidmain(Stringargs[]){
Framef=newFrame("CardLayoutExample");
Cardcard=newCard();
f.add(card,BorderLayout.CENTER);
f.pack();
f.setSize(300,200);
f.setVisible(true);
}
}
局部视图如图8-5所示。
图8-5布局视图
示例01:
请问,是否GridLayout布局管理器中所有单元格均具有相同的大小?
请选择一个正确答案:
(1)是
(2)否
答案:
(1)
详解:
本题考查的核心是GridLayout布局管理器,该布局管理器的构造器语法为publicGridLayout(introw,intcols),通过指定行数和列数将整个显示区域划分成相同大小的子区域,用于放置组件。
示例02:
请问以下哪些布局管理器按照表格形式组件?
请选择一个正确答案:
(1)CardLayout
(2)BorderLayout
(3)FlowLayout
(4)GridLayout
(5)GridBagLayout
答案:
(4)
详解:
本题考查的核心是GridLayout布局管理器,该布局管理器通过指定行数和列数将整个显示区域划分成相同大小的子区域,用于放置组件。
示例03:
请问,以下哪些布局管理器具有可使所有所包含组件有相同高度和宽度?
请选择一个正确答案:
答案:
(3)
详解:
本题考查的核心是GridLayout布局管理器,该布局管理器通过指定行数和列数将整个显示区域划分成相同大小的子区域用于放置组件。
放置在其内的组件自动调整大小以适应GridLayout布局管理器划分的子区域大小,即不管原有组件的大小。
示例04:
请问,BorderLayout布局管理器是否要求每个区域只能容纳一个组件?
请选择一个正确答案:
(1)是
(2)否
答案:
(1)
详解:
本题考查的核心是BorderLayout布局管理器,该布局管理器把整个显示区域分为5部分,分别为center、north、south、west和east。
每个区域只能放置一个可视组件,如果在同一个区域连续放置多个可视组件,则只有最后一个组件可以显示出来,如果需要在BorderLayout布局管理器中的一个子区域中放置多个组件,必须通过一个具有可放置多个组件的布局管理器的中间容器来实现在同一个BorderLayout布局管理器的区域中放置多个组件。
示例05:
请问,以下哪个布局管理器是Applet和Panel的默认布局管理器?
(1)FlowLayout
(2)BorderLayout
(3)GridBagLayout
(4)GridLayout
(5)以上均不是
答案:
(1)
详解:
本题考查的核心是默认布局管理器,ATM中的每一个容器都有自己的默认布局管理器。
例如,Frame的默认布局管理器为BorderLayout,而Applet的默认布局管理器为FlowLayout。
注意,可以通过调用setLayout()方法来重新设置布局管理器。
示例06:
请问,FlowLayout布局管理器是以下哪些容器类的默认布局管理器?
请选择所有正确答案:
(1)Panel
(2)Applet
(3)Frame
(4)Window
(5)Dialog
答案:
(1)
(2)
详解:
本题考查的核心是默认布局管理器。
本题和上题考查的实质是一样,只是反过来提问。
示例07:
请问以下哪个是Frame窗体默认的布局管理器?
(1)FlowLayout
(2)BorderLayout
(3)GridLayout
(4)GridBagLayout
(5)CardLayout
答案:
(2)
详解:
本题考查的核心是默认布局管理器。
Frame窗体默认的是BorderLayout布局管理器。
示例08:
请问,以下哪些布局管理器保留所管理组件的首选宽度和高度?
(1)GridLayout
(2)GridBagLayout
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 第8章 用户界面