用户登录
用户注册

分享至

jpasswordfield.clea

  • 作者: 榴莲鞋
  • 来源: 51数据库
  • 2020-04-21

一、关于JPasswordField

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class PasswordVerification extends JFrame implements ActionListener{

JLabel userLabel;// 定义用户标签提示

JLabel passwordLabel;// 定义密码标签提示

JTextField userText;// 定义用户文本框

JPasswordField passwordText;// 定义密码文本框

int count=1;// 统计输入信息的次数

public PasswordVerification() {

super("请输入信息");

Container container=getContentPane();// 得到容器对象container;

container.setLayout(new FlowLayout());// 设置默认布局

userLabel=new JLabel("用户名");// 创建用户标签

passwordLabel=new JLabel("密码");;// 创建密码标签

userText=new JTextField(15);;// 创建用户文本输入框

passwordText=new JPasswordField(15);// 创建密码输入文本框

//passwordText.setEchoChar('#');

passwordText.addActionListener(this);// 注册事件监听者;

container.add(userLabel);

container.add(userText);

container.add(passwordLabel);

container.add(passwordText);

setSize(240,100);

setVisible(true);

}

public void actionPerformed(ActionEvent e) {

// TODO 自动生成方法存根

String userName=new String("张三");// 假设为正确的用户名;

String password=new String("12345678");// 假设为正确的密码;

//e.getSource()==passwordText

if(true){

count++;

char[] passwords=passwordText.getPassword();

if(userText.getText().equals(userName)&&password.equals(new String(passwords)))

{

JOptionPane.showMessageDialog(null, "欢迎您:" + userName);

System.exit(0);

}

else if(count>3)

System.exit(0);

else{

JOptionPane.showMessageDialog(null,userText.getText()+"请输入正确信息");

//userText.setText(null);

//passwordText.setText(null);

}

}

}

public static void main(String args[]){

PasswordVerification pv=new PasswordVerification();

pv.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

二、JPasswordField

楼主可以试试这样啊.

import java.awt.BorderLayout;

import java.awt.Dimension;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JPasswordField;

import java.awt.Rectangle;

public class Frame1 extends JFrame {

JPanel contentPane;

JPasswordField jPasswordField1 = new JPasswordField();

public Frame1() {

try {

setDefaultCloseOperation(EXIT_ON_CLOSE);

jbInit();

} catch (Exception exception) {

exception.printStackTrace();

}

}

private void jbInit() throws Exception {

contentPane = (JPanel) getContentPane();

contentPane.setLayout(null);

setSize(new Dimension(400, 300));

setTitle("Frame Title");

jPasswordField1.setText("jPasswordField1");

jPasswordField1.setBounds(new Rectangle(136, 165, 127, 24));

contentPane.add(jPasswordField1);

}

}

三、java中JPasswordField如何在输入之后获取输入的值,就是已经创建了

给这个组件添加事件监听方法

监听器方法根据发送的动作来确定。

假设发生一个鼠标点击的动作,那么要给事件源添加鼠标的监听器方法。

假设你想让事件源获取焦点时,执行某一件事情,那么你就要给事件源添加焦点事件监听器方法。

addActionListener() ActionListener 动作监听器接口

addMouseListener() MouseListener 鼠标监听器接口

addMouseMotionListener() MouseMotionLIstener 鼠标移动监听器接口

addKeyListener() KeyListener 键盘监听器接口

你的情况可以添加动作监听器接口或者键盘监听器接口

代码如下:

JPasswordField jpf = new JPasswordField();//创建密码框

//设置事件监听

//实例化一个监听器,通过构造函数传参,该监听器是自定义的,定义方法参见下面的代码

ActionListener ac = new WriteActionListener(jpf);

//给密码框绑定监听事件

jpf.addActionListener(ac);

//实现了键盘事件监听器接口

public class WriteActionListener implements ActionListener {

private JPasswordField password;

public WriteActionListener(JPasswordField password){

this.password = password;

}

//实现接口中的抽象方法

public void actionPerformed(ActionEvent e) {

//得到输入的密码值

String password_value =(String) password.getText();

}

}

转载请注明出处51数据库 » jpasswordfield.clea

软件
前端设计
程序设计
Java相关