Java GridBagLayout布局实例(附完整代码)

这是老师布置的作业,实现界面和监听。本人采用了GridBagLayout布局。先放上效果图

当鼠标放到file和about时,分别显示
在这里插入图片描述
点击exit执行退出操作
在这里插入图片描述
点击update和information分别会弹出一个提示框
在这里插入图片描述
在这里插入图片描述
账号和密码有限制位数和形式,点击checkinfo会进行检查,并在左下的status栏显示是否合法,register表示注册,用户登录名和密码存在文件中
在这里插入图片描述
注册成功时的txt文件
在这里插入图片描述
点击color按钮,会在两种颜色之间切换,点击center,整体移动到电脑屏幕中央位置。

窗口类

import javax.swing.*;
import java.awt.*;
public class Chuangkou extends JFrame{
   

	public	JMenu file;
	public	JMenu about;
	public	JPanel blank2;
	public	JButton checkinfo;
	public	JButton color;
	public	JButton center;
	public	JButton register;
	public	JTextField email;
	public	JPasswordField password;
	public	JTextField status;
	public	JTextArea blank3;
	public  JMenuBar br;
	public  JMenuItem exit;
	public  JMenuItem update;
	public  JMenuItem information;
	
	public Chuangkou() {
   
		init();
		this.setTitle("Hello World");
		this.pack();
		this.setSize(377,230);
		this.setVisible(true);
//		this.setBackground(Color.BLUE);//设置背景色为蓝色
//		this.getContentPane().setVisible(true);
		this.getContentPane().setBackground(Color.WHITE);
	//	this.setLocationRelativeTo(null);//位于中间
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public void init() {
   
		file=new JMenu("File");
		about=new JMenu("About");
		blank2=new JPanel();
		email=new JTextField(15);
		password=new JPasswordField(15);
		register=new JButton("Register");
		checkinfo=new JButton("Check Info");
		color=new JButton("     Color     ");
		center=new JButton("    Center   ");
		status=new JTextField("stutas");
		blank3=new JTextArea();
		br=new JMenuBar();
		exit=new JMenuItem("Exit");
		update=new JMenuItem(
  • 9
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值