如果用戶名和密碼正確,則進行了登錄,可以打開圖片。我把它當做一個圖形用戶界面,但問題是,在我編寫用戶名和密碼並單擊登錄後,沒有任何反應。該項目工程在控制檯(沒有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」與實際用戶名或密碼進行比較 – SomeJavaGuy