0

Action Listener in main class

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

}

I am making a game in which the there is homescreen where user clicks playbutton and another screen appears where the main game is.The problem is when I click play button a blank black screen appears and nothing happens. But when I create Game oibject in main method , it works fine but it doesn't work when its object is created in Action Listener. I've even added the object g in add().

Ashish Aggarwal
  • 3,018
  • 2
  • 23
  • 46
ABC
  • 33
  • 6
  • Call `this.setVisible(true);` after adding the component in the end. – Braj May 25 '14 at 15:10
  • @Braj if you mean in ActionListener then it gives error. But if I add right after action listener ends then it doesn't work – ABC May 25 '14 at 15:16
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 2) For many components in one space, use a [`CardLayout`](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) as seen in this [short example](http://stackoverflow.com/a/5786005/418556). 3) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson May 26 '14 at 00:35

0 Answers0