Java Swing界面编程(19)---密码输入组件:JPasswordField

JTextField是使用明文方式进行数据显示的,如果现在需要将回显的内容设置成其他字符,则可以使用JPasswordField类。

package com.beyole.util;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;

public class test18 {
	public static void main(String[] args) {
		JFrame frame = new JFrame("Crystal");// 实例化窗体对象
		JPasswordField jPasswordField1 = new JPasswordField();// 定义密文框
		JPasswordField jPasswordField2 = new JPasswordField();// 定义密文框
		jPasswordField2.setEchoChar('#');// 设置回显字符为#
		JLabel label1 = new JLabel("默认的回显:");
		JLabel label2 = new JLabel("设置的回显#:");
		label1.setBounds(10, 10, 100, 20);
		label2.setBounds(10, 40, 100, 20);
		jPasswordField1.setBounds(110, 10, 80, 20);
		jPasswordField2.setBounds(110, 40, 50, 20);
		frame.setLayout(null);
		frame.add(label2);
		frame.add(label1);
		frame.add(jPasswordField2);
		frame.add(jPasswordField1);
		frame.setSize(300, 100);
		frame.s
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以给你提供一个简单的Java代码,使用Swing库来实现一个GUI界面,模拟QQ登录界面。代码如下所示: ``` import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class QQLogin extends JFrame implements ActionListener { private JTextField textFieldUsername; private JPasswordField passwordField; public QQLogin() { super("QQ登录"); setSize(300, 200); setResizable(false); setLocationRelativeTo(null); JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints constraints = new GridBagConstraints(); constraints.insets = new Insets(5, 5, 5, 5); // 用户名标签和输入JLabel labelUsername = new JLabel("用户名:"); constraints.gridx = 0; constraints.gridy = 0; panel.add(labelUsername, constraints); textFieldUsername = new JTextField(15); constraints.gridx = 1; constraints.gridy = 0; panel.add(textFieldUsername, constraints); // 密码标签和输入JLabel labelPassword = new JLabel("密码:"); constraints.gridx = 0; constraints.gridy = 1; panel.add(labelPassword, constraints); passwordField = new JPasswordField(15); constraints.gridx = 1; constraints.gridy = 1; panel.add(passwordField, constraints); // 登录按钮 JButton buttonLogin = new JButton("登录"); buttonLogin.addActionListener(this); constraints.gridx = 1; constraints.gridy = 2; panel.add(buttonLogin, constraints); add(panel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("登录")) { String username = textFieldUsername.getText(); String password = new String(passwordField.getPassword()); // 在这里进行用户名和密码的验证 if (username.equals("admin") && password.equals("123456")) { JOptionPane.showMessageDialog(this, "登录成功!"); } else { JOptionPane.showMessageDialog(this, "用户名或密码错误!"); } } } public static void main(String[] args) { new QQLogin(); } } ``` 这个代码会创建一个窗口,包含一个用户名输入框、一个密码输入框和一个登录按钮。当用户点击登录按钮时,程序会获取输入框中的用户名和密码,并进行用户名和密码的验证。如果验证通过,程序会弹出一个提示框,提示用户登录成功;否则,程序会弹出一个提示框,提示用户用户名或密码错误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值