一、流式布局
package GUIProgramming; import java.awt.*; public class TestFlowLayout { public static void main(String[] args) { Myframe f = new Myframe("",500,500,500,500, Color.red); MyPanel p1 = new MyPanel(50,100,100,100,Color.green); MyPanel p2 = new MyPanel(150,100,100,100,Color.gray); MyPanel p3 = new MyPanel(50,200,100,100,Color.yellow); MyPanel p4 = new MyPanel(150,200,100,100,Color.pink); f.setLayout(new FlowLayout()); //设置为流式布局,默认是CENTER(1) f.setLayout(new FlowLayout(FlowLayout.LEFT));//0 f.setLayout(new FlowLayout(FlowLayout.RIGHT));//2 //f.setLayout(new FlowLayout(0)); //f.setLayout(new FlowLayout(2)); f.add(p1); f.add(p2); f.add(p3); f.add(p4); } }
二、边界布局
package GUIProgramming; import java.awt.*; public class TestBorderLayout { public static void main(String[] args) { frame f = new frame(); PanelColor p1 = new PanelColor(Color.cyan); PanelColor p2 = new PanelColor(Color.yellow); PanelColor p3 = new PanelColor(Color.white); PanelColor p4 = new PanelColor(Color.gray); PanelColor p5 = new PanelColor(Color.pink); f.setBounds(200,200,500,500); //边界布局设置 f.add(p1,BorderLayout.EAST); f.add(p2,BorderLayout.WEST); f.add(p3,BorderLayout.SOUTH); f.add(p4,BorderLayout.NORTH); f.add(p5,BorderLayout.CENTER); f.setVisible(true); } } class PanelColor extends Panel { public PanelColor(Color color){ setBackground(color); } }
三、表格布局
package GUIProgramming; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestGridLayout { public static void main(String[] args) { frame frame = new frame(); PanelColor pl1 = new PanelColor(Color.cyan); PanelColor pl2 = new PanelColor(Color.yellow); PanelColor pl3 = new PanelColor(Color.white); PanelColor pl4 = new PanelColor(Color.gray); PanelColor pl5 = new PanelColor(Color.pink); PanelColor pl6 = new PanelColor(Color.red); //表格布局,自动填充 frame.setLayout(new GridLayout(3,2)); //3行2列 frame.add(pl1); frame.add(pl2); frame.add(pl3); frame.add(pl4); frame.add(pl5); frame.add(pl6); frame.setBounds(200,200,500,500); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)