简体   繁体   English

如何从其父JFrame外部重绘JPanel?

[英]How to repaint JPanel from outside its parent JFrame?

I can add/remove elements to/from a panel and repaint it when the method used to fill the panel is called by one of its parent JFrame events, but I can not repaint it by events from other classes even if their sources have been added to it, or that is how I understand the problem for now. 我可以在panel添加元素/从panel删除元素,并在其父JFrame事件之一调用用于填充面板的方法时重新绘制元素,但是即使其他类的事件已经添加,我也不能重新绘制它还是那是我目前对问题的理解。

I want to understand what is going on here, Thank you. 我想了解这里发生的事情,谢谢。

Main Class 主班

public class Principal extends JFrame implements ActionListener{
private static Principal instPrincipal = null;    
private SubClass subClassInst =new SubClass();
public JPanel panelPrincipal;

public static Principal getInstance() {
    if (instPrincipal != null)
        return instPrincipal ;
    else {
        instPrincipal = new Principal ();
        return instPrincipal ;
    }
}

public void actionPerformed(ActionEvent event) {
    Object source = event.getSource();
    try {
        if(source == btnSub)
        {
          subClassInst.fillPanelPrincipal();
        }
        }
     catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
     }
    }
 }

Sub Classes Example 子类示例

public class SubClass implements ActionListener {

private JPanel tempPanel;
private JButton btnSave; 
private Principal instPrincipal; 

public void fillPanelPrincipal() {
    instPrincipal = Principal.getInstance();
    instPrincipal.panelPrincipal.removeAll();
    //Start adding elements..
    tempPanel = new JPanel();
    instPrincipal.panelPrincipal.add(tempPanel);
    btnSave = new JButton("Save");
    btnSave.addActionListener(this);
    tempPanel.add(btnSave);
    //End. 
    instPrincipal.panelPrincipal.repaint();
  }

public void actionPerformed(ActionEvent event) {
    instPrincipal = Principal.getInstance();
    Object source = event.getSource();
    if (source == btnSave) {
    // modify local data, Database .. ; //work but need to be repainted on panelPrincipal
    instPrincipal.panelPrincipal.repaint();//does not work
    }
   }

}

Update 更新

To clarify the problem more, I have one single JPanel on a JFrame and there are different classes to fill it for multiple functionalities, I call their methods using JMenuItems on the main frame, these Classes implement ActionListener , passing the panel didn't work, and also the method I am trying here. 为了进一步阐明问题,我在JFrame上只有一个JPanel ,并且为不同的功能填充了不同的类,我在主框架上使用JMenuItems调用了它们的方法,这些类实现ActionListener ,传递面板不起作用,还有我在这里尝试的方法 I thought about changing the design to use CardLayout , but it was very difficult. 我曾考虑过更改设计以使用CardLayout ,但这非常困难。

You are calling Principal as a static reference, so how is it supposed to know what frame to repaint? 您将Principal称为静态引用,因此如何知道要重画的帧? You should pass the instance of the JFrame through the constructor of the subclass. 您应该通过子类的构造函数传递JFrame的实例。 Like so: 像这样:

private SubClass subClassInst = new SubClass(this);

And create the constructor like this 并像这样创建构造函数

private JFrame parent;
public SubClass(JFrame parent) { this.parent = parent; }

You can then use it like so 然后可以像这样使用它

this.parent.repaint();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM