1、Frame已经介绍过,不懂可以参考 博客-Frame
2、Pannel:用在Frame中的,类似一种面板放在Frame框架中,使用时需要配合Frame
因为生成的Frame框不能X掉,写下面这个就可以把生成的界面X掉,不至于强行停止程序
frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } });
package 狂神说__AWT; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; //面板 public class TestPannel { public static void main(String[] args) { Frame frame=new Frame(); Panel panel=new Panel(); frame.setLayout(null); //框架的坐标大小颜色 frame.setBounds(300,300,400,400); frame.setBackground(new Color(133, 240, 192)); //在frame框架中设置面板 panel.setBounds(100,100,100,100); panel.setBackground(new Color(255, 18, 30)); //框架中加入面板 frame.add(panel); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
3、Button按钮有三种类型:
(1)FlowLayout 流式布局,指定按钮在左边、右边或者中间
package 狂神说__AWT; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; //Layout布局 public class TestFlowLayout { public static void main(String[] args) { Frame frame = new Frame(); frame.setVisible(true); frame.setBounds(200,200,400,400); //按钮 Button button1 = new Button("button1"); Button button2 = new Button("button2"); Button button3 = new Button("button3"); frame.add(button1); frame.add(button2); frame.add(button3); //流式布局 //frame.setLayout(new FlowLayout()); //中心 //frame.setLayout(new FlowLayout(FlowLayout.LEFT)); //左边 frame.setLayout(new FlowLayout(FlowLayout.RIGHT)); //左边 frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
(2)BorderLayout 东西南北布局
package 狂神说__AWT; 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(); frame.setBounds(200,200,400,400); frame.setVisible(true); Button button1 = new Button("button1"); Button button2 = new Button("button2"); Button button3 = new Button("button3"); Button button4 = new Button("button4"); Button button5 = new Button("button5"); Button button6 = new Button("button6"); //表格布局 frame.setLayout(new GridLayout(3,2));//三行两列 frame.add(button1); frame.add(button2); frame.add(button3); frame.add(button4); frame.add(button5); frame.add(button6); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
综合:用布局如下的界面:
package 狂神说__AWT; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class FrameComprehensive { public static void main(String[] args) { Frame frame = new Frame(); Panel panel1 = new Panel(new BorderLayout()); Panel panel2 =new Panel(new GridLayout(2,1));//这个面板分成两行一列 Panel panel3=new Panel(new BorderLayout()); Panel panel4=new Panel(new GridLayout(2,2));//这个面板分成两行两列 frame.setVisible(true); frame.setBounds(200,200,400,400); frame.setLayout(new GridLayout(2,1)); panel1.add(new Button("button1"),BorderLayout.WEST); panel1.add(new Button("button3"),BorderLayout.EAST); panel2.add(new Button("button5")); panel2.add(new Button("button6")); panel1.add(panel2,BorderLayout.CENTER); panel3.add(new Button("button2"),BorderLayout.WEST); panel3.add(new Button("button4"),BorderLayout.EAST); for (int i = 7; i <=10; i++) { panel4.add(new Button("button"+i)); } panel3.add(panel4,BorderLayout.CENTER); frame.add(panel1); frame.add(panel3); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }