package test.swing.layout; import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; public class TestBorderLayout extends JFrame { private JPanel contentPane; public static void main(String[] args) { TestBorderLayout frame = new TestBorderLayout(); frame.setVisible(true); } public TestBorderLayout() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); //(左邊界,上邊界,宽度,長度) contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); //(上,左,下,右) setContentPane(contentPane); //依據東、西、南、北、中的方位來放置components contentPane.setLayout(new BorderLayout()); JButton eastBtn = new JButton("東方"); contentPane.add(eastBtn, BorderLayout.EAST); JButton westBtn = new JButton("西方"); contentPane.add(westBtn, BorderLayout.WEST); JButton southBtn = new JButton("南方"); contentPane.add(southBtn, BorderLayout.SOUTH); JButton northBtn = new JButton("北方"); contentPane.add(northBtn, BorderLayout.NORTH); JButton centerBtn = new JButton("中間"); contentPane.add(centerBtn, BorderLayout.CENTER); } }
執行結果
沒有留言:
張貼留言