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

来自CloudWiki
跳转至: 导航搜索
任务描述:购书系统登录窗体的设计
 
(未显示2个用户的15个中间版本)
第1行: 第1行:
 
==任务描述:购书系统登录窗体的设计==
 
==任务描述:购书系统登录窗体的设计==
创建用户登录窗口,实现效果如图8.1所示。
+
创建用户登录窗口,实现效果如图8.1所示
  
 
[[文件:java8-6.png]]
 
[[文件:java8-6.png]]
第8行: 第8行:
 
==任务实现==
 
==任务实现==
 
===实施思路===
 
===实施思路===
第一步:启动Eclipse,导入工程Project0801。
+
启动Eclipse,导入工程Project0801。
  
第二步:在工程中新建一个类,类名为LoginForm,继承类JFrame。
+
在工程中新建一个类,类名为UserForm ,继承类JFrame。
  
  <nowiki>public class LogonForm extends JFrame {
+
  <nowiki>package view;
}</nowiki>
+
import java.awt.*;
第三步:添加成员变量。
+
import javax.swing.*;
  
<nowiki>private JLabel lblUserID;//“登录账号”标签
+
public class UserForm extends JFrame{
private JLabel lblPassword;//“登录密码”标签
 
private JTextField txtUserID;//“登录账号”文本框
 
private JPasswordField txtPassword;//密码框
 
private JButton btnLogin;//登录按钮
 
private JButton btnReset;//重置按钮</nowiki>
 
  
第四步:创建无参构造方法,设置窗口的基本属性
+
public static void main(String[] args) {
 +
// TODO Auto-generated method stub
 +
 
 +
}
 +
 
 +
}
 +
</nowiki>
 +
 
 +
创建无参构造方法,设置窗口的基本属性
  
  <nowiki>public LogonForm() {
+
  <nowiki>public UserForm() {
pack();
+
this.setTitle("商苑面馆-用户登陆");
this.setTitle("电商购物平台-登录");
 
 
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
+
                this.setSize(300,150);
 +
this.setVisible(true);
 +
this.setLayout(new BorderLayout());//为框架上的面板设立布局方式,设为边界布局
 +
this.setLocationRelativeTo(null);//将窗口设为中央
 
}</nowiki>
 
}</nowiki>
  
第五步:在构造方法中,初始化成员变量。
+
为类添加成员变量。
 +
 
 +
<nowiki>
 +
private JLabel labelUser=new JLabel("用户登录",JLabel.CENTER);
 +
private JLabel labelPwd= new JLabel("登录密码",JLabel.CENTER);//“登录密码”标签
 +
private JTextField txtUserID = new JTextField(16);//“登录账号”文本框
 +
private JPasswordField txtPassword=new JPasswordField(16);//密码框
 +
private JLabel labelUser2=new JLabel("@126.com",JLabel.CENTER);
 +
        private JButton btnLogon =new JButton("登陆");//登录按钮
 +
private JButton btnReset = new JButton("重置");//重置按钮
 +
</nowiki>
  
<nowiki>lblUserID=new JLabel("用户登录",JLabel.CENTER);
+
在框架中,添加第一块面板。
lblPassword= new JLabel("登录密码",JLabel.CENTER);
 
txtUserID=new JTextField(16);
 
txtPassword=new JPasswordField(16);
 
btnLogon=new JButton("登录");
 
        btnReset=new JButton("重置");</nowiki>
 
  
第六步:在构造方法中,添加用户登录、用户密码控件。
+
[[文件:java2020-12-7.png|600px]]
  
  <nowiki>JPanel southPane=new JPanel();
+
  <nowiki> //A.创建第一个面板,内部采用网格布局,在其中添加用户登录、用户密码控件
southPane.setLayout(new FlowLayout());
+
JPanel centerPane=new JPanel();
southPane.add(btnLogon);
+
centerPane.setLayout(new GridLayout(2,2));//步骤1:创建网格布局
southPane.add(btnReset);
+
this.getContentPane().add(southPane,BorderLayout.SOUTH);</nowiki>
+
centerPane.add(labelUser);//步骤2:将组件添加到面板上
 +
