【问题标题】:How can this java code display an output in BlueJ?这个 java 代码如何在 BlueJ 中显示输出?
【发布时间】:2022-01-25 19:10:26
【问题描述】:

下面的代码是一个图形用户界面,它有一个登录框,可以让用户输入他们的凭据。但是,当我运行代码时,它不会显示输出。有人可以帮忙吗?

        public void addComponentsToContainer() {
            container.add(userLabel);
            container.add(passwordLabel);
            container.add(userTextField);
            container.add(passwordField);
            container.add(showPassword);
            container.add(loginButton);
            container.add(resetButton);
        }
        public void addActionEvent() {
            loginButton.addActionListener(this);
            resetButton.addActionListener(this);
            showPassword.addActionListener(this);
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            //Coding Part of LOGIN button
            if (e.getSource() == loginButton) {
                String userText;
                String pwdText;
                userText = userTextField.getText();
                pwdText = passwordField.getText();
                if (userText.equalsIgnoreCase("admin") && pwdText.equalsIgnoreCase("12345")) {
                    JOptionPane.showMessageDialog( null, "Login Successful" );
                    Home obj= new Home();
                    obj.setVisible(true);
                   // setVisible(false);
                } else {
                    JOptionPane.showMessageDialog ( null, "Invalid Username or Password");
                }
            }
            //Coding Part of RESET button
            if (e.getSource() == resetButton) {
                userTextField.setText("");
                passwordField.setText("");
            }
           //Coding Part of showPassword JCheckBox
            if (e.getSource() == showPassword) {
                if (showPassword.isSelected()) {
                    passwordField.setEchoChar((char) 0);
                } else {
                    passwordField.setEchoChar('*');
                }
            } 
        }
    }

【问题讨论】:

    标签: java user-interface bluej


    【解决方案1】:

    在这种特殊情况下,您并没有真正显示足够的信息,这就是为什么 cmets 要求您提供 Minimal Reproducible Example。尽管如此,我还是会同意您正在使用 JPasswordField 组件这一事实。

    首先,出于安全原因,JPasswordField#getText() 方法自 Java 2 平台 v1.2 起已弃用,并替换为返回 char[] 的 JPasswordField#getPassword() 方法 输入的密码数组。尽管您可能仍然可以在您正在使用的 Java 平台上为此组件使用 getText() 方法进行编译,但您可能在实际运行代码时遇到问题。

    尝试改用JPasswordField#getPassword() 方法,看看是否会有所不同:

    String userText = userTextField.getText();
    String pwdText  = String.valueOf(passwordField.getPassword());
    if (userText.equalsIgnoreCase("admin") && pwdText.equalsIgnoreCase("12345")) {
    

    它应该会飞...也许...谁知道没有Minimal Reproducible Example。您是使用JPasswordField 还是使用带有DocumentFilter 的JTextField 来屏蔽输入的文本?

    也许您的代码正在运行,而您只是看不到消息框,因为您使用 null 作为 JOptionPane 的父级:

    JOptionPane.showMessageDialog( null, "Login Successful" );
    

    消息框对话框窗口位于您的应用程序窗口后面。如果应用程序窗口的setAlwaysOnTop() 属性设置为布尔值true,这是相当典型的。这可以给人以应用程序挂起的印象。给对话框一个父级...也许尝试loginButton 而不是null。

    附带说明:

    考虑散列密码,然后将散列与散列进行比较。您真的不应该将密码硬编码或存储为纯文本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-22
      • 2011-09-29
      • 1970-01-01
      • 2019-04-07
      • 2018-05-22
      • 2019-10-11
      相关资源
      最近更新 更多