在視窗框的配置 contentPane.setLayout () 這方法中,其參數 LayoutManager 物件設為 BorderLayout。
2. public BorderLayout(int hgap, vgap)
例如:設定元件間的水平間距hgap是 30 像素,垂直間距vgap是 400 像素。
import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; class Frame2 extends JFrame{ private JPanel contentPane; Frame2(){ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(200,200,450,300); setTitle("Swing JFrame 視窗"); contentPane = new JPanel(); contentPane.setLayout(new BorderLayout(0,0)); setContentPane(contentPane); JButton btnNorth = new JButton("按鈕-北"); contentPane.add(btnNorth, BorderLayout.NORTH); JButton btnWest = new JButton("按鈕-西"); contentPane.add(btnWest, BorderLayout.WEST); JButton btnCenter = new JButton("按鈕-中"); contentPane.add(btnCenter, BorderLayout.CENTER); JButton btnEast = new JButton("按鈕-東"); contentPane.add(btnEast, BorderLayout.EAST); JButton btnSouth = new JButton("按鈕-南"); contentPane.add(btnSouth, BorderLayout.SOUTH); setVisible(true); } } public class FrameBorder { public static void main(String[] args) { Frame2 frame = new Frame2(); } }執行結果
沒有留言:
張貼留言