在子 jpanel 上重新绘制

发布于 2024-08-18 04:08:34 字数 680 浏览 4 评论 0原文

好吧,假设我在另一个 jpanel 中有一个 JPanel“控件”和 jpanel“graphPanel”,

public class outer extends JPanel implements ActionListener{
   private JPanel controls,graphPanel;
   private JButton doAction

   public outer(){
      JPanel controls = new JPanel();
      JButton doAction = new JButton("Do stuff");
      doAction.addActionListener(this);
      controls.add(doAction);

      JPanel graphPanel = new JPanel();
      this.add(controls);
      this.add(graphPanel);
   }



public void actionPerformed(ActionEvent e) {
    if(e.getSource()==doAction){
        //How do I fire paintComponent of controls JPanel on this click
}

单击按钮后如何重新绘制“graphPanel”

Ok say I have a JPanel "controls" and jpanel "graphPanel" within another jpanel

public class outer extends JPanel implements ActionListener{
   private JPanel controls,graphPanel;
   private JButton doAction

   public outer(){
      JPanel controls = new JPanel();
      JButton doAction = new JButton("Do stuff");
      doAction.addActionListener(this);
      controls.add(doAction);

      JPanel graphPanel = new JPanel();
      this.add(controls);
      this.add(graphPanel);
   }



public void actionPerformed(ActionEvent e) {
    if(e.getSource()==doAction){
        //How do I fire paintComponent of controls JPanel on this click
}

How do i make "graphPanel" repaint after my button is clicked

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟凡古楼 2024-08-25 04:08:34

doAction 和 graphPanel 声明了两次 - 一次在类级别,然后在方法中再次声明:

   private JPanel controls,graphPanel; // declared here first
   private JButton doAction; // declared here first

   public outer(){
      JPanel controls = new JPanel(); // Whoops, declared here again
      JButton doAction = new JButton("Do stuff"); // Whoops, declared here again
      doAction.addActionListener(this);
      controls.add(doAction);

      JPanel graphPanel = new JPanel(); // Whoops, declared here again
      ...

在方法中删除声明,并使它们成为简单的赋值,如下所示:

controls = new JPanel(); // no leading 'JPanel'

这样做,附加的重绘代码将不会抛出 NPE

The doAction and graphPanel are declared twice - once at the class level, then again in the method:

   private JPanel controls,graphPanel; // declared here first
   private JButton doAction; // declared here first

   public outer(){
      JPanel controls = new JPanel(); // Whoops, declared here again
      JButton doAction = new JButton("Do stuff"); // Whoops, declared here again
      doAction.addActionListener(this);
      controls.add(doAction);

      JPanel graphPanel = new JPanel(); // Whoops, declared here again
      ...

In the method remove the declaration, and make them simple assignments, like this:

controls = new JPanel(); // no leading 'JPanel'

Do that and the additional repaint code won't throw a NPE

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文