0

我是编程新手,并创建了一个小程序来练习代码。这是一个身份验证程序,您在其中输入用户名和密码,只有我指定的用户名和密码才能使其显示图片。我输入了所有代码,没有错误;但是当我运行它并输入用户名和密码时,它无法显示图片。这是我的代码。

package main.Swing.com;
//imports

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputMethodEvent;
import java.awt.event.InputMethodListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.util.EventObject;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

//class that carries other classes and carries some variables
public class Main extends javax.swing.JFrame implements ActionListener, TextListener, InputMethodListener {

    JButton Test = new JButton("TEST IF YOU HAVE ACCESS TO THIS");
    JButton Cancel = new JButton("CANCEL");
    JTextField username = new JTextField(15);
    JTextField password = new JTextField(15);
    String n = ("Nathan");
    int nathannam;
    int nathannamer;
    JButton superman;
    JButton supermanny;

//constructor class that has the jframe 
    public Main() {
        super("Authenticator");
        setSize(300, 220);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLookAndFeel();
//creating the pane and defining some labels    
        JPanel pane = new JPanel();
        JLabel UsernameLabel = new JLabel("Username: ");
        JLabel PasswordLabel = new JLabel("Password: ");
//adding all the components to the pane
        pane.add(UsernameLabel);
        pane.add(username);
        pane.add(PasswordLabel);
        pane.add(password);
        pane.add(Test);
        pane.add(Cancel);
//adding the pane
        add(pane);
//adding a event listener to my JButton titled Test
        Test.addActionListener(this);
//checking if the password variable and name variable are both 
//correct by taking the values assigned to each of them later
//on in the code and adding them together and if they add together
//to the correct amount it should display a button, but it doesn't
//which is the problem I am having
        if (nathannam + nathannamer == 24) {
            ImageIcon superman = new ImageIcon("JButton.png");
            JButton supermanny = new JButton(superman);
            pane.add(supermanny);
        }

//setting visibility to true
        setVisible(true);
//end of constructor class
    }
//start of class that checks if the username is correct

    public void METHODPREFORMED(ActionListener evt) {
        Object source = ((EventObject) evt).getSource();
//testing if username is equivalent to my name which is nathan
        if (source == Test) {
            String get = username.getText().toString();
            String notation = "Nathan";
//if it is equivalent a variable will be assigned to nathanam
            for (int i = 0; i < get.length(); i++) {
                if (get.substring(i) == notation) {
                    int nathannam = 14;
//if it is not it will assign a wrong variable to nathannam
                } else {
                    int nathannam = 15;
                }
            }
        }
    }

//testing if password variable is correct
    public void ACTIONPREFORMED(ActionListener evt) {

        Object source = ((EventObject) evt).getSource();
//testing if password is correct
        if (source == Test) {

            String got = password.getText().toString();
            String notition = "iamnathan";
//if it is equivalent a variable will be assigned to nathanamer
            for (int i = 0; i < got.length(); i++) {
                if (got.substring(i) == notition) {
                    int nathannamer = 10;
//if it is not equivalent nathannamer will be assigned a wrong variable
                } else {
                    int nathannamer = 15;
                }
            }

        }
    }   //setting the nimbus setlookandfeel that was implemented in java 7

    private static void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel(
                    "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
            );
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

//adding the main method
    public static void main(String[] args) {
        setLookAndFeel();
        Main main = new Main();
    }

    @Override
    public void textValueChanged(TextEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void caretPositionChanged(InputMethodEvent event) {
        // TODO Auto-generated method stub
    }

    @Override
    public void inputMethodTextChanged(InputMethodEvent event) {
        // TODO Auto-generated method stub
    }
}//end of program
4

1 回答 1

1

你永远不会从你的实现中调用你的测试方法actionPerformed()。这是您的程序的一个非常简化的版本,username当您单击 时会进行检查TestAccess

展望未来,请参阅如何使用密码字段以获取使用JPasswordField.

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

/**
 * @see
 */
public class Test {

    private final JTextField username = new JTextField(15);
    private final JButton test = new JButton("Test access");

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new GridLayout(0, 1));
        f.add(username);
        test.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("nathan".equalsIgnoreCase(username.getText()));
            }
        });
        f.add(test);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            new Test().display();
        });
    }
}
于 2014-08-23T01:24:55.953 回答