How do I get a JLabel to show over a JButton?

后端 未结 1 881
天涯浪人
天涯浪人 2021-01-28 16:40

I have a JLabel that is ontop of a JButton, but it will not show up ontop. When the code for the JButton is commented out, the JLabel is shown, meaning that it is there but is o

相关标签:
1条回答
  • 2021-01-28 17:20

    Swing paint components based on the ZOrder of the component. Basically the last component added is painted first.

    So you could achieve what you want by doing:

    c.add(button);
    c.add(label);
    

    Although, I agree with the comments. What are you trying to do? There is probably a better solution. It is almost never a good idea to be using setBounds() to position and size a component.

    I need this for my game where when the button is clicked, a JLabel will be shown ontop of the button to display a "score" that is added.

    If you are going to force the user to click a button to display the score then you should probably be using a JOptionPane to display the label. Otherwise how will the label disappear? Forcing the user to click on the same button is not a very good UI.

    0 讨论(0)
提交回复
热议问题