如果用户名和密码正确,则进行了登录以打开图片。我把它作为一个GUI,但问题是,在我写了用户名和密码并点击登录后没有任何反应。该程序在控制台中工作(没有GUI)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.ImageIcon;
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;
public class LoginView {
private static final String IMG_FILE_PATH = "index.jpg";
private static final String USERNAME = "Hudhud";
private static final String PASSWORD = "123";
public static void main(String[] args) {
JFrame frame = new JFrame("Login");
frame.setSize(300, 160);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel userLabel = new JLabel("User");
userLabel.setBounds(10, 10, 80, 25);
panel.add(userLabel);
final JTextField userText = new JTextField(20);
userText.setBounds(100, 10, 160, 25);
panel.add(userText);
JLabel passwordLabel = new JLabel("Password");
passwordLabel.setBounds(10, 40, 80, 25);
panel.add(passwordLabel);
final JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(100, 40, 160, 25);
panel.add(passwordText);
JButton loginButton = new JButton("login");
loginButton.setBounds(10, 80, 80, 25);
panel.add(loginButton);
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (userText.equals(USERNAME)&& passwordText.equals(PASSWORD)) {
URL url = LoginView.class.getResource(IMG_FILE_PATH);
ImageIcon icon = new ImageIcon(url);
JFrame f = new JFrame();
f.setSize(300, 300);
JLabel label = new JLabel(icon);
f.add(label);
f.setVisible(true);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
} else {
}
}
});
}
}
编辑:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.Arrays;
import javax.swing.ImageIcon;
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;
public class LoginView
{
private static final String IMG_FILE_PATH = "index.jpg";
private static final String USERNAME = "Hudhud";
private static final String PASSWORD = "123";
public static void main(String[] args)
{
JFrame frame = new JFrame("Login");
frame.setSize(300, 160);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel)
{
panel.setLayout(null);
JLabel userLabel = new JLabel("User");
userLabel.setBounds(10, 10, 80, 25);
panel.add(userLabel);
final JTextField userText = new JTextField(20);
userText.setBounds(100, 10, 160, 25);
panel.add(userText);
JLabel passwordLabel = new JLabel("Password");
passwordLabel.setBounds(10, 40, 80, 25);
panel.add(passwordLabel);
final JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(100, 40, 160, 25);
panel.add(passwordText);
JButton loginButton = new JButton("login");
loginButton.setBounds(10, 80, 80, 25);
panel.add(loginButton);
loginButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if ((userText.getText()).equals(USERNAME)&& (passwordText.getPassword().toString()).equals(PASSWORD))
{
URL url = LoginView.class.getResource(IMG_FILE_PATH);
ImageIcon icon = new ImageIcon(url);
JFrame frame = new JFrame();
frame.setSize(300, 300);
JLabel label = new JLabel(icon);
frame.add(label);
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
}
else {
JOptionPane.showMessageDialog(null, "You ain't the boss");
}
}
});
}
}
我收到此错误但我不知道如何修复它。当我写用户名和密码时,它会跳过if语句并给我else语句,即使用户名和密码是正确的。
答案 0 :(得分:1)
试试这个
if ((userText.getText()).equals(USERNAME)&& (passwordText.getPassword().toString()).equals(PASSWORD)) {
您应该使用getText()方法来比较值。在您的代码中,您使用的是Object类的equals方法而不是String类。
答案 1 :(得分:0)
如果您查看文档,就会直接从JPasswordField
看到equals()
继承Object
方法,其中声明:
类Object的equals方法实现最具辨别力 对象可能的等价关系;也就是说,对于任何非null 引用值x和y,当且仅当x时,此方法返回true 和y引用相同的对象(x == y的值为true)。
JPasswordField
和String
显然不是这种情况。因此,您必须将JPasswordField
的内容与给定的String
进行比较。
如果再次查看文档,您会看到从用于输入文本的某个组件获取文本的常用方法,出于安全原因,不推荐使用getText()
。确切的原因与String
是一个对象的事实有关,它会留在内存中,直到垃圾清理器启动,而这不是我们想要的。我们想要在我们不需要它时立即从内存中删除密码,所以正确的方法是使用char数组,我们可以通过它更多地控制它。正确的方法是:
char[] passwordInput = passwordText.getPassword();
if ((userText.getText()).equals(USERNAME)&&
(Arrays.equals(password.toCharArray(), passwordInput)) {
//do something
} else {
//do something else, e. g. inform the user about the error
}
Arrays.fill(passwordInput, '0'); //clearing out password from memory