[的Java Swing] JPasswordField中使用Java – JPasswordField中使用Java

内容
创建JPasswordField中
获取的事实,并从JPasswordField中的密码

JPasswordField中的对象允许我们输入了一行字像 JTextField的 但隐藏星号 (*) 或点创建密码 (密码). JPasswordField中经常一起使用以创建一对的JTextField 用户名密码 如下所示:

在JPasswordField中的Java Swing

现在,我们将通过建设JPasswordField中的程序如上.

创建JPasswordField中

我们有许多方法通常用于初始化JPasswordField中:
– JPasswordField中(): 初始化JPasswordField中没有任何文字和宽度 0 职位
– JPasswordField中(INT列): 初始化JPasswordField中没有文本列和列宽度
– JPasswordField中(文本字符串): 初始化原文Voit的JPasswordField中
– JPasswordField中(文本字符串, INT列): 初始化JPasswordField中Voit的原始文本列和列宽

首先,我们将创建的程序的接口. 接口包括 1 的JPanel 主面板中设置 BorderLayout的 包含输入面板和ButtonPanel. 输入面板放一个JPanel GridLayout的 含有 2 的JLabel, 1 JTextField的nHAP颗粒用户名, 1 JPasswordField中输入密码. ButtonPanel包含 2 JButton的 是btnLogin和btnHelp.

package nguyenvanquan7826.JPasswordField;

import java.awt.BorderLayout;
import java.awt.GridLayout;
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;
import javax.swing.border.EmptyBorder;

public class MyJPasswordField extends JFrame implements ActionListener {
	private JTextField tfUserName;
	private JPasswordField pf;
	private JButton btnLogin, btnHelp;

	public MyJPasswordField() {
		// add main panel to frame
		add(createMainPanel());

		// set display
		setTitle("JPasswordField");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
		setLocationRelativeTo(null);
		setVisible(true);
	}

	// create main panel containes input panel and button panel
	private JPanel createMainPanel() {
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBorder(new EmptyBorder(10, 10, 10, 10));
		panel.add(createInputPanel(), BorderLayout.CENTER);
		panel.add(createButtonPanel(), BorderLayout.PAGE_END);
		return panel;
	}

	// create input panel
	private JPanel createInputPanel() {
		int col = 15; // column of JTextField and JPasswordField
		JPanel panel = new JPanel(new GridLayout(2, 2, 5, 5));
		panel.add(createLabel("User name:"));
		panel.add(tfUserName = createTextField(col));
		panel.add(createLabel("Password:"));
		panel.add(pf = createPasswordField(col));
		return panel;
	}

	// create button panel with button login and button help
	private JPanel createButtonPanel() {
		JPanel panel = new JPanel();
		panel.add(btnLogin = createButton("Login"));
		panel.add(btnHelp = createButton("Help"));
		return panel;
	}

	// create a JLabel with text
	private JLabel createLabel(String text) {
		JLabel lb = new JLabel(text);
		return lb;
	}

	// create JTextField with column is col
	private JTextField createTextField(int col) {
		JTextField tf = new JTextField(col);
		return tf;
	}

	// create JPasswordField with column is col
	private JPasswordField createPasswordField(int col) {
		JPasswordField pf = new JPasswordField(col);
		return pf;
	}

	// create JButton with text is btnName and add ActionListener
	private JButton createButton(String btnName) {
		JButton btn = new JButton(btnName);
		btn.addActionListener(this);
		return btn;
	}

	@Override
	public void actionPerformed(ActionEvent e) {

	}

	public static void main(String[] args) {
		new MyJPasswordField();
	}
}

我们的下一个任务是创建并捕获为用户名和密码的事件,并与原始数据进行比较,以显示消息日志成败.

获取的事实,并从JPasswordField中的密码

为了捕捉JPasswordField中的类似JButton的事件, 我们需要执行 addActionListener方法 它. 另一点是,它往往是 setActionCommand 它类似于ActionCommand btnLogin完成时,按Enter键输入密码,就可以使这个过程甚至不需要费力点击btnLogin. 使用方法 getPassword来() 检索在JPasswordField中的密码. 这个方法返回一个字符数组作为密码字符串中的字符. 我们可以使用该方法 的getText() 但建议不要使用更多.

