Java GUI之五大布局
阿新 • • 發佈:2019-02-03
1.預設佈局FlowLayout
public class SetFlowLayout { JFrame frame; JButton[] button; SetFlowLayout() { frame = new JFrame(); frame.setLayout(new FlowLayout()); button = new JButton[3]; for (int i = 0; i < 3; i++) { button[i] = new JButton("" + i); frame.add(button[i]); } frame.setBounds(3502.BorderLayout, 100, 500, 500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { new SetFlowLayout(); } }
public class SetBorderLayout { JFrame frame; JButton[] button; public SetBorderLayout() { // TODO Auto-generated constructor stub3.BoxLayoutframe = new JFrame(); frame.setTitle("BorderLayout"); frame.setLayout(new BorderLayout()); button = new JButton[5]; // 先分配陣列空間範圍 button[0] = new JButton("北"); frame.add(button[0], BorderLayout.NORTH); button[1] = new JButton("南"); frame.add(button[1], BorderLayout.SOUTH); button[2] = new JButton("東"); frame.add(button[2], BorderLayout.EAST); button[3] = new JButton("西"); frame.add(button[3], BorderLayout.WEST); button[4] = new JButton("中"); frame.add(button[4], BorderLayout.CENTER); frame.setBounds(350, 100, 500, 500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { new SetBorderLayout(); } }
public class SetBoxLayout extends JFrame { private static final long serialVersionUID = 1L; public SetBoxLayout() { setLayout(new BoxLayout(getContentPane(), BoxLayout.LINE_AXIS)); getContentPane().setBackground(Color.green); add(new Button("123")); add(new Button("123")); add(new Button("123")); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 50, 700, 400); } public static void main(String[] args) { new SetBoxLayout(); } }4.卡片佈局CardLayout
public class SetCardLayout { public static void main(String[] args) { JFrame f = new JFrame(); CardLayout card = new CardLayout(); f.setLayout(card); Container c = f.getContentPane(); JButton[] b = new JButton[6]; for (int i = 0; i < b.length; i++) { b[i] = new JButton("第" + (i + 1) + "頁"); c.add(b[i], "page" + (i + 1)); } b[0].setBackground(Color.blue); b[1].setBackground(Color.yellow); b[2].setBackground(Color.red); b[3].setBackground(Color.green); b[4].setBackground(Color.orange); b[5].setBackground(Color.pink); f.setSize(300, 300); f.setVisible(true); card.show(c, "1"); while (true) { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } card.next(c); } /* * for (int i = 0; i < 6; i++) { try { while (true) { * Thread.sleep(1000); card.next(c); } } catch (InterruptedException e) * { e.printStackTrace(); } } */ } }5.網格佈局GridLayout
public class SetGridLayout { JFrame frame; JButton[] button; public SetGridLayout() { // TODO Auto-generated constructor stub frame = new JFrame(); frame.setLayout(new GridLayout(3, 4, 1, 1)); /* * GridLayout( ); 第一個、二哥引數表示網格分成的行、列, 第三個、四個引數表示網格之間的橫縱間距 */ button = new JButton[12]; for (int i = 0; i < 12; i++) { button[i] = new JButton("" + i); // " "+1將數字轉換成字串“1” frame.add(button[i]); } frame.setBounds(350, 100, 500, 500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { new SetGridLayout(); } }