编写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);
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章