java Swing布局管理之BoxLayout布局


本文为大家解析java Swing布局管理中的BoxLayout布局,供大家参考,具体内容如下

BoxLayout:可以指定在容器中是否对控件进行水平或者垂直放置,比 FlowLayout 要更为灵活
BoxLayout与其他布局管理器稍有不同,必须向其构造函数中传递容器实例的引用,由该容器使用BoxLayout。另外必须指定BoxLayout中组件的布局方式:垂直排列(按列)或水平排列(按行)。用水平组件和垂直组件的不同组合嵌套多面板的作用类似于 GridBagLayout,但没那么复杂。
1.构造函数
BoxLayout(Container target, int axis) :创建一个将沿给定轴放置组件的布局管理器。
LINE_AXIS  :指定应该根据目标容器的 ComponentOrientation 属性确定的文本行方向放置组件。
PAGE_AXIS :指定应该根据目标容器的 ComponentOrientation 属性确定的文本行在页面中的流向来放置组件。
X_AXIS  :指定组件应该从左到右放置。
Y_AXIS  :指定组件应该从上到下放置。
2.常用方法
getAxis()  :返回用于布局组件的轴。
getLayoutAlignmentX(Container target)  :返回容器沿 X 轴的对齐方式。
getLayoutAlignmentY(Container target) :  返回容器沿 Y 轴的对齐方式
getTarget() :返回使用此布局管理器的容器。
3.实例

<span style="font-family:KaiTi_GB2312;font-size:18px;">import java.awt.Container; 
 
import javax.swing.BoxLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.UIManager; 
 
public class BoxLayoutDemo { 
  public static void main(String[] args) { 
    try { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
    } catch (Exception e) { 
    } 
    JFrame frame = new JFrame("BoxLayout Test"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    Container panel = frame.getContentPane(); 
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 
    for (float align = 0.0f; align <= 1.0f; align += 0.25f) { 
      JButton button = new JButton("X align = " + align); 
      button.setAlignmentX(align); 
      panel.add(button); 
    } 
    frame.setSize(400, 300); 
    frame.setVisible(true); 
  } 
 
} 
</span> 

4.结果

以上就是本文的全部内容,希望对大家学习java Swing布局管理有所帮助和启发,谢谢大家的阅读。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3