创建一个窗体GUI
- 创建一个java类。
public class test01{
public static void main(String[] args) {
}
}
- 让这个类继承Frame类,并导入。
import java.awt.Frame;
public class test01 extends Frame{
public static void main(String[] args) {
}
}
- 构造方法,初始化窗口。
import java.awt.Frame;
public class test01 extends Frame{
public test01() {
}
public static void main(String[] args) {
}
}
- 设置窗体属性。
设置窗体标题
this.setTitle(“我是窗体标题”);
置窗体大小
this.setSize(800, 600);
当前窗体是否居中
this.setLocationRelativeTo(null);
设置窗体关闭
本身的关闭按钮是不可用的,所以需要给关闭按钮添加一个监听器,需要导入类。
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
设置窗体可见
this.show();
全部添加进去
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class test01 extends Frame{
public test01() {
this.setTitle("我是窗体标题");
this.setSize(800, 600);
this.setLocationRelativeTo(null);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.show();
}
public static void main(String[] args) {
}
}
- 实例化
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class test01 extends Frame{
public test01() {
this.setTitle("我是窗体标题");
this.setSize(800, 600);
this.setLocationRelativeTo(null);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.show();
}
public static void main(String[] args) {
new test01();
}
}
- 保存运行
- 补充属性设置。
设置窗体大小不允许被更改,默认为可以。
this.setResizable(false);