弹出一个窗口
package com.zhoutianhao.lesson1;
import java.awt.*;
//GUI的第一个界面
public class TestFrame {
public static void main(String[] args) {
//Frame对象
Frame frame = new Frame("我的第一个Java图像界面窗口");
//需要设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色 color
frame.setBackground(new Color(48, 156, 0));
//设置弹出位置
frame.setLocation(300,300);
//设置大小固定
frame.setResizable(false);
}
}
问题:该窗口不能通过导航栏关闭,只能通过终止Java程序才能关闭
弹出多个窗口
package com.zhoutianhao.lesson1;
import java.awt.*;
public class TestFrame2 {
public static void main(String[] args) {
MyFrame myFrame1 = new MyFrame(100,100,200,200,Color.blue);
MyFrame myFrame2 = new MyFrame(300,100,200,200,Color.green);
MyFrame myFrame3 = new MyFrame(100,300,200,200,Color.yellow);
MyFrame myFrame4 = new MyFrame(300,300,200,200,Color.gray);
}
}
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);
}
}