JPanel doesn't update when adding Component in another class

前端 未结 3 579
甜味超标
甜味超标 2020-11-29 13:43

I\'m fairly new to Java Swing and I\'m running into a few problems.

  1. As a side question, when making a fairly large Java Swing Application, what is the best wa
相关标签:
3条回答
  • 2020-11-29 14:16

    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

    0 讨论(0)
  • 2020-11-29 14:21

    You should revalidate your panel

    @Override
    public void actionPerformed(ActionEvent e) {
       JButton temp = new JButton("temp");
       mPanel.add(temp);
       mPanel.revalidate();
       mPanel.repaint();
    }
    
    0 讨论(0)
  • 2020-11-29 14:23

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

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