0

I have this problem with my Applet not showing Buttons, before I move the mouse over them. I googled it, and found out "validate();" might help. It did not. Maybe I did it wrong, but couldn't find any other solution. Here's my code:

    public void paintMainMenu(){
    Container conMain = getContentPane();
    play = new JButton("Play");
    credits = new JButton("Credits");
    controls = new JButton("Controls");
    FlowLayout lay1 = new FlowLayout();
    JPanel mainPanel = new JPanel();
    mainPanel.setSize(SW, SH);
    mainPanel.setLayout(lay1);
    mainPanel.add(play);
    mainPanel.add(credits);
    mainPanel.add(controls);
    play.addActionListener(this);
    credits.addActionListener(this);
    controls.addActionListener(this);
    conMain.add(mainPanel);
    conMain.validate(); // Validation...
    setContentPane(conMain);
}

I think the problem exists in here, but if you want full code just tell.

Thanks!

  • 1
    *"if you want full code just tell."* No thanks. For better help sooner, post an [SSCCE](http://sscce.org/). BTW - Most of that should be done in the `init()` method. – Andrew Thompson Aug 22 '13 at 20:05
  • Hi, thanks for the SSCCE advice, however it will be kinda hard. The thing is my program has to be able to switch layouts during runtime. Therefore I can't use init. Im using the paint(Graphics g); for this purpose. So making a SSCCE will be hard, since the nearly all my code relates to paint(Graphics g); But still thanks for your time. – Emil Henningsen Aug 28 '13 at 18:17
  • *"my program has to be able to switch layouts during runtime."* 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). – Andrew Thompson Aug 28 '13 at 20:17
  • Thanks alot for your input Andrew! Sorry for the long response. – Emil Henningsen May 06 '22 at 16:08

1 Answers1

0

You need to revalidate(); AND repaint();

Buttons should appear

Andrew Kor
  • 154
  • 5
  • 16