I am having a weird issue with my initial JFrame login. When I run the program it will only pull up the cancel JButton at first. Then if I minimize the frame it shows everything like it should. Is there a reason for this? If so, how do I fix it?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
public class example 
{
   public static void main (String[] args)
   {    
      JFrame frame = new JFrame("Login");
      frame.setVisible(true);
      frame.setSize(350,150);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      JLabel label1 = new JLabel("User Id: ");
      JLabel label2 = new JLabel("Password: ");
      JTextField txt2 = new JTextField(20);
      JButton button = new JButton("login");
      JTextField txt = new JTextField(20);
      frame.add(label1);
      frame.add(txt);
      frame.add(label2);
      frame.add(txt2);
      String user = txt.getText();
      String password = txt2.getText();
      JButton button2 = new JButton("Cancel");
      frame.add(button);
      frame.add(button2);
      button2.addActionListener (new Action2()); 
      frame.setVisible(true);
      frame.setLayout(new FlowLayout());
      button.addActionListener(new ActionListener()
      { 
         public void actionPerformed( ActionEvent e)
         {   
            if ("abc".equals(txt.getText()) && "123".equals(txt2.getText())) 
            {
               JFrame frame2 = new JFrame("Student");
               frame2.setVisible(true);
               frame2.setSize(200,200);
               frame.setVisible(false);
            }
            if ("def".equals(txt.getText()) && "456".equals(txt2.getText())) 
            {
               JFrame frame2 = new JFrame("Instructor");
               frame2.setVisible(true);
               frame2.setSize(200,200);
               frame.setVisible(false);
            }
            if ("ghi".equals(txt.getText()) && "789".equals(txt2.getText())) 
            {
               JFrame frame2 = new JFrame("Teacher");
               frame2.setVisible(true);
               frame2.setSize(200,200);
               frame.setVisible(false);
            }
            else
            {
               System.out.println("Invalid Password");
            }
         } 
      });
   }
   static class Action2 implements ActionListener {        
      public void actionPerformed (ActionEvent e) 
      {     
         System.exit(0);
      } 
   }
}
 
     
    