0

(should get username and password to procede to other frame)

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Cuizon07 {

public static void main(String[] args) {

    JFrame frames = new JFrame();
    frames.setVisible(true);
    frames.setSize(700, 500);
    frames.setResizable(false);
    frames.setLocation(170, 100);
    JPanel panels = new JPanel();
    frames.add(panels);
    panels.setBackground(new Color(56, 3, 96));
    panels.setLayout(null);
    JTextField tf1 = new JTextField();
    panels.add(tf1);
    tf1.setBounds(200, 50, 100, 25);
    JLabel label1 =  new JLabel("USERNAME:");
    panels.add(label1);
    label1.setBounds(100, 50, 150, 30);
    JLabel label2 =  new JLabel("PASSWORD:");
    panels.add(label2);
    JTextField tf2 = new JTextField();
    panels.add(tf2);
    tf2.setBounds(200, 100, 100, 25);
    label2.setBounds(100, 100, 150, 30);

    JButton button = new JButton("NEXT FRAME");
    panels.add(button);
    button.setBounds(500, 250, 150, 30);

    String user=tf1.getText();
    String pass=tf2.getText();

    //getting the username and password to the user i think is correct..

    if (user.equals("Dominic") && (pass.equals("1234"))) 
    {
        button.addActionListener(new copro());
        JOptionPane.showMessageDialog(null, "LOGIN SUCCESSFULL");

    }
    else
    {
        tf1.setText(" ");
        tf2.setText(" ");

        JOptionPane.showMessageDialog(null, "INCORRECT USERNAME OR PASSWORD");

     /*the else statement, joptionpane always popup after i run the program, i dont know how to  correct this problem.. */
    }
}

    public static class copro implements ActionListener{

        public void actionPerformed (ActionEvent e){

        JFrame frame2 =  new JFrame("NEW FRAME");
        frame2.setVisible(true);
        frame2.setSize(700, 500);
        frame2.setLocation(170, 100);
        JLabel label =  new JLabel("YOU CLICKED ME");
        JPanel panel2 = new JPanel();
        frame2.add(panel2);
        panel2.add(label);
        panel2.setLayout(null);
        panel2.setBackground(new Color(13, 97, 150));
        label.setBounds(500, 250, 150, 30);
        }
        /*this is the 2nd frame after you enter the correct username and password*/

    }
}
MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59

1 Answers1

0
  1. You need to put the login code in an ActionListener and add that to the button

  2. I would put the code to call the second frame in a seperate method (it doesn't need to be in an ActionListener itself)

Here's a working example:

    JButton button = new JButton("NEXT FRAME");
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            String user = tf1.getText();
            String pass = tf2.getText();

            if (user.equals("Dominic") && (pass.equals("1234"))) {
                openFrame2();
                JOptionPane.showMessageDialog(null, "LOGIN SUCCESSFULL");

            } else {
                tf1.setText("");
                tf2.setText("");

                JOptionPane.showMessageDialog(null, "INCORRECT USERNAME OR PASSWORD");
            }
        }
    });
    panels.add(button);
    button.setBounds(500, 250, 150, 30);

}

protected static void openFrame2() {
    JFrame frame2 = new JFrame("NEW FRAME");
    frame2.setVisible(true);
    frame2.setSize(700, 500);
    frame2.setLocation(170, 100);
    JLabel label = new JLabel("YOU CLICKED ME");
    JPanel panel2 = new JPanel();
    frame2.add(panel2);
    panel2.add(label);
    panel2.setLayout(null);
    panel2.setBackground(new Color(13, 97, 150));
    label.setBounds(500, 250, 150, 30);
}
JensS
  • 1,151
  • 2
  • 13
  • 20