centerPane.add(txtUserID);
 +
centerPane.add(labelPwd);
 +
centerPane.add(txtPassword);
 +
this.add(centerPane, BorderLayout.CENTER);
 +
//步骤3:以边界布局的方式,将面板整体添加到框架上
 +
</nowiki>
 +
 
 +
第七步:在框架中,添加第二块面板。
 +
 
 +
[[文件:java2020-12-8.png|600px]]
  
 +
<nowiki>//B.创建第二个面板,内部采用流式布局,在其中添加用户登录和重置按钮
 +
JPanel southPane=new JPanel();
 +
southPane.setLayout(new FlowLayout());     
 +
southPane.add(btnLogon);
 +
southPane.add(btnReset);
 +
this.add(southPane,BorderLayout.SOUTH);</nowiki>
 
第六步:测试程序,在main方法中添加语句。
 
第六步:测试程序,在main方法中添加语句。
  
  LogonForm logon=new LogonForm();
+
  UserForm userForm=new UserForm();
  
 
==程序代码==
 
==程序代码==
  <nowiki>public class LogonForm extends JFrame {
+
  <nowiki>
private JLabel lblUserID;//“登录账号”标签
+
package main;
private JLabel lblPassword;//“登录密码”标签
+
import java.awt.*;
private JTextField txtUserID;//“登录账号”文本框
+
import java.awt.event.ActionEvent;
private JPasswordField txtPassword;//密码框
+
import java.awt.event.ActionListener;
private JButton btnLogon;//登录按钮
+
 
private JButton btnReset;//重置按钮
+
import javax.swing.*;
 +
 
 +
public class UserForm extends JFrame{
 +
private JLabel labelUser=new JLabel("用户登录",JLabel.CENTER);
 +
private JLabel labelPwd= new JLabel("登录密码",JLabel.CENTER);//“登录密码”标签
 +
private JTextField txtUserID = new JTextField(16);//“登录账号”文本框
 +
private JPasswordField txtPassword=new JPasswordField(16);//密码框
 +
private JLabel labelUser2=new JLabel("@126.com",JLabel.CENTER);
 +
        private JButton btnLogon =new JButton("登陆");//登录按钮
 +
private JButton btnReset = new JButton("重置");//重置按钮
 
 
public LogonForm() {
+
public UserForm() {
//控件初始化
+
this.setTitle("商苑面馆-用户登陆");
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();
 
JPanel centerPane=new JPanel();
centerPane.setLayout(new GridLayout(2,2));
+
centerPane.setLayout(new GridLayout(2,2));//步骤1:创建网格布局
centerPane.add(lblUserID);
+
 
 +
centerPane.add(labelUser);//步骤2:将组件添加到面板上
 
centerPane.add(txtUserID);
 
centerPane.add(txtUserID);
centerPane.add(lblPassword);
+
centerPane.add(labelPwd);
 
centerPane.add(txtPassword);
 
centerPane.add(txtPassword);
this.getContentPane().add(centerPane, BorderLayout.CENTER);
+
this.add(centerPane, BorderLayout.CENTER);
//添加登录按钮和重置按钮
+
//步骤3:以边界布局的方式,将面板整体添加到框架上
 +
 +
//B.创建第二个面板,内部采用流式布局,在其中添加用户登录和重置按钮
 
JPanel southPane=new JPanel();
 
JPanel southPane=new JPanel();
southPane.setLayout(new FlowLayout());
+
southPane.setLayout(new FlowLayout());      
 
southPane.add(btnLogon);
 
southPane.add(btnLogon);
southPane.add(btnReset);
+
southPane.add(btnReset);
this.getContentPane().add(southPane,BorderLayout.SOUTH);
+
this.add(southPane,BorderLayout.SOUTH);
 
 
pack();
 
this.setTitle("电商购物平台-登录");
 
 
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
+
                this.setSize(300,150);
 +
this.setVisible(true);
 +
this.setLayout(new BorderLayout());//为框架上的面板设立布局方式,设为边界布局
 +
this.setLocationRelativeTo(null);//将窗口设为中央
 
}
 
}
 +
 +
 
public static void main(String[] args) {
 
public static void main(String[] args) {
LogonForm logon=new LogonForm();
+
// TODO Auto-generated method stub
 +
UserForm userForm=new UserForm();
 
}
 
}
}</nowiki>
+
 
 +
}
 +
 
 +
