📄 right7_2_4.htm
字号:
<html><head><title>JAVA编程语言</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><link rel="stylesheet" href="../../../css/text.css" type="text/css"></head><body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" ><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF"> <tr> <td valign="top"> <table width="98%" border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="#FFFFFF"> <tr> <td height="244" valign="top"> <p> <span class="pt9-black"> 和AWT相同,为了容器中的组件能实现平台无关的自动合理排列,Swing也采用了布局管理器来管理组件的排放、位置、大小等布置任务,在此基础上将显示风格做了改进。<br> <br> 另外一个不同点在于Swing虽然有顶层容器,但是我们不能把组件直接加到顶层容器中,Swing窗体中含有一个称为内容面板的容器(ContentPane),在顶层容器上放内容面板,然后把组件加入到内容面板中,前面已讲过如何得到和设置内容面板。<br> <br> 所以,在Swing中,设置布局管理器是针对于内容面板的,另外Swing新增加了一个BoxLayout布局管理器。显示上与AWT略有不同,如下图所示:<br> <br> <img src="../../../images/tu/ch07/7_2_4_1.gif" width="256" height="155"><br> <br> <img src="../../../images/tu/ch07/7_2_4_2.gif" width="391" height="154"><br> <br> <img src="../../../images/tu/ch07/7_2_4_3.gif" width="440" height="69"> <br> <br> <img src="../../../images/tu/ch07/7_2_4_4.gif" width="440" height="215"> <br> <br> 现在简单介绍一下BoxLayout布局管理器 <br> <br> BoxLayout布局管理器按照自上而下(y轴)或者从左到右(x轴)的顺序布局依次加入组件。建立一个BoxLayout对象,必须指明两个参数:被布局的容器和BoxLayout的主轴。缺省情况下,组件在纵轴方向上居中对齐。<br> <br> 设置布局管理器的方法如下:<br> pane.setLayout(new BoxLayout(pane,BoxLayout.Y-AXIS));<br> <br> <img src="../../../images/tu/ch07/7_2_4_5.gif" width="391" height="203"></span></p> <p class="pt9-black"> JScrollPane listScroller=new JScrollPane(list);<br> listScroller.setPreferredSize(new Demension(250,80));<br> listScroller.setMinimumSize(new Dimension(250,80));<br> listScroller.setAlignmentX(LEFT_ALIGNMENT);<br> ……<br> <font color="339900">//从上到下设置标签和滚动板</font>.<br> JPanel listPane=new JPanel();<br> listPane.setLayout(new BoxLayout(listPanae,BoxLayout,Y_AXIS));<br> JLabel label=new JLabel(labelText);<br> listPane.add(label);<br> listPane.add(Box.createRigidArea(new Demension(0,5)));<br> listPane.add(listScroller);<br> listPane.setBorder(BorderFactory.creatEmptyBorder(10,10,10,10);<br> <font color="339900">//从左到右设置按钮</font><br> JPanel buttonPane=new JPanel();<br> buttonPane.setLayout(new BoxLayout(buttonPane,Boxlayout.X_AXIS));<br> buttonPane.setBoder(BorderFactory.createEmptyBorder(0,10,10,10));<br> buttonPane.add(Box.createHorizontalGlue());<br> buttonPane.add(cancelButton);<br> buttonPane.add(Box.createRigiArea(new Dimension(10,0)));<br> buttonPane.add(setButton);<br> Container contentPane=getContentPane();<br> contentPane.add(listPane,BorderLayout.CENTER);<br> contentPane.add(buttonPane,BorderLayout.SOUTH);</p> </td> </tr> </table> </td> </tr></table></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -