package test.swing.layout; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; public class TestBoxLayout extends JFrame { private JPanel contentPane; public static void main(String[] args) { TestBoxLayout frame = new TestBoxLayout(); frame.setVisible(true); } public TestBoxLayout() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 420, 300); //(左邊界,上邊界,宽度,長度) contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); //(上,左,下,右) setContentPane(contentPane); //將Layout設為BoxLayout時,components會依序並列或並行放置於面板上,而且超過視窗寬度時不會換行 contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS)); JButton firstBtn = new JButton("按鈕 1"); contentPane.add(firstBtn); JButton secondBtn = new JButton("按鈕 2"); contentPane.add(secondBtn); JButton thirdBtn = new JButton("按鈕 3"); contentPane.add(thirdBtn); JButton forthBtn = new JButton("按鈕 4"); contentPane.add(forthBtn); JButton fifthBtn = new JButton("按鈕 5"); contentPane.add(fifthBtn); JButton sixthBtn = new JButton("按鈕 6"); contentPane.add(sixthBtn); } }
執行結果
沒有留言:
張貼留言