Swing中常用组件

1 文本框

JTextField类的构造方法及常用方法如下。
JTextField类构造方法
  
JTextField()创建内容空的文本框
JTextField(Document doc ,String text,int columns)创建指定文本内容存储模型和列数的文本框
JTextField(int columns)创建指定列数的文本框
JTextField(String Text)创建指定文本内容的文本框
JTextField(String Text,int columns)创建指定文本内容和列数的文本框

常用操作方法
public int getColumns() 
public String getText()
public Document getDocument()
public void setColumns(int columns)
public void setText(String t)
public void setDocument(Document doc)
JTextField可以在用户按下enter键后触发ActionEvent事件,其中注册和注销监听器的方法如下:
public void addActionListener(ActionListener l)
public void removeActionListener(ActionListener l)

2 密码框

JPasswordField类的构造方法及常用方法如下。
JPasswordFiled类构造方法
JPasswordField()构造一个新的内容为null 默认列数为0 具有默认文档的对象
JPasswordField(Document doc,String text,int columns)构造指定内容列数文档的对象
JPasswordField(int columns)构造指定列数的对象
JPasswordField(String text)构造指定内容的对象
JPasswordField(String text,int columns)构造指定内容和列数的对象

JPasswordField继承自JTextField类,所有它拥有JTextField类的方法,还有以下自身的方法:

public boolean echoCharIsSet()  检测密码框是否具有回显设置的字符
public char  setechoChar()   设置回显字符
public char  getechoChar()  获得回显字符默认为*

public char[] getPassword() 以字符数组的方式返回密码框实际文本内容



package swingdmeo;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class LoginTest extends JFrame implements ActionListener {
   
	private JPanel  jp = new JPanel();
	private JButton jb1 = new JButton("登录");
	private JButton jb2 = new JButton("重置");
	private JButton jb[] = {jb1,jb2};
	private JLabel name = new JLabel("请输入用户名");
	private JLabel password = new JLabel("请输入密码");
	private JLabel [] jl = {name,password};
	private JLabel show = new JLabel();
	
	
	private JTextField jName = new JTextField();
	private JPasswordField jPassword = new JPasswordField();

	
	public LoginTest(){
		jp.setLayout(null);//空布局
		//添加资源和监听器
		for (int i = 0;i<jb.length;i++)
		{
			jl[i].setBounds(30,20+40*i,180,20);
			jb[i].setBounds(30+110*i,100,80,20);
			jp.add(jb[i]);//
			jp.add(jl[i]);
			jb[i].addActionListener(this);
			
		}
		jName.setBounds(130,15,100,20);
		jp.add(jName);
		jName.addActionListener(this);
		jPassword.setBounds(130,60,100,20);
		jPassword.setEchoChar('*');
		jp.add(jPassword);
		jPassword.addActionListener(this);
		
		show.setBounds(10,180,270,20);
		jp.add(show);
		this.add(jp);
		//窗体设置
		this.setTitle("登入窗口");
		this.setBounds(300,300,270,250);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new LoginTest();
	}

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource() == jName)
		{
			jPassword.requestFocus();
		}
		else
			if(e.getSource() == jb[1])
			{
				show.setText("");
				jName.setText("");
				jPassword.setText("");
				jName.requestFocus();
			}
			else
			{
				//if(jName.getText().equals("zhangsan")&&jPassword.getPassword().equals("123456"))   
				//前面返回字符串  后面返回字符数组 要把字符数组转换为字符串   
				if(jName.getText().equals("zhangsan")&&String.valueOf(jPassword.getPassword()).equals("123456"))
				{
					show.setText("登录成功!");
				}
				else
				{
					show.setText("用户名或密码错误");
				}
					
			}
	}

}










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值