1

I am trying to have one class open by default, and then when I click a Swing button I would like to have the other form run. I used to use Visual basic and it was so simple:

Form2.Show()

and

Form2.Hide()

But in Java, I can't find anything like that. Is there any way to do this easily?

Thanks!

Lucas
  • 75
  • 1
  • 1
  • 4

3 Answers3

2

Study the following to see how pressing a button causes a new frame to display.

public static void main(String args[]) throws Exception { 
    new JFrame(){{  
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);  
        setSize(300,300);  
        setLocation(100,100);  
        setVisible(true);  
        setLayout(new BorderLayout());  
        add(new JButton("PressToOpenNewFrame"){{  
           addActionListener(new ActionListener(){  
           public void actionPerformed(ActionEvent e) {  
               new JFrame(){{  
                   setDefaultCloseOperation(DISPOSE_ON_CLOSE);  
                   setSize(300,300);  
                   setLocation(200,200);  
                   setVisible(true);  
                   setLayout(new BorderLayout());  
                   add(new JButton("PressMeToBeep"){{  
                      addActionListener(new ActionListener(){  
                      public void actionPerformed(ActionEvent e) {  
                         Toolkit.getDefaultToolkit().beep();  
                      }});  
                   }}, BorderLayout.SOUTH);  
               }};  
           }});  
        }}, BorderLayout.SOUTH);  
    }};  
} 
Java42
  • 7,628
  • 1
  • 32
  • 50
0

You can try: Form#setVisible(boolean)

BachT
  • 1,058
  • 9
  • 17
-1
frmMain1 f = new frmMain1();
        f.show();
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130