学习记录-窗体-利用Frame创建窗体

创建一个窗体GUI

  1. 创建一个java类。
public class test01{
	public static void main(String[] args) {

	}
}
  1. 让这个类继承Frame类,并导入。
import java.awt.Frame;
public class test01 extends Frame{
	public static void main(String[] args) {
	}
}
  1. 构造方法,初始化窗口。
import java.awt.Frame;
public class test01 extends Frame{
	public test01() {
  	
  	}
	public static void main(String[] args) {
	}
}
  1. 设置窗体属性。

设置窗体标题

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) {
	
	}
}
  1. 实例化
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();
	}
}
  1. 保存运行
    在这里插入图片描述
  2. 补充属性设置。

设置窗体大小不允许被更改,默认为可以。

this.setResizable(false);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值