-1

im a newbie on this, ive been watching videos to practice in Java GUI's, what im trying to do is execute a login gui and when it successfully enters the username and password, it goes to another window.

public class login implements ActionListener{

private static JLabel label;
private static JTextField userText;
private static JLabel passwordLabel;
private static JPasswordField passwordText;
private static JButton button;
private static JLabel test;

public static void main(String[] args) {
    

    JPanel panel = new JPanel();
    JFrame frame = new JFrame();
    frame.setSize(350,200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ImageIcon image = new ImageIcon("PICTURE.png");
    frame.setIconImage(image.getImage());
    frame.setTitle("Login : TITLEHERE");
    frame.setResizable(false);
    frame.add(panel);
    
    panel.setLayout(null);
    
    label = new JLabel("User");
    label.setBounds(10, 20, 80, 25);
    panel.add(label);
    
    userText = new JTextField(20);
    userText.setBounds(100,20,165,25);
    panel.add(userText);
    
    
    passwordLabel = new JLabel ("Password");
    passwordLabel.setBounds(10,50,80,25);
    panel.add(passwordLabel);
    
    passwordText = new JPasswordField();
    passwordText.setBounds(100, 50, 165, 25);
    panel.add(passwordText);
    
    button = new JButton("Login");
    button.setBounds(10, 80, 80 , 25);
    button.addActionListener(new login());
    panel.add(button);
    
    test = new JLabel("");
    test.setBounds(10,110,300,25);
    panel.add(test);
    
    frame.setVisible(true);}

the problem i have is when i try to access the frame from the main, it says "Frame cannot be resolved." It works on other videos but it doesnt work for mine. heres my actionPerformed code:

@Override
    public void actionPerformed(ActionEvent e) {
        String user = userText.getText();
        String password = passwordText.getText();
        System.out.println(user + ", " +password);
        if(user.equals("NAME") && password.equals("LASTNAME")) {
            test.setText("Login successful!");//no spaces and all capital for success
            myFrame home = new myFrame();
            frame.setVisible(false);// this,
            frame.discard();// - and this does not work.
        }
        else {
            test.setText("Login Failed");
        }

}
Hovercraft Full Of Eels
  • 280,125
  • 25
  • 247
  • 360
  • Look up "variable scope" Your frame variable has been declared within the main method and so is only visible within the main method and nowhere else. – Hovercraft Full Of Eels May 06 '22 at 23:13
  • 1
    But more importantly, I would strongly urge you not to learn programming from Youtube videos since your question and your code both suggest that you are missing some very basic Java concepts that are not learned through these videos and should be learned before trying to tackle GUI coding (if you want to avoid a world of frustration). These concepts include the difference between JavaScript and Java, why you should avoid static methods, why you want to learn OOP concepts first,.... – Hovercraft Full Of Eels May 06 '22 at 23:14
  • [tag:javascript] tag removed and [tag:swing] tag added – Hovercraft Full Of Eels May 06 '22 at 23:15
  • `null` layouts == bad, make the time to learn [how to use layout managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) – MadProgrammer May 06 '22 at 23:26

0 Answers0