我们需要更新的方法 createPasswordField和createButton 如下:

// create JPasswordField with column is col
private JPasswordField createPasswordField(String action, int col) {
	JPasswordField pf = new JPasswordField(col);
	// set actionCommand for JPasswordField
	pf.setActionCommand(action);
	// add action for JPasswordField
	pf.addActionListener(this);
	return pf;
}

// create JButton with text is btnName and add ActionListener
private JButton createButton(String action, String btnName) {
	JButton btn = new JButton(btnName);
	btn.setActionCommand(action);
	btn.addActionListener(this);
	return btn;
}

最后,只有剩余的数据处理. 注意,显示,我们所使用的信息 JOptionPane的 显示一个对话框通知.

在JPasswordField中的Java Swing

完成下面的代码

package nguyenvanquan7826.JPasswordField;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class MyJPasswordField extends JFrame implements ActionListener {
	private JTextField tfUserName;
	private JPasswordField pf;
	private JButton btnLogin, btnHelp;
	String actionLogin = "login";
	String actionHelp = "help";

	public MyJPasswordField() {
		// add main panel to frame
		add(createMainPanel());

		// set display
		setTitle("JPasswordField");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
		setLocationRelativeTo(null);
		setVisible(true);
	}

	// create main panel containes input panel and button panel
	private JPanel createMainPanel() {
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBorder(new EmptyBorder(10, 10, 10, 10));
		panel.add(createInputPanel(), BorderLayout.CENTER);
		panel.add(createButtonPanel(), BorderLayout.PAGE_END);
		return panel;
	}

	// create input panel
	private JPanel createInputPanel() {
		int col = 15; // column of JTextField and JPasswordField
		JPanel panel = new JPanel(new GridLayout(2, 2, 5, 5));
		panel.add(createLabel("User name:"));
		panel.add(tfUserName = createTextField(col));
		panel.add(createLabel("Password:"));
		panel.add(pf = createPasswordField(actionLogin, col));
		return panel;
	}

	// create button panel with button login and button help
	private JPanel createButtonPanel() {
		JPanel panel = new JPanel();
		panel.add(btnLogin = createButton(actionLogin, "Login"));
		panel.add(btnHelp = createButton(actionHelp, "Help"));
		return panel;
	}

	// create a JLabel with text
	private JLabel createLabel(String text) {
		JLabel lb = new JLabel(text);
		return lb;
	}

	// create JTextField with column is col
	private JTextField createTextField(int col) {
		JTextField tf = new JTextField(col);
		return tf;
	}

	// create JPasswordField with column is col
	private JPasswordField createPasswordField(String action, int col) {
		JPasswordField pf = new JPasswordField(col);
		// set actionCommand for JPasswordField
		pf.setActionCommand(action);
		// add action for JPasswordField
		pf.addActionListener(this);
		return pf;
	}

	// create JButton with text is btnName and add ActionListener
	private JButton createButton(String action, String btnName) {
		JButton btn = new JButton(btnName);
		btn.setActionCommand(action);
		btn.addActionListener(this);
		return btn;
	}

	// check password
	private boolean checkData(String inputUserName, char[] inputPassword) {
		String userName = "nguyenvanquan";
		char[] password = { '7', '8', '2', '6' };
		return (Arrays.equals(inputPassword, password) && userName
				.equals(inputUserName));
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();

		// enter on JPasswordField or click button Login to Login
		if (actionLogin.equals(command)) {
			if (checkData(tfUserName.getText(), pf.getPassword())) {
				JOptionPane.showMessageDialog(null, "login success!", "login",
						JOptionPane.INFORMATION_MESSAGE);
			} else {
				JOptionPane.showMessageDialog(null, "login failed, Try again!",
						"login", JOptionPane.ERROR_MESSAGE);
			}
			return;
		}
		// click button help
		if (actionHelp.equals(command)) {
			JOptionPane.showMessageDialog(null, "get user name and password in"
					+ "n" + "cachhoc.net", "help",
					JOptionPane.INFORMATION_MESSAGE);
			return;
		}
	}

	public static void main(String[] args) {
		new MyJPasswordField();
	}
}

阅读更多: 类JPasswordField中, 使用JPasswordField中