Java任务:商苑面馆登录窗体的设计

来自CloudWiki
Cloud17讨论 | 贡献2018年5月21日 (一) 14:29的版本 任务描述:购书系统登录窗体的设计
跳转至: 导航搜索

任务描述:购书系统登录窗体的设计

创建用户登录窗口,实现效果如图8.1所示

Java8-6.png

图8-1 电商购物平台——登录

任务实现

实施思路

第一步:启动Eclipse,导入工程Project0801。

第二步:在工程中新建一个类,类名为LoginForm,继承类JFrame。

public class LogonForm extends JFrame {
}

第三步:添加成员变量。

private JLabel lblUserID;//“登录账号”标签
	private JLabel lblPassword;//“登录密码”标签
	private JTextField txtUserID;//“登录账号”文本框
	private JPasswordField txtPassword;//密码框
	private JButton btnLogin;//登录按钮
	private JButton btnReset;//重置按钮

第四步:创建无参构造方法,设置窗口的基本属性

public LogonForm() {
		pack();
		this.setTitle("电商购物平台-登录");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}

第五步:在构造方法中,初始化成员变量。

lblUserID=new JLabel("用户登录",JLabel.CENTER);
		lblPassword= new JLabel("登录密码",JLabel.CENTER);
		txtUserID=new JTextField(16);
		txtPassword=new JPasswordField(16);
		btnLogon=new JButton("登录");
	        btnReset=new JButton("重置");

第六步:在构造方法中,添加用户登录、用户密码控件。

JPanel southPane=new JPanel();
		southPane.setLayout(new FlowLayout());
		southPane.add(btnLogon);
		southPane.add(btnReset);
	this.getContentPane().add(southPane,BorderLayout.SOUTH);

第六步:测试程序,在main方法中添加语句。

LogonForm logon=new LogonForm();

程序代码

public class LogonForm extends JFrame {
	private JLabel lblUserID;//“登录账号”标签
	private JLabel lblPassword;//“登录密码”标签
	private JTextField txtUserID;//“登录账号”文本框
	private JPasswordField txtPassword;//密码框
	private JButton btnLogon;//登录按钮
	private JButton btnReset;//重置按钮
	
	public LogonForm() {
		//控件初始化
		lblUserID=new JLabel("用户登录",JLabel.CENTER);
		lblPassword= new JLabel("登录密码",JLabel.CENTER);
		txtUserID=new JTextField(16);
		txtPassword=new JPasswordField(16);
		btnLogon=new JButton("登录");
		btnReset=new JButton("重置");
		//设置控件布局,在窗口中添加用户登录、用户密码控件
		JPanel centerPane=new JPanel();
		centerPane.setLayout(new GridLayout(2,2));
		centerPane.add(lblUserID);
		centerPane.add(txtUserID);
		centerPane.add(lblPassword);
		centerPane.add(txtPassword);
		this.getContentPane().add(centerPane, BorderLayout.CENTER);
		//添加登录按钮和重置按钮
		JPanel southPane=new JPanel();
		southPane.setLayout(new FlowLayout());
		southPane.add(btnLogon);
		southPane.add(btnReset);
		this.getContentPane().add(southPane,BorderLayout.SOUTH);
		
		pack();
		this.setTitle("电商购物平台-登录");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	public static void main(String[] args) {
		LogonForm logon=new LogonForm();
	}
}