一个图形
package com.nbuf;
import java.awt.*;
//GUI第一个界面
public class TestFrame {
public static void main(String[] args) {
//Frame对象
Frame frame=new Frame("我的第一个Java界面图形窗口");
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色 Color
frame.setBackground(Color.gray);
//弹出的基本位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
//需要设置可见性
frame.setVisible(true);
}
}
四个图形
import java.awt.*;
public class DemoFrame {
public static void main(String[] args) {
//展示多个窗口
MyFrame myFrame=new MyFrame(100,100,400,400,Color.black);
MyFrame myFrame1=new MyFrame(500,100,400,400,Color.blue);
MyFrame myFrame2=new MyFrame(100,500,400,400,Color.red);
MyFrame myFrame3=new MyFrame(500,500,400,400,Color.pink);
}
}
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);
setResizable(false);
}
}
关闭事件
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});