流式布局 FlowLayout
东西南北中 BorderLayout
表格布局 GridLayout
流式布局
package YMP.GUI; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestFlowLayout { public static void main(String[] args) { Frame frame=new Frame("TestFlowLayout"); Button button1=new Button("btn1"); Button button2=new Button("btn2"); Button button3=new Button("btn3"); frame.setLayout(new FlowLayout()); frame.setSize(200,200); frame.setVisible(true); frame.add(button1); frame.add(button2); frame.add(button3); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
东西南北中
package YMP.GUI; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestBorderLayout { public static void main(String[] args) { Frame frame=new Frame("TestBorderLayout"); Button east=new Button("East"); Button west=new Button("Wset"); Button south=new Button("South"); Button north=new Button("North"); Button center=new Button("Center"); frame.add(east,BorderLayout.EAST); frame.add(west,BorderLayout.WEST); frame.add(south,BorderLayout.SOUTH); frame.add(north,BorderLayout.NORTH); frame.add(center,BorderLayout.CENTER); frame.setSize(200,200); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
表格布局
package YMP.GUI; 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("TestGridLayout"); frame.setLayout(new GridLayout(3,2)); for(int i=0;i<6;i++){ frame.add(new Button("bton"+(i+1))); } frame.setSize(400,400); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
应用练习
设计出图片所示窗口
流程思路
1.frame顶级容器
2.4个panel,分别对应frame的上半,下半和图中橙黄两色块
3.蓝色部分为button
4.在橙黄色色块中用BorderLayout加入button
代码实现
package YMP.GUI; import javax.swing.border.Border; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.BufferedReader; public class ExDemo { public static void main(String[] args) { Frame frame=new Frame(); frame.setSize(400,300); frame.setLayout(new GridLayout(2,1)); frame.setVisible(true); Panel p1=new Panel(); Panel p2=new Panel(); Panel p3=new Panel(); Panel p4=new Panel(); frame.add(p1); frame.add(p2); Button button1=new Button("btn1"); Button button2=new Button("btn2"); Button button3=new Button("btn3"); Button button4=new Button("btn4"); Button button5=new Button("btn5"); Button button6=new Button("btn6"); Button button7=new Button("btn7"); Button button8=new Button("btn8"); Button button9=new Button("btn9"); Button button10=new Button("btn10"); p1.setLayout(new BorderLayout()); p1.add(button2,BorderLayout.EAST); p1.add(button1,BorderLayout.WEST); p1.add(p3,BorderLayout.CENTER); p2.setLayout(new BorderLayout()); p2.add(button3,BorderLayout.WEST); p2.add(button4,BorderLayout.EAST); p2.add(p4, BorderLayout.CENTER); p3.setLayout(new GridLayout(2,1)); p3.add(button5); p3.add(button6); p4.setLayout(new GridLayout(2,2)); p4.add(button7); p4.add(button8); p4.add(button9); p4.add(button10); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
结果展示
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » GUI编程02-布局管理器
发表评论 取消回复