java实现输入密码GUI
时间: 2023-12-10 14:04:53 浏览: 71
可以使用Java Swing库来实现输入密码GUI。以下是一个简单的示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PasswordInputGUI extends JFrame {
private JPasswordField passwordField;
public PasswordInputGUI() {
setTitle("输入密码");
setSize(300, 150);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
JLabel label = new JLabel("请输入密码:");
passwordField = new JPasswordField();
passwordField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String password = new String(passwordField.getPassword());
JOptionPane.showMessageDialog(null, "您输入的密码是:" + password);
}
});
panel.add(label, BorderLayout.NORTH);
panel.add(passwordField, BorderLayout.CENTER);
add(panel);
setVisible(true);
}
public static void main(String[] args) {
new PasswordInputGUI();
}
}
```
在上述代码中,我们创建了一个JPasswordField对象并加入到一个JPanel面板中,最后将该面板加入到JFrame窗口中。当用户输入密码后,按下回车键即可触发监听器,弹出一个JOptionPane对话框,显示用户输入的密码。
阅读全文