【发布时间】:2015-11-21 08:57:01
【问题描述】:
package me.daniel.practice;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Switch
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Password Login System");
frame.setSize(400, 100);
frame.setResizable(false);
frame.setVisible(true);
frame.setBackground(Color.WHITE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
JPanel panel = new JPanel();
JLabel label = new JLabel("Enter Password: ");
JPasswordField pass = new JPasswordField(10);
pass.setEchoChar('*');
pass.addActionListener(new AL());
panel.add(label, BorderLayout.WEST);
panel.add(pass, BorderLayout.EAST);
frame.add(panel);
}
private static String password = "daniel";
static class AL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JPasswordField input = (JPasswordField) e.getSource();
char[] passy = input.getPassword();
String p = new String(passy);
if (p.equals(password))
{
JOptionPane.showMessageDialog(null, "Correct");
}
else
{
JOptionPane.showMessageDialog(null, "Incorrect");
}
}
}
}
我想要打开一个框架,在其中显示“输入密码:”的文本,在其右侧,您可以在其中输入密码的文本框。这种情况下的密码是“daniel”。
当您正确输入密码时,会弹出另一个窗口,提示您密码正确。如果不是,则会弹出一个不同的窗口,提示它不正确。但是,当我运行程序时,只显示框架而不显示框架内的实际内容。
【问题讨论】:
-
我刚刚执行了你的代码,它似乎工作正常(java 7)
-
可能与您的问题无关,因为线程安全问题引起的错误是随机且微妙的,但您确实应该create your Swing components on the EDT。
-
getPassword返回char数组是有原因的。为了良好的做法,不要使用String p = new String(passy);:P -
你的标题应该描述你的问题。
标签: java swing debugging jframe