【发布时间】:2015-09-07 10:23:27
【问题描述】:
如果用户名和密码正确,则登录后会打开图片。我将其作为 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 语句,即使用户名和密码是正确的。
【问题讨论】:
-
您正在对一个图形组件调用
equals方法,该方法使用Object的标准equals。您需要将JTextField#getText或JPasswordField#getPassword与实际的用户名或密码进行比较
标签: java user-interface