GUI窗口
单个窗口
import java.awt.*;
public class lesson01 {
public static void main(String[] args) {
Frame frame = new Frame("第一个GUI界面");
//可见性
frame.setVisible(true);
//窗口大小
frame.setSize(400,400);
//背景颜色
frame.setBackground(new Color(231, 8, 8));
//初始位置
frame.setLocation(200,200);
//固定大小
frame.setResizable(false);
}
}
多个窗口
import java.awt.*;
public class TestFrame {
public static void main(String[] args) {
MyFrame myFrame1 = new MyFrame(100,100,200,200,Color.green);
MyFrame myFrame2 = new MyFrame(200,200,100,100,Color.blue);
MyFrame myFrame3 = new MyFrame(200,400,200,400,Color.black);
MyFrame myFrame4 = new MyFrame(400,400,200,200,Color.red);
}
}
class MyFrame extends Frame{
static int id = 0;
public MyFrame(int x,int y,int w,int h,Color color){
super("myFrame"+(++id));//设置标题
setBackground(color);//设置背景颜色
setBounds(x,y,w,h);//设置大小
setVisible(true);//设置可见性
}
}
Panel面板
事件监听------关闭面板
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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(222,243,3));
//设置坐标,相对于frame
panel.setBounds(1,0,200,500);
panel.setBackground(new Color(12,123,23));
//把panel放进frame
frame.add(panel);
frame.setVisible(true);
//监听事件
//适配器模式
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭的时候需要做的事情
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}
布局
东西南北中
import java.awt.*;
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestBorderlayout");
Button east = new Button("East");
Button west = new Button("West");
Button south = new Button("South");
Button nouth = new Button("Nouth");
Button center = new Button("Center");
//东西南北中布局
frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(south,BorderLayout.SOUTH);
frame.add(nouth,BorderLayout.NORTH);
frame.add(center,BorderLayout.CENTER);
frame.setSize(400,400);
frame.setVisible(true);
}
}
表格
import java.awt.*;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestGridLayout");
Button btu1 = new Button("btu1");
Button btu2 = new Button("btu2");
Button btu3 = new Button("btu3");
Button btu4 = new Button("btu4");
Button btu5 = new Button("btu5");
Button btu6 = new Button("btu6");
frame.setLayout(new GridLayout(3,2));
frame.add(btu1);
frame.add(btu2);
frame.add(btu3);
frame.add(btu4);
frame.add(btu5);
frame.add(btu6);
frame.pack();//java函数 自动找到合适位置
frame.setVisible(true);
}
}
表格布局
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class ExDemo {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setLocation(300,400);
frame.setSize(400,300);
frame.setVisible(true);
frame.setLayout(new GridLayout(2,1));
//四个面板
Panel p1 = new Panel();
Panel p2 = new Panel(new GridLayout(2,1));
Panel p3 = new Panel();
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-but-1"));
p2.add(new Button("p2-but-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) {
super.windowClosing(e);
System.exit(0);
}
});
}
}