java Swing布局管理之BoxLayout布局_java

來源:互聯網
上載者:User

本文為大家解析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布局管理有所協助和啟發,謝謝大家的閱讀。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.