组件和容器
一、Frame
package GUI;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* @Auther: 小雷学长
* @Date: 2022/3/12 - 10:55
* @Version: 1.8
*/
public class TestFrame {
public static void main(String[] args) {
//新建一个窗口
Frame frame = new Frame("管理系统");
//需要设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400, 400);
//设置背景颜色
frame.setBackground(new Color(1, 55, 10));
//弹出的初始位置
frame.setLocation(200,200);
//设置大小固定,无法改变大小
frame.setResizable(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
package GUI;
import java.awt.*;
/**
* @Auther: 小雷学长
* @Date: 2022/3/12 - 11:08
* @Version: 1.8
*/
public class TestFrame2 {
public static void main(String[] args) {
//展示多个窗口
new MyFrame(100,100,200,200,Color.GREEN);
new MyFrame(300,100,200,200,Color.GREEN);
new MyFrame(500,100,200,200,Color.GREEN);
new MyFrame(700,100,200,200,Color.GREEN);
}
}
class MyFrame extends Frame {
//可能存在多个窗口,需要一个计数器
static int id = 0;
public MyFrame(int x, int y,int w,int h,Color color) {
super("MyFrame"+(++id));
setBounds(x,y,w,h);
setVisible(true);
setBackground(color);
}
}
二、面板Panel
panel不能单独存在,可以看成是一个空间,需要放在Frame上
package GUI;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
/**
* @Auther: 小雷学长
* @Date: 2022/3/12 - 11:37
* @Version: 1.8
*/
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame();
//布局的概念
Panel panel = new Panel();
//需要设置可见性
frame.setVisible(true);
//设置布局
frame.setLayout(null);
//坐标
frame.setBounds(300,300,400,400);
frame.setBackground(new Color(193,3,4));
//panel设置坐标,相对于frame
panel.setBounds(30,40,400,500);
panel.setBackground(new Color(40,61,35));
//frame.add(panel)
frame.add(panel);
//监听事件,监听窗口关闭事件,system.exit(0)
/* frame.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
}
@Override
public void windowDeactivated(WindowEvent e) {
}
});*/
//监听事件,监听窗口关闭事件,system.exit(0)
//适配器模式
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭时要做的事情
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}