Swing布局管理器介绍 不念不忘少年蓝@ 2021-12-17 14:21 230阅读 0赞 创作品,允许转载,转载时请务必以超链接形式标明文章 [原始出处][Link 1] 、作者信息和本声明。否则将追究法律责任。 [http://zhangjunhd.blog.51cto.com/113473/128174][Link 1] 当选择使用JPanel和顶层容器的content pane时,需要考虑布局管理。JPanel缺省是初始化一个FlowLayout,而content pane缺省是初始化一个BorderLayout。 下面将分别介绍几种最常用的布局管理器:FlowLayout、BorderLayout、BoxLayout、CardLayout、GridLayout和GridBagLayout。 **代码演示** 每一个布局管理器都会有一个代码演示,xxxLayoutDemo.java(见附件)。这些文件主要有三个方法组成: addComponentsToPane()提供布局逻辑(依据不同的布局管理器和UI内容)。 <table> <tbody> <tr> <td> <div> <strong>public</strong> <strong>static</strong> <strong>void</strong> addComponentsToPane(Container pane) {。。。} </div> </td> </tr> </tbody> </table> createAndShowGUI()实例化一个JFrame,通过它的ContentPane加载布局逻辑内容。 <table> <tbody> <tr> <td> <div> <strong>private</strong> <strong>static</strong> <strong>void</strong> createAndShowGUI() { </div> <div> // Create and set up the window. </div> <div> JFrame frame = <strong>new</strong> JFrame("FlowLayoutDemo"); </div> <div> frame.setDefaultCloseOperation(JFrame. <em>EXIT_ON_CLOSE</em>); </div> <div> // Set up the content pane. </div> <div> <em>addComponentsToPane</em>(frame.getContentPane()); </div> <div> // Display the window. </div> <div> frame.pack(); </div> <div> frame.setVisible( <strong>true</strong>); </div> <div> } </div> </td> </tr> </tbody> </table> main()程序入口,单独起一个线程,实例化UI。 <table> <tbody> <tr> <td> <div> <strong>public</strong> <strong>static</strong> <strong>void</strong> main(String[] args) { </div> <div> javax.swing.SwingUtilities. <em>invokeLater</em>( <strong>new</strong> Runnable() { </div> <div> <strong>public</strong> <strong>void</strong> run() { </div> <div> <em>createAndShowGUI</em>(); </div> <div> } </div> <div> }); </div> <div> } </div> </td> </tr> </tbody> </table> **FlowLayout** FlowLayout类是最简单的布局管理器。它按照和页面上排列单词的类似方式来安排组件----从左到右,直至没有多余的空间,然后转到下一行。 效果: 内容面板代码: <table> <tbody> <tr> <td> <div> <strong>public</strong> <strong>static</strong> <strong>void</strong> addComponentsToPane(Container pane) { </div> <div> pane.setLayout( <strong>new</strong> FlowLayout()); </div> <div> pane.add( <strong>new</strong> JButton("Button 1")); </div> <div> pane.add( <strong>new</strong> JButton("Button 2")); </div> <div> pane.add( <strong>new</strong> JButton("Button 3")); </div> <div> pane.add( <strong>new</strong> JButton("Long-Named Button 4")); </div> <div> pane.add( <strong>new</strong> JButton("5")); </div> <div> } </div> </td> </tr> </tbody> </table> **BorderLayout** 一个BorderLayout对象将界面分成五大区域,分别用BorderLayout类的静态常量指定: \-PAGE\_START \-PAGE\_END \-LINE\_START \-LINE\_END \-CENTER 效果: 内容面板代码: <table> <tbody> <tr> <td> <div> <strong>public</strong> <strong>static</strong> <strong>void</strong> addComponentsToPane(Container pane) { </div> <div> JButton button = <strong>new</strong> JButton("Button 1 (PAGE_START)"); </div> <div> pane.add(button, BorderLayout. <em>PAGE_START</em>); </div> <div> button = <strong>new</strong> JButton("Button 2 (CENTER)"); </div> <div> button.setPreferredSize( <strong>new</strong> Dimension(200, 100)); </div> <div> pane.add(button, BorderLayout. <em>CENTER</em>); </div> <div> button = <strong>new</strong> JButton("Button 3 (LINE_START)"); </div> <div> pane.add(button, BorderLayout. <em>LINE_START</em>); </div> <div> button = <strong>new</strong> JButton("Long-Named Button 4 (PAGE_END)"); </div> <div> pane.add(button, BorderLayout. <em>PAGE_END</em>); </div> <div> button = <strong>new</strong> JButton("5 (LINE_END)"); </div> <div> pane.add(button, BorderLayout. <em>LINE_END</em>); </div> <div> } </div> </td> </tr> </tbody> </table> **BoxLayout** BoxLayout可以将组件由上至下或由左至右依次加入当前面板。 效果: 内容面板代码: <table> <tbody> <tr> <td> <div> <strong>public</strong> <strong>static</strong> <strong>void</strong> addComponentsToPane(Container pane) { </div> <div> JPanel xPanel = <strong>new</strong> JPanel(); </div> <div> xPanel.setLayout( <strong>new</strong> BoxLayout(xPanel, BoxLayout. <em>X_AXIS</em>)); </div> <div> <em>addButtons</em>(xPanel); </div> <div> JPanel yPanel = <strong>new</strong> JPanel(); </div> <div> yPanel.setLayout( <strong>new</strong> BoxLayout(yPanel, BoxLayout. <em>Y_AXIS</em>)); </div> <div> <em>addButtons</em>(yPanel); </div> <div> </div> <div> pane.add(yPanel, BorderLayout. <em>PAGE_START</em>); </div> <div> pane.add(xPanel, BorderLayout. <em>PAGE_END</em>); </div> <div> } </div> <div> </div> <div> <strong>private</strong> <strong>static</strong> <strong>void</strong> addAButton(String text, Container container) { </div> <div> JButton button = <strong>new</strong> JButton(text); </div> <div> button.setAlignmentX(Component. <em>CENTER_ALIGNMENT</em>); </div> <div> container.add(button); </div> <div> } </div> <div> </div> <div> <strong>private</strong> <strong>static</strong> <strong>void</strong> addButtons(Container container) { </div> <div> <em>addAButton</em>("Button 1", container); </div> <div> <em>addAButton</em>("Button 2", container); </div> <div> <em>addAButton</em>("Button 3", container); </div> <div> <em>addAButton</em>("Long-Named Button 4", container); </div> <div> <em>addAButton</em>("5", container); </div> <div> } </div> </td> </tr> </tbody> </table> **CardLayout** 卡片布局和其他布局不同,因为它隐藏了一些组件。卡片布局就是一组容器或者组件,它们一次仅仅显是一个,组中的每个容器称为卡片。 效果: 内容面板代码: <table> <tbody> <tr> <td> <div> <strong>public</strong> <strong>void</strong> addComponentToPane(Container pane) { </div> <div> <strong>final</strong> JPanel contentPanel = <strong>new</strong> JPanel(); </div> <div> JPanel controlPanel = <strong>new</strong> JPanel(); </div> <div> <strong>final</strong> CardLayout cardLayout= <strong>new</strong> CardLayout();; </div> <div> pane.setLayout( <strong>new</strong> BorderLayout()); </div> <div> pane.add(contentPanel, BorderLayout. <em>CENTER</em>); </div> <div> pane.add(controlPanel, BorderLayout. <em>PAGE_END</em>); </div> <div> controlPanel.setLayout( <strong>new</strong> FlowLayout()); </div> <div> </div> <div> JButton[] b = <strong>new</strong> JButton[10]; </div> <div> <strong>for</strong> ( <strong>int</strong> i = 0; i < 10; i++) { </div> <div> b[i] = <strong>new</strong> JButton("No." + i); </div> <div> contentPanel.add(b[i]); </div> <div> } </div> <div> contentPanel.setLayout(cardLayout); </div> <div> JButton nextButton = <strong>new</strong> JButton("next"); </div> <div> nextButton.addActionListener( <strong>new</strong> ActionListener(){ </div> <div> <strong>public</strong> <strong>void</strong> actionPerformed(ActionEvent e) { </div> <div> cardLayout.next(contentPanel); </div> <div> }}); </div> <div> controlPanel.add(nextButton); </div> <div> } </div> </td> </tr> </tbody> </table> **GridLayout** GridLayout让你建立一个组件表格,并且当组件加入时,会依序又左至右,由上至下填充到每个格子,它不能由你指定想放那个格子就放那个格子 效果: 内容面板代码: <table> <tbody> <tr> <td> <div> <strong>public</strong> <strong>static</strong> <strong>void</strong> addComponentsToPane(Container pane) { </div> <div> JButton[] buttons = <strong>new</strong> JButton[9]; </div> <div> pane.setLayout( <strong>new</strong> GridLayout(3, 3)); </div> <div> <strong>for</strong> ( <strong>int</strong> i = 0; i < buttons.length; i++) { </div> <div> buttons[i] = <strong>new</strong> JButton(i + ""); </div> <div> pane.add(buttons[i]); </div> <div> } </div> <div> } </div> </td> </tr> </tbody> </table> **GridBagLayout** GridBagLayout是所有AWT布局管理器当中最复杂的,同时他的功能也是最强大的。GridBagLayout同GridLayout一样,在容器中以网格形式来管理组件。但GridBagLayout功能要来得强大得多。 1、GridBagLayout管理的所有行和列都可以是大小不同的; 2、GridLayout把每个组件限制到一个单元格,而GridBagLayout并不这样:组件在容器中可以占据任意大小的矩形区域。 GridBagLayout通常由一个专用类来对他布局行为进行约束,该类叫GridBagConstraints。其中有11个公有成员变量,GridBagConstraints可以从这11个方面来进行控制和操纵。这些内容是: 1、gridx—组件的横向坐标; 2、girdy—组件的纵向坐标; 3、gridwidth—组件的横向宽度,也就是指组件占用的列数; 4、gridheight—组件的纵向长度,也就是指组件占用的行数; 5、weightx—指行的权重,告诉布局管理器如何分配额外的水平空间; 6、weighty—指列的权重,告诉布局管理器如何分配额外的垂直空间; 7、anchor—当组件小于其显示区域时使用此字段; 8、fill—如果显示区域比组件的区域大的时候,可以用来控制组件的行为。控制组件是垂直填充,还是水平填充,或者两个方向一起填充; 9、insets—指组件与表格空间四周边缘的空白区域的大小; 10、ipadx— 组件间的横向间距,组件的宽度就是这个组件的最小宽度加上ipadx值; 11、ipady— 组件间的纵向间距,组件的高度就是这个组件的最小高度加上ipady值。 说明: 1、gridx,gridy:其实就是组件行列的设置,注意都是从0开始的,比如 gridx=0,gridy=1时放在0行1列; 2、gridwidth,gridheight:默认值为1;GridBagConstraints.REMAINDER常量,代表此组件为此行或此列的最后一个组件,会占据所有剩余的空间; 3、weightx,weighty:当窗口变大时,设置各组件跟着变大的比例。比如组件A的weightx=0.5,组件B的weightx=1,那么窗口X轴变大时剩余的空间就会以1:2的比例分配给组件A和B; 4、anchor:当组件空间大于组件本身时,要将组件置于何处。 有CENTER(默认值)、NORTH、NORTHEAST、EAST、SOUTHEAST、WEST、NORTHWEST选择。 5、insets:设置组件之间彼此的间距。它有四个参数,分别是上,左,下,右,默认为(0,0,0,0)。 效果: 内容面板代码: <table> <tbody> <tr> <td> <div> <strong>public</strong> <strong>static</strong> <strong>void</strong> addComponentsToPane(Container pane) { </div> <div> JButton button; </div> <div> pane.setLayout( <strong>new</strong> GridBagLayout()); </div> <div> GridBagConstraints c = <strong>new</strong> GridBagConstraints(); </div> <div> </div> <div> button = <strong>new</strong> JButton("Button 1"); </div> <div> c.fill = GridBagConstraints. <em>HORIZONTAL</em>; </div> <div> c.gridx = 0; </div> <div> c.gridy = 0; </div> <div> pane.add(button, c); </div> <div> </div> <div> button = <strong>new</strong> JButton("Button 2"); </div> <div> c.fill = GridBagConstraints. <em>HORIZONTAL</em>; </div> <div> c.weightx = 0.5; </div> <div> c.gridx = 1; </div> <div> c.gridy = 0; </div> <div> pane.add(button, c); </div> <div> </div> <div> button = <strong>new</strong> JButton("Button 3"); </div> <div> c.fill = GridBagConstraints. <em>HORIZONTAL</em>; </div> <div> c.weightx = 0.5; </div> <div> c.gridx = 2; </div> <div> c.gridy = 0; </div> <div> pane.add(button, c); </div> <div> </div> <div> button = <strong>new</strong> JButton("Long-Named Button 4"); </div> <div> c.fill = GridBagConstraints. <em>HORIZONTAL</em>; </div> <div> c.ipady = 40; // make this component tall </div> <div> c.weightx = 0.0; </div> <div> c.gridwidth = 3; </div> <div> c.gridx = 0; </div> <div> c.gridy = 1; </div> <div> pane.add(button, c); </div> <div> </div> <div> button = <strong>new</strong> JButton("5"); </div> <div> c.fill = GridBagConstraints. <em>HORIZONTAL</em>; </div> <div> c.ipady = 0; // reset to default </div> <div> c.weighty = 1.0; // request any extra vertical space </div> <div> c.anchor = GridBagConstraints. <em>PAGE_END</em>; // bottom of space </div> <div> c.insets = <strong>new</strong> Insets(10, 0, 0, 0); // top padding </div> <div> c.gridx = 1; // aligned with button 2 </div> <div> c.gridwidth = 2; // 2 columns wide </div> <div> c.gridy = 2; // third row </div> <div> pane.add(button, c); </div> <div> } </div> </td> </tr> </tbody> </table> 一个GardBagLayout布局的左右选择框,代码GridBagLayoutFrame.java见附件,效果: 本文出自 “[子 孑][Link 2]” 博客,请务必保留此出处[http://zhangjunhd.blog.51cto.com/113473/128174][Link 1] 转载于:https://www.cnblogs.com/kungfupanda/p/5995461.html [Link 1]: http://zhangjunhd.blog.51cto.com/113473/128174 [Link 2]: http://zhangjunhd.blog.51cto.com/
还没有评论,来说两句吧...