【问题标题】:Need help to open a new frame in a frame需要帮助在一个框架中打开一个新框架
【发布时间】: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#getTextJPasswordField#getPassword 与实际的用户名或密码进行比较

标签: java user-interface


【解决方案1】:

试试这个

 if ((userText.getText()).equals(USERNAME)&& (passwordText.getPassword().toString()).equals(PASSWORD)) {

您应该使用 getText() 方法来比较值。在您的代码中,您使用的是 Object 类的 equals 方法而不是 String 类。

【讨论】:

  • JPasswordField#getText 已弃用,不应使用。
  • 这有两个问题:char[] 是一个数组,toString() 会返回垃圾,其次,如果我做对了,你将重新实现 getText() 被弃用的相同弱点.看我的回答。
【解决方案2】:

如果您查看文档,您会看到 JPasswordField 直接从 Object 继承 equals() 方法,其中指出:

Object类的equals方法实现了最区分 对象上可能的等价关系;也就是说,对于任何非空 参考值 x 和 y,此方法返回 true 当且仅当 x 和 y 引用同一个对象(x == y 的值为 true)。

JPasswordFieldString 显然不是这种情况。因此,您必须将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

【讨论】:

  • 为什么'Arrays.fill(passwordInput, '0');'后面不能写else语句
  • Else 语句与 'if' 相关联,因此它应该立即跟随它,在它之后留下 Arrays.fill。我已经更新了我的答案。
猜你喜欢
  • 2015-10-25
  • 1970-01-01
  • 1970-01-01
  • 2013-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-18
  • 2013-10-15
相关资源
最近更新 更多