Java 如何通过在主类中创建另一个JFrame对象来显示它

Java 如何通过在主类中创建另一个JFrame对象来显示它,java,swing,jframe,actionlistener,Java,Swing,Jframe,Actionlistener,主类中的操作侦听器 play.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Game g= new Game(); add(g); } }); Constructor of Game class Game(){ Container c=this.getContentPane(); c.setVisible(true);

主类中的操作侦听器

play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
    Game g= new Game();
        add(g);
    }
});


Constructor of Game class

Game(){
    Container c=this.getContentPane();
    c.setVisible(true);
    this.setVisible(true);
    add(new g());


    addMouseListener(new ml());
        this.setBounds(100,100,200,300);
    this.setTitle("LEVEL 1:");


    for(int loop=0;loop<=10;loop++){
        ballfall();

}


ScoreScreen sc= new ScoreScreen();
sc.setmethod(String.valueOf(sum));

}
play.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
游戏g=新游戏();
添加(g);
}
});
游戏类的构造函数
游戏(){
容器c=this.getContentPane();
c、 setVisible(真);
此.setVisible(true);
添加(新g());
addMouseListener(新ml());
本标准为100100300300;
本文件的标题为“第1级:”;

对于(int loop=0;loopCall
this.setVisible(true);
在最后添加组件后。@Braj如果您是指在ActionListener中,那么它会给出错误。但是如果我在action listener结束后立即添加,那么它就不起作用1)为了更快地获得更好的帮助,请发布一个(最小完整且可验证的示例).2)对于一个空间中的多个组件,请使用如图所示的。3)请参见