编写Java程序,使用Swing布局管理器与常用控件,实现用户登录界面

返回本章节

返回作业目录


需求说明:

使用Swing布局管理器与常用控件,实现用户登录界面

实现思路:

创建用户登录界面的类LoginFrame,在该类中创建无参数的构造方法,在构造方法中,设置窗体大小为宽300、高180,设置窗体的布局格式为空布局(绝对定位,各组件可通过坐标将组件放置于窗体中的指定位置),通过this.setResizable(false)方法设置窗体大小不能改变。

在LoginFrame类中,定义init()方法,在该方法中,通过坐标指定各组件在窗体中的位置。

实现代码:

package com.test.view;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Index extends JFrame {
	public Index(){
		this.setBounds(700, 300, 550, 400);
		this.setTitle("用户登录");
		this.setLayout(null);
		this.setResizable(false);
		
		JLabel userTest = new JLabel("账号:");
		userTest.setBounds(90, 30, 100, 100);
		JLabel pwdTest = new JLabel("密码:");
		pwdTest.setBounds(90, 90, 100, 200);
		
		JTextField userfield = new JTextField();
		userfield.setBounds(140, 60, 300, 50);
		JPasswordField pwdfield = new JPasswordField();
		pwdfield.setBounds(140, 155, 300, 50);
		
		JButton DLbutton = new JButton("登录");
		DLbutton.setBounds(160, 230, 90, 40);
		JButton ZCbutton = new JButton("注册");
		ZCbutton.setBounds(290, 230, 90, 40);
		
		this.add(userTest);
		this.add(pwdTest);
		this.add(userfield);
		this.add(pwdfield);
		this.add(DLbutton);
		this.add(ZCbutton);
	}
	public static void main(String[] args) {
		Index index= new Index();
		index.setVisible(true);
	}
}

 

  • 1
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

明金同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值