Java组件的添加 方法一

来自CloudWiki
跳转至: 导航搜索

算法设计

1.构建JFrame

2.在JFrame上添加面板

3.在面板上添加组件。

实训步骤

创建类TestForm

 

public class TestForm{
        public TestForm(){
        }
	public static void main(String[] args) {
	
	}
}</nowiki>

在类中创建一个JFrame对象

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

添加两个面板(JPanel)

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

JPanel centerPane=new JPanel();userInfo.add(centerPane, BorderLayout.CENTER);
JPanel southPane=new JPanel(); userInfo.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 TestForm {
	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("注册");//重置按钮
	
	public TestForm(){
		JFrame userInfo= new JFrame();
	    userInfo.setTitle("电商购物平台——用户信息");//设置标题
	    
	    
	    JPanel centerPane=new JPanel();userInfo.add(centerPane, BorderLayout.CENTER);
	    
	    JPanel southPane=new JPanel(); userInfo.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);
    	
    	userInfo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置默认关闭时操作
	    userInfo.setResizable(false);//窗口大小不能改变
	    userInfo.setSize(300,150);//设置窗口大小
	    userInfo.setVisible(true);//设置窗口可见性
	    userInfo.setLocationRelativeTo(null);//将窗口设为中央
	    
    }
	public static void main(String[] args) {
		 TestForm t = new TestForm();
	}

}