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

来自CloudWiki
跳转至: 导航搜索
第45行: 第45行:
 
private JLabel labelUser2=new JLabel("@126.com",JLabel.CENTER);
 
private JLabel labelUser2=new JLabel("@126.com",JLabel.CENTER);
 
         private JButton btnLogon =new JButton("登陆");//登录按钮
 
         private JButton btnLogon =new JButton("登陆");//登录按钮
private JButton btnReset = new JButton("注册");//重置按钮
+
private JButton btnReset = new JButton("重置");//重置按钮
 
</nowiki>
 
</nowiki>
  
第87行: 第87行:
 
private JLabel labelUser2=new JLabel("@126.com",JLabel.CENTER);
 
private JLabel labelUser2=new JLabel("@126.com",JLabel.CENTER);
 
     private JButton btnLogon =new JButton("登陆");//登录按钮
 
     private JButton btnLogon =new JButton("登陆");//登录按钮
private JButton btnSet = new JButton("注册");//重置按钮
+
private JButton btnSet = new JButton("重置");//重置按钮
 
 
 
 

2019年5月19日 (日) 07:04的版本

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

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

Java8-6.png

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

任务实现

实施思路

启动Eclipse,导入工程Project0801。

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

package view;

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,200);
		this.setVisible(true);	
	}

在构造方法中,初始化成员变量。

super("图书管理系统用户登陆");				
	 this.setLayout(new BorderLayout());//为框架上的面板设立布局方式,设为边界布局
	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("重置");//重置按钮

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

		//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:以边界布局的方式,将面板整体添加到框架上
			

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

//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();

程序代码

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

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 btnSet = new JButton("重置");//重置按钮
	
	
    public UserForm(){
    	JPanel centerPane=new JPanel();
    	centerPane.setLayout(new GridLayout(2,3));//步骤1:创建网格布局   	
		centerPane.add(labelUser);//步骤2:将组件添加到面板上
		centerPane.add(txtUserID);
		centerPane.add(labelUser2);
		centerPane.add(labelPwd);
		centerPane.add(txtPassword);		
		this.add(centerPane, BorderLayout.CENTER);
    	
		
    	JPanel southPane=new JPanel();    	
    	southPane.setLayout(new FlowLayout());
    	southPane.add(btnLogon);
    	southPane.add(btnSet);
    	this.add(southPane, BorderLayout.SOUTH);
    	
    	
    	this.setTitle("电商管理pingt");
    	this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    	this.setSize(300,200);
    	this.setVisible(true);
    	
    }
	public static void main(String[] args) {
		// TODO Auto-generated method stub
        UserForm u= new UserForm();
	}

}