“Java任务:商苑面馆登录窗体的设计”的版本间的差异

来自CloudWiki
跳转至: 导航搜索
任务描述:购书系统登录窗体的设计
程序代码
第54行: 第54行:
  
 
==程序代码==
 
==程序代码==
  <nowiki>public class LogonForm extends JFrame {
+
  <nowiki>package main;
private JLabel lblUserID;//“登录账号”标签
+
 
private JLabel lblPassword;//“登录密码”标签
+
import java.awt.BorderLayout;
 +
import java.awt.FlowLayout;
 +
import java.awt.GridLayout;
 +
 
 +
import javax.swing.*;
 +
 
 +
public class UserForm extends JFrame{
 +
private JLabel labelUser;//“登录账号”标签
 +
private JLabel labelPwd;//“登录密码”标签
 
private JTextField txtUserID;//“登录账号”文本框
 
private JTextField txtUserID;//“登录账号”文本框
 
private JPasswordField txtPassword;//密码框
 
private JPasswordField txtPassword;//密码框
第62行: 第70行:
 
private JButton btnReset;//重置按钮
 
private JButton btnReset;//重置按钮
 
 
public LogonForm() {
+
public UserForm() {
//控件初始化
+
    super("图书管理系统用户登陆");
lblUserID=new JLabel("用户登录",JLabel.CENTER);
+
    this.setLayout(new BorderLayout());//为框架上的面板设立布局方式,设为边界布局
lblPassword= new JLabel("登录密码",JLabel.CENTER);
+
   
txtUserID=new JTextField(16);
+
//0.控件初始化
txtPassword=new JPasswordField(16);
+
labelUser=new JLabel("用户登录",JLabel.CENTER);
btnLogon=new JButton("登录");
+
labelPwd= new JLabel("登录密码",JLabel.CENTER);
btnReset=new JButton("重置");
+
txtUserID=new JTextField(16);
//设置控件布局,在窗口中添加用户登录、用户密码控件
+
txtPassword=new JPasswordField(16);
JPanel centerPane=new JPanel();
+
btnLogon=new JButton("登录");
centerPane.setLayout(new GridLayout(2,2));
+
btnReset=new JButton("重置");
centerPane.add(lblUserID);
+
centerPane.add(txtUserID);
+
centerPane.add(lblPassword);
+
//A.创建第一个面板,内部采用网格布局,在其中添加用户登录、用户密码控件
centerPane.add(txtPassword);
+
JPanel centerPane=new JPanel();
this.getContentPane().add(centerPane, BorderLayout.CENTER);
+
centerPane.setLayout(new GridLayout(2,2));//步骤1:创建网格布局
//添加登录按钮和重置按钮
+
centerPane.add(labelUser);//步骤2:将组件添加到面板上
JPanel southPane=new JPanel();
+
centerPane.add(txtUserID);
southPane.setLayout(new FlowLayout());
+
centerPane.add(labelPwd);
southPane.add(btnLogon);
+
centerPane.add(txtPassword);
southPane.add(btnReset);
+
this.getContentPane().add(centerPane, BorderLayout.CENTER);
this.getContentPane().add(southPane,BorderLayout.SOUTH);
+
                          //步骤3:以边界布局的方式,将面板整体添加到框架上
 +
 +
//B.创建第二个面板,内部采用流式布局,在其中添加用户登录和重置按钮
 +
JPanel southPane=new JPanel();
 +
southPane.setLayout(new FlowLayout());
 +
southPane.add(btnLogon);
 +
southPane.add(btnReset);
 +
this.getContentPane().add(southPane,BorderLayout.SOUTH);
 +
 +
pack();//从java.awt.Window继承而来的方法,调整窗口的大小,使其适应组件的大小和布局。
 +
this.setTitle("图书管理系统-用户登陆");
 +
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 +
this.setVisible(true);
 
 
pack();
 
this.setTitle("电商购物平台-登录");
 
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
this.setVisible(true);
 
 
}
 
}
 +
/*创建网格布局*/
 
public static void main(String[] args) {
 
public static void main(String[] args) {
LogonForm logon=new LogonForm();
+
UserForm userForm=new UserForm();
 +
 
}
 
}
 
}</nowiki>
 
}</nowiki>

2018年5月22日 (二) 07:51的版本

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

创建用户登录窗口,实现效果如图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();

程序代码

package main;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.*;

public class UserForm extends JFrame{
	private JLabel labelUser;//“登录账号”标签
	private JLabel labelPwd;//“登录密码”标签
	private JTextField txtUserID;//“登录账号”文本框
	private JPasswordField txtPassword;//密码框
	private JButton btnLogon;//登录按钮
	private JButton btnReset;//重置按钮
	
	public UserForm() {
		    super("图书管理系统用户登陆");				
		    this.setLayout(new BorderLayout());//为框架上的面板设立布局方式,设为边界布局
		    
			//0.控件初始化
			labelUser=new JLabel("用户登录",JLabel.CENTER);
			labelPwd= new JLabel("登录密码",JLabel.CENTER);
			txtUserID=new JTextField(16);
			txtPassword=new JPasswordField(16);
			btnLogon=new JButton("登录");
			btnReset=new JButton("重置");
			
			
			//A.创建第一个面板,内部采用网格布局,在其中添加用户登录、用户密码控件
			JPanel centerPane=new JPanel();
			centerPane.setLayout(new GridLayout(2,2));//步骤1:创建网格布局
			centerPane.add(labelUser);//步骤2:将组件添加到面板上
			centerPane.add(txtUserID);
			centerPane.add(labelPwd);
			centerPane.add(txtPassword);
			this.getContentPane().add(centerPane, BorderLayout.CENTER);
			                          //步骤3:以边界布局的方式,将面板整体添加到框架上
			
			//B.创建第二个面板,内部采用流式布局,在其中添加用户登录和重置按钮			
			JPanel southPane=new JPanel();
			southPane.setLayout(new FlowLayout());
			southPane.add(btnLogon);
			southPane.add(btnReset);
			this.getContentPane().add(southPane,BorderLayout.SOUTH);
			
			pack();//从java.awt.Window继承而来的方法,调整窗口的大小,使其适应组件的大小和布局。
			this.setTitle("图书管理系统-用户登陆");
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			this.setVisible(true);	
		
	}
	/*创建网格布局*/
	public static void main(String[] args) {
		UserForm userForm=new UserForm();
		
	}
}