Java组件的添加 方法二

来自CloudWiki
跳转至: 导航搜索

算法设计

1.构建JFrame

2.在JFrame上添加面板

3.在面板上添加组件。

实训步骤

创建JFrame的子类UserInfo


import java.awt.*;
import javax.swing.*;
public class UserInfo extends JFrame{
	public UserInfo() {
		super("电商购物平台-用户信息");
	}
	public static void main(String[] args) {
		UserInfo userInfo=new UserInfo();
	}
}

为UserInfo指定窗口大小

import java.awt.*;
import javax.swing.*;
public class UserInfo extends JFrame{
	public UserInfo() {
		super("电商购物平台-用户信息");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置默认关闭时操作
		setResizable(false);//窗口大小不能改变
		setSize(300,150);//设置窗口大小
		setVisible(true);//设置窗口可见性
                setLocationRelativeTo(null);//将窗口设为中央
	}
	public static void main(String[] args) {
		UserInfo userInfo=new UserInfo();
	}
}


添加两个面板(JPanel)

在构造方法中添加两个面板,以准备在上面添加组件

                JPanel centerPane=new JPanel();
		this.add(centerPane, BorderLayout.CENTER);
		JPanel southPane=new JPanel(); 
		this.add(southPane, BorderLayout.SOUTH);

添加输入框等组件

在类中添加labelUser、labelPwd等组件。

	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 JButton btnLogon =new JButton("登陆");//登录按钮
	private JButton btnSet = new JButton("注册");//重置按钮

在第一块面板中添加组件

   	    centerPane.setLayout(new GridLayout(2,2));//步骤1:创建网格布局   	
	    centerPane.add(labelUser);//步骤2:将组件添加到面板上
	    centerPane.add(txtUserID);		//centerPane.add(labelUser2);
	    centerPane.add(labelPwd);
	    centerPane.add(txtPassword);	

在第二块面板中添加组件

   	southPane.setLayout(new FlowLayout());
    	southPane.add(btnLogon);
    	southPane.add(btnSet);

完整代码

package task10;

import java.awt.*;

import javax.swing.*;
public class UserInfo 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 JButton btnLogon =new JButton("登陆");//登录按钮
	private JButton btnSet = new JButton("注册");//重置按钮
	
	
    UserInfo(){
    	setTitle("电商购物平台——用户信息");
    	
    	JPanel centerPane=new JPanel();this.add(centerPane, BorderLayout.CENTER);
    	JPanel southPane=new JPanel(); this.add(southPane, BorderLayout.SOUTH);
    	 centerPane.setLayout(new GridLayout(2,2));//步骤1:创建网格布局   	
 	    centerPane.add(labelUser);//步骤2:将组件添加到面板上
 	    centerPane.add(txtUserID);		//centerPane.add(labelUser2);
 	    centerPane.add(labelPwd);
 	    centerPane.add(txtPassword);	
 	    
 	    southPane.setLayout(new FlowLayout());
   	    southPane.add(btnLogon);
   	    southPane.add(btnSet);
   	    
   	    
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置默认关闭时操作
		setResizable(false);//窗口大小不能改变
		setSize(300,150);//设置窗口大小
		setVisible(true);//设置窗口可见性
		setLocationRelativeTo(null);//将窗口设为中央
    }
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		UserInfo userInfo=new UserInfo();
	}

}