JPanel doesn't update when adding Component in another class

瘦欲@ 提交于 2019-11-27 02:21:58

You should revalidate your panel

@Override
public void actionPerformed(ActionEvent e) {
   JButton temp = new JButton("temp");
   mPanel.add(temp);
   mPanel.revalidate();
   mPanel.repaint();
}
Adam

I believe you need to call revalidate() and repaint() to see the changes, here is a similar question here

The problem here is the panel is not repainted automatically.. When you resize the panel Java repaints the panel on the screen. Try repainting the panel everytime any button to modify the panel is clicked..

Just call the validate() and repaint() method with the panel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!