Frame窗口
package GUI1; import java.awt.*; public class Frame{ public static void main(String[] args) { java.awt.Frame F1=new java.awt.Frame("我的图形界面"); F1.setVisible(true); //可视化 F1.setSize(500,300); //边框的大小 F1.setBackground(Color.pink); //颜色 F1.setLocation(100,100); //位置 F1.setResizable(false); //不能变大 } }
package GUI1; import java.awt.*; public class Frame2 { public static void main(String[] args) { new Fam(200,144,200,345,Color.darkGray); new Fam(200,144,100,345,Color.green); new Fam(300,344,200,345,Color.pink); new Fam(300,544,200,345,Color.darkGray); } } class Fam extends Frame { static int id=0; public Fam (int x,int y,int w,int h,Color color){ super("fame+"+(++id)); setVisible(true); //可视化 setBounds(x, y, w, h); setBackground(color);//颜色 } }
多个窗口;
package GUI1; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestPane { public static void main(String[] args) { Frame f1=new Frame(); Panel p1=new Panel(); f1.setLayout(null); //设置布局; f1.setBounds(300,300,500,500); f1.setBackground(new Color(0xB35269)); p1.setBounds(50,50,400,400); p1.setBackground(new Color(193, 138, 42)); f1.add(p1); f1.setVisible(true); f1.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { super.windowClosing(e); System.exit(0); } }); } }
面板以及其关闭;
布局管理器:
package GUI1; import java.awt.*; public class TestFlowLayout { public static void main(String[] args) { Frame frame=new Frame(); Button button=new Button("按钮"); Button button2=new Button("按钮"); Button button3=new Button("按钮"); frame.setLayout(new FlowLayout(FlowLayout.LEFT)); frame.setSize(200,200); frame.setVisible(true); frame.add(button); frame.add(button2); frame.add(button3); } }
package GUI1; import java.awt.*; public class TestBoderLayout { public static void main(String[] args) { Frame frame=new Frame("testboderlayout"); Button east = new Button("east"); Button west = new Button("west"); Button north = new Button("north"); Button south = new Button("south"); Button center = new Button("center"); frame.add(east,BorderLayout.EAST); frame.add(west,BorderLayout.WEST); frame.add(north,BorderLayout.NORTH); frame.add(south,BorderLayout.SOUTH); frame.add(center,BorderLayout.CENTER); frame.setVisible(true); frame.setBounds(200,400,400,500); } }
东西南北中的布局;
package GUI1; import java.awt.*; public class TestGridLayOUT { public static void main(String[] args) { Frame frame=new Frame("testboderlayout"); Button bt1 = new Button("bt1"); Button bt2 = new Button("bt2"); Button bt3 = new Button("bt3"); Button bt4 = new Button("bt4"); Button bt5 = new Button("bt5"); Button bt6 = new Button("bt6"); frame.setLayout(new GridLayout(3,3)); frame.add(bt1); frame.add(bt2); frame.add(bt3); frame.add(bt4); frame.add(bt5); frame.add(bt6); frame.pack(); //自动选择优秀的布局 frame.setVisible(true); } }
frame.pack(); //自动选择优秀的布局