How to Dynamically Add JButton to JPanel?

后端 未结 3 552
傲寒
傲寒 2020-12-06 23:51

In NetBeans, I have used the GUI editor to make a JFrame and I\'ve put a JPanel in the frame. At the moment, I\'m trying to make a new button in the panel when the class con

3条回答
  •  一生所求
    2020-12-07 00:18

    Some times when you don't see a button it is a layout manager issue (as in you aren't setting the right properties for the layout manager). You can test this by disabling it:

    this.jPanel2.setLayoutManager(null);
    

    And setting bounds for the button (JButton.setBounds()).

    If the above fixes your problem, then you need to look into the requirements set by the LayoutManager you are using (see also the answer by Robin).

    All the calls to validate(), revalidate() and repaint() are not needed to do this.

提交回复
热议问题