package test.swing.layout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class TestGridLayout extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
TestGridLayout frame = new TestGridLayout();
frame.setVisible(true);
}
public TestGridLayout() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300); //(左邊界,上邊界,宽度,長度)
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); //(上,左,下,右)
setContentPane(contentPane);
//將Layout設定為GridLayout會把視窗畫面切割成長、宽相同大小的方格
contentPane.setLayout(new GridLayout(2, 0, 10, 5)); //(列數,欄數,左右間距,上下間距)
JButton firstBtn = new JButton("按鈕 1");
contentPane.add(firstBtn);
JButton secondBtn = new JButton("按鈕 2");
contentPane.add(secondBtn);
JButton thirthBtn = new JButton("按鈕 3");
contentPane.add(thirthBtn);
JButton forthBtn = new JButton("按鈕 4");
contentPane.add(forthBtn);
}
}
執行結果

沒有留言:
張貼留言