Lucas
Lucas

Reputation: 75

Java Swing two form classes

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!

Upvotes: 1

Views: 684

Answers (3)

user3213740
user3213740

Reputation: 1

frmMain1 f = new frmMain1();
        f.show();

Upvotes: -1

BachT
BachT

Reputation: 1068

You can try: Form#setVisible(boolean)

Upvotes: 0

Java42
Java42

Reputation: 7716

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);  
    }};  
} 

Upvotes: 2

Related Questions