user2988879
user2988879

Reputation: 389

Open another class in java

I recently started learning some java, and I am facing a problem at this moment. When an if statement is completed, I want a JFrame from another class to pop up.

So I would like something like this:

if(...)
{
    //open the JFrame from the other class  
}

My JFrame class looks like this:

import javax.swing.*;

public class Game {

    public static Display f = new Display();
    public static int w = 600;
    public static int h = 400;

    public static void main(String args[])
    {
        f.setSize(w, h);
        f.setResizable(true);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setTitle("Frame Test");
    }
}

class Display extends JFrame
{

}

Upvotes: 0

Views: 1859

Answers (1)

Sionnach733
Sionnach733

Reputation: 4736

Something like this would work:

if(...)
{
    //open the JFrame from the other class  
    OtherClass otherClass = new OtherClass();
    otherClass.setVisible(true);
}

You should also initialise your JFrames in the constructor, otherwise your main method will get messy

Upvotes: 4

Related Questions