package com.guoguo.lesson01; import sun.font.TrueTypeFont; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; //panel可以看成一个空间,不能单独存在 public class Testpanel { public static void main(String[] args) { frame frame = new frame(); //布局的概念 Panel panel = new Panel(); //设置布局 frame.setLayout(null); //坐标 frame.setBounds(300,300,500,500); frame.setBackground(new Color(2, 232, 191)); //panel也可设置,相对于frame //panel坐标设置 panel.setBounds(50,50,400,400); panel.setBackground(new Color(177,17,71)); frame.add(panel); frame.setVisible(true); //监听事件,监听窗口关闭System.exit() //适配器模式 frame.addWindowListener(new WindowAdapter() { //窗口关闭,需要做的事情 @Override public void windowClosing(WindowEvent e) { //结束程序 System.exit(0); } }); } }布局管理
流式布局、东西南北中、表格布局,java中new用alt enter
流式布局
package com.guoguo.lesson01; import java.awt.*; public class TestFlowLayerout { public static void main(String[] args) { frame frame = new frame(); //组件;按钮 Button button1 = new Button("button1"); Button button2 = new Button("button2"); Button button3 = new Button("button3"); //设置为流式布局 frame.setLayout(new FlowLayout());//默认居中 frame.setLayout(new FlowLayout(FlowLayout.LEADING));//默认居中 frame.setSize(200,200); frame.add(button1); frame.add(button2); frame.add(button3); frame.setVisible(true); }
东西南北中
package com.guoguo.lesson01; import java.awt.*; public class TestBorderlayout { public static void main(String[] args) { frame frame = new frame(); Button east = new Button("East"); Button west = new Button("West"); 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(500,200); frame.setVisible(true); } }
表格grid,包括作业;
package com.guoguo.lesson01; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class Exdemo { public static void main(String[] args) { //总frame窗口 frame frame = new frame(); frame.setSize(400,300); frame.setLocation(200,300); frame.setBackground(Color.BLACK); frame.setVisible(true); frame.setLayout(new GridLayout(2,1)); //四个面板 //上面 Panel p1 = new Panel(new BorderLayout()); Panel p2 = new Panel(new GridLayout(2,1)); Panel p3 = new Panel(new BorderLayout()); Panel p4 = new Panel(new GridLayout(2,2)); p1.add(new Button("East-1"),BorderLayout.EAST); p1.add(new Button("West-1"),BorderLayout.WEST); p2.add(new Button("P2-BTN-1")); p2.add(new Button("P2-BTN-2")); p1.add(p2,BorderLayout.CENTER); //下面 p3.add(new Button("East-2"),BorderLayout.EAST); p3.add(new Button("West-2"),BorderLayout.WEST); for (int i = 0; i < 4; i++) { p4.add(new Button("for-"+i)); } p3.add(p4,BorderLayout.CENTER); frame.add(p1); frame.add(p3); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)