在java中打开另一个类

时间:2013-11-13 17:43:30

标签: java swing jframe

我最近开始学习一些java,现在我遇到了一个问题。 当if语句完成时,我想要弹出另一个类的JFrame。

所以我想要这样的事情:

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

我的JFrame类看起来像这样:

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
{

}

1 个答案:

答案 0 :(得分:2)

这样的事情会起作用:

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

你还应该在构造函数中初始化你的JFrame,否则你的main方法会变得混乱

相关问题