</nowiki>

2020年6月3日 (三) 01:03的最新版本

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

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

Java8-6.png

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

任务实现

实施思路

启动Eclipse,导入工程Project0801。

在工程中新建一个类,类名为UserForm ,继承类JFrame。

package view;
import java.awt.*;
import javax.swing.*;

public class UserForm extends JFrame{

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

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

public UserForm() {
		this.setTitle("商苑面馆-用户登陆");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                this.setSize(300,150);
		this.setVisible(true);								
		this.setLayout(new BorderLayout());//为框架上的面板设立布局方式,设为边界布局
		this.setLocationRelativeTo(null);//将窗口设为中央
	}

为类添加成员变量。

	private JLabel labelUser=new JLabel("用户登录",JLabel.CENTER);
	private JLabel labelPwd= new JLabel("登录密码",JLabel.CENTER);//“登录密码”标签
	private JTextField txtUserID = new JTextField(16);//“登录账号”文本框
	private JPasswordField txtPassword=new JPasswordField(16);//密码框
	private JLabel labelUser2=new JLabel("@126.com",JLabel.CENTER);
        private JButton btnLogon =new JButton("登陆");//登录按钮
	private JButton btnReset = new JButton("重置");//重置按钮

在框架中,添加第一块面板。

Java2020-12-7.png

		//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.add(centerPane, BorderLayout.CENTER);
			 //步骤3:以边界布局的方式,将面板整体添加到框架上
			

第七步:在框架中,添加第二块面板。

Java2020-12-8.png

//B.创建第二个面板,内部采用流式布局,在其中添加用户登录和重置按钮			
	JPanel southPane=new JPanel();
	southPane.setLayout(new FlowLayout());       
	southPane.add(btnLogon);
	southPane.add(btnReset);
	this.add(southPane,BorderLayout.SOUTH);

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

UserForm userForm=new UserForm();

程序代码

package main;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class UserForm extends JFrame{
	private JLabel labelUser=new JLabel("用户登录",JLabel.CENTER);
	private JLabel labelPwd= new JLabel("登录密码",JLabel.CENTER);//“登录密码”标签
	private JTextField txtUserID = new JTextField(16);//“登录账号”文本框
	private JPasswordField txtPassword=new JPasswordField(16);//密码框
	private JLabel labelUser2=new JLabel("@126.com",JLabel.CENTER);
        private JButton btnLogon =new JButton("登陆");//登录按钮
	private JButton btnReset = new JButton("重置");//重置按钮
	
	public UserForm() {
		this.setTitle("商苑面馆-用户登陆");		
		
		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.add(centerPane, BorderLayout.CENTER);
		 //步骤3:以边界布局的方式,将面板整体添加到框架上
		
		//B.创建第二个面板,内部采用流式布局,在其中添加用户登录和重置按钮			
		JPanel southPane=new JPanel();
		southPane.setLayout(new FlowLayout());       
		southPane.add(btnLogon);
		southPane.add(btnReset);	
		this.add(southPane,BorderLayout.SOUTH);
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                this.setSize(300,150);
		this.setVisible(true);								
		this.setLayout(new BorderLayout());//为框架上的面板设立布局方式,设为边界布局
		this.setLocationRelativeTo(null);//将窗口设为中央
	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		UserForm userForm=new UserForm();
	}

}