GUI编程(1)
内容:
1.Frame是一个顶级窗口
2.Panel无法单独显示,必须添加到某个容器中
3.布局管理器
1.流式
2.东西南北中
3.表格
4.设置 大小 背景颜色 可见性 监听
具体实现如下:
package com.Jiu;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Notto1 {
public static void main(String[] args) {
Frame frame1 = new Frame();
frame1.setVisible(true);
frame1.setSize(500,500);
frame1.setLocation(50,50);
frame1.setResizable(false); //设置固定大小
frame1.setBackground(new Color(0x1212E7));
Button button01 = new Button("button1");
Button button02 = new Button("button2");
Button button03 = new Button("button3");
frame1.setLayout(new FlowLayout());
frame1.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
frame1.add(button01);
frame1.add(button02);
frame1.add(button03);
MyFrame myFrame1 = new MyFrame(300, 300, 400, 400);
Button button = new Button("button");
Button button1 = new Button("button");
Button button2 = new Button("button");
Button button3 = new Button("button");
Button button4 = new Button("button");
Button button5 = new Button("button");
Button button6 = new Button("button");
Button button7 = new Button("button");
Button button8 = new Button("button");
Button button9 = new Button("button");
Panel panel1 = new Panel();
Panel panel2 = new Panel();
Panel panel3 = new Panel();
Panel panel4 = new Panel();
panel1.setLayout(new BorderLayout());
panel2.setLayout(new GridLayout(2,1));
panel3.setLayout(new BorderLayout());
panel4.setLayout(new GridLayout(2,2));
myFrame1.setLayout(new GridLayout(2,1));
myFrame1.add(panel1);
myFrame1.add(panel3);
panel1.add(panel2);
panel3.add(panel4);
panel1.add(panel2,BorderLayout.CENTER);
panel3.add(panel4,BorderLayout.CENTER);
panel1.add(button,BorderLayout.EAST);
panel1.add(button1,BorderLayout.WEST);
panel3.add(button2,BorderLayout.EAST);
panel3.add(button3,BorderLayout.WEST);
panel2.add(button4);
panel2.add(button5);
panel4.add(button6);
panel4.add(button7);
panel4.add(button8);
panel4.add(button9);
}
}
class MyFrame extends Frame{
private static int id=0;
public MyFrame(int x,int y,int w,int h){
super("Myframe"+(++id));
setLocation(x,y);
setSize(w,h);
setVisible(true);
setBackground(new Color(0xCE11CE));
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}
结果如下: