[Java][Swing] GroupLayout example

熱門文章 (Popular Post)

Posted by : Duran Hsieh 2013-08-28

GroupLayout example:

在使用Group Layout的時候,必須去計算物件的水平與垂直相對位置,
以下圖為範例,有4個Jlabel與4個JTextField。



首先設定水平群組:
在同一個水平線上,我們有兩個元件。
分別是1個Jlabel與1個JTextField,而每一個水平群組中,
各有4個Jlabel(lbl1,lbl2,lbl3,lbl4)與JTextField(tf1,tf2,tf3,tf4)。
所以可以撰寫程式碼如下:





layout.setHorizontalGroup(layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup()
           .addComponent(lbl1)
           .addComponent(lbl2)
           .addComponent(lbl3)
           .addComponent(lbl4))
        .addGroup(layout.createParallelGroup()
           .addComponent(tf1)
           .addComponent(tf2)
           .addComponent(tf3)
           .addComponent(tf4))
);



其次我們設定垂直群組:

















共有4個垂直群組,每個垂直群組各有一個Jlabel與JTextField。
所以可以撰寫程式碼如下:

layout.setVerticalGroup(layout.createSequentialGroup()
        .addGap(10)
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
           .addComponent(lbl1)
           .addComponent(tf1))
        .addGap(10)
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
           .addComponent(lbl2)
           .addComponent(tf2))
        .addGap(10)
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
           .addComponent(lbl3)
           .addComponent(tf3))
        .addGap(10)
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
           .addComponent(lbl4)
           .addComponent(tf4))
        .addGap(10)
     );


package SwingDemo;

import java.awt.BorderLayout;

import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


/***
 * 
 * @author Duran
 * 
 * GroupLayout Demo
 *
 */
public class GroupLayoutDemo {
 
 private static JFrame frame;
 
    public static void main(String[] args) {
     
     frame = new JFrame("Group Layout Demo");
     frame.setSize(200, 170);
     frame.setResizable(true);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     JPanel panel = new JPanel();
     GroupLayout layout = new GroupLayout(panel);
     panel.setLayout(layout);
     layout.setAutoCreateGaps(true);
     layout.setAutoCreateContainerGaps(true);
     
     
     JLabel lbl1 = new JLabel("lbl1");
     JLabel lbl2 = new JLabel("lbl2");
     JLabel lbl3 = new JLabel("lbl3");
     JLabel lbl4 = new JLabel("lbl4");
     
     JTextField tf1 = new JTextField();
     JTextField tf2 = new JTextField();
     JTextField tf3 = new JTextField();
     JTextField tf4 = new JTextField();

     //group layout
     layout.setHorizontalGroup(layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup()
           .addComponent(lbl1)
           .addComponent(lbl2)
           .addComponent(lbl3)
           .addComponent(lbl4))
        .addGroup(layout.createParallelGroup()
           .addComponent(tf1)
           .addComponent(tf2)
           .addComponent(tf3)
           .addComponent(tf4))
  );

     layout.setVerticalGroup(layout.createSequentialGroup()
        .addGap(10)
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
           .addComponent(lbl1)
           .addComponent(tf1))
        .addGap(10)
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
           .addComponent(lbl2)
           .addComponent(tf2))
        .addGap(10)
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
           .addComponent(lbl3)
           .addComponent(tf3))
        .addGap(10)
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
           .addComponent(lbl4)
           .addComponent(tf4))
        .addGap(10)
     );
  
     //BorderLayout
     frame.getContentPane().add(BorderLayout.CENTER,panel);

     frame.setVisible(true);

    }
}   


Demo


Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © Duran Hsieh @ Duran 的技術冶煉廠 - Date A Live - Powered by Blogger - Designed by Johanes Djogan -