布局模式
-
流式布局
Frame frame = new Frame("这是我的第一个frame窗口!"); frame.setVisible(true); frame.setSize(400, 400); frame.setBackground(Color.LIGHT_GRAY); frame.setLayout(new FlowLayout(FlowLayout.CENTER));//流式布局 Button btn1 = new Button("按钮1"); btn1.setBackground(Color.BLUE); Button btn2 = new Button("按钮2"); btn2.setBackground(Color.CYAN); Button btn3 = new Button("按钮3"); btn3.setBackground(Color.DARK_GRAY); frame.add(btn1); frame.add(btn2); frame.add(btn3);
-
东西南北中布局
Frame frame = new Frame("这是我的第一个frame窗口!"); frame.setVisible(true); frame.setBounds(50,50,500,500); frame.setBackground(Color.DARK_GRAY); frame.setLayout(new BorderLayout()); //东西南北中布局 frame.add(new Button("East"),BorderLayout.EAST); frame.add(new Button("Wast"),BorderLayout.WEST); frame.add(new Button("South"),BorderLayout.SOUTH); frame.add(new Button("North"),BorderLayout.NORTH); frame.add(new Button("Center"),BorderLayout.CENTER); frame.setVisible(true);
-
表格布局
Frame frame = new Frame("这是我的第一个frame窗口!"); frame.setBounds(50,50,500,500); frame.setBackground(Color.DARK_GRAY); frame.setLayout(new GridLayout(3,2)); //表格布局 frame.add(new Button("b1")); frame.add(new Button("b2")); frame.add(new Button("b3")); frame.add(new Button("b4")); frame.add(new Button("b5")); frame.add(new Button("b6")); frame.pack(); frame.setVisible(true);
-
小练习
Frame frame = new Frame("这是我的第一个frame窗口!"); frame.setBounds(50,50,800,800); frame.setBackground(Color.DARK_GRAY); frame.setLayout(new GridLayout(2,1)); //上面板 Panel p1 = new Panel(new BorderLayout()); p1.setBackground(Color.GREEN); Panel p12 = new Panel(new GridLayout(2,1)); p12.add(new Button("上中上")); p12.add(new Button("上中下")); p1.add(new Button("上左"),BorderLayout.EAST); p1.add(new Button("上右"),BorderLayout.WEST); p1.add(p12,BorderLayout.CENTER); //下面板 Panel p2 = new Panel(new BorderLayout()); p2.setBackground(Color.BLUE); Panel p22 = new Panel(new GridLayout(2,2)); p22.add(new Button("下上左")); p22.add(new Button("下上右")); p22.add(new Button("下下左")); p22.add(new Button("下下右")); p2.add(new Button("下左"),BorderLayout.EAST); p2.add(new Button("下右"),BorderLayout.WEST); p2.add(p22,BorderLayout.CENTER); frame.add(p1); frame.add(p2); frame.setVisible(true);