Java - 从单独的组件中重绘组件

Java - Redrawing a component from within a separate component

我对 Java 中的 GUI 编程还很陌生,遇到了一个问题,即一个组件的更改会导致另一个组件(不是 parent 或 child 或 grand parent 或 grandchild 等)必须重新绘制。更具体地说,我有一个 window 作为 winGUIJFrame 的子 class)和 BorderLayout contentPane,其 CENTER 包含一个具有以下方法的组件单击组件时运行:

winGUI window = (winGUI)(getParent().getParent().getParent().getParent());
window.updatePanel(panelContent);

(我怀疑有更好的方法来检索 window,所以知道这一点会很好。)

winGUI的class定义如下

import java.awt.BorderLayout;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class winGUI extends JFrame {
    private UIController userInterface;
    private JPanel content;

    public static void main(String[] args) {
        JFrame window = new winGUI();
        window.setResizable(false);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);
    }

    public winGUI() {
        super("Window");
        setSize(Toolkit.getDefaultToolkit().getScreenSize());
        setResizable(false);
        content = new JPanel();
        content.setLayout(new BorderLayout());
        userInterface = new UIController();
        content.add(userInterface.getCenter(), BorderLayout.CENTER);
        content.add(userInterface.getPanel(), BorderLayout.WEST);
        setContentPane(content);
        setLocation(0, 0);
    }

    public void updatePanel(PanelContent panelContent) {
        userInterface.buildPanel(panelContent);
        content.add(userInterface.getPanel(), BorderLayout.WEST);
    }

}

下面是 UIController 的重要部分 class:

private JPanel sidePanel;

public UIController() {
    buildPanel(null);
}

public void buildPanel(PanelContent panelContent) {
    sidePanel = new JPanel();
    sidePanel.setBackground(new Color(220, 220, 220));
    sidePanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
    sidePanel.setPreferredSize(new Dimension(250, 500));
    if (panelContent != null) {
        sidePanel.add(new JLabel(panelContent.getLabel()));
    }
}

public JPanel getPanel() {
    return sidePanel;
}

updatePanel 方法中调用 userInterface.buildPanel(panelContent) 后,我希望面板发生变化,因为 getPanel() 方法通过引用传递,所以组件添加到 content window 和 sidePanel 创建时应该更新,因为它们引用相同的 object。然而,这并没有发生,所以我尝试添加

content.add(userInterface.getPanel(), BorderLayout.WEST);
userInterface.getPanel().repaint();
this.repaint();

updatePanel方法结束,但无济于事。

非常感谢任何帮助。

您可以使用 observer pattern(创建您自己的类似 Swing 的侦听器)将事件传递给 winGui 它应该更新它的面板。

interface SomeUpdateListener {
     public void onUpdate(PanelContent panelContent);
}

那就让winGui成为这样的倾听者:

public class winGUI extends JFrame implements SomeUpdateListener
    ...
    @Override
    public void onUpdate(PanelContent panelContent) {
        userInterface.buildPanel(panelContent);
        content.add(userInterface.getPanel(), BorderLayout.WEST);
        // Fix for your last issue I believe
        this.revalidate();  // Pretty sure this is what's needed in your case
        this.repaint(); // redraws them
    }

我不知道调用 updatePanel 的自定义组件是什么,也不知道它是如何工作的,但我们假设它现在被称为 SomeCustomComponent,代码大致如下:

public class SomeCustomComponent extends JComponent {
    private SomeUpdateListener listener;
    public SomeCustomComponent(SomeUpdateListener someListener) {
         this.listener = someListener;
    }
    ...
    private void timeToMakeThatPanelContentUpdate() {
         PanelContent newContent = createNewPanelContents();
         listener.onUpdate(newContent);
    }
    ...

所以现在有了这个新的 "listener" 类型,您的父组件(或者我想这里最终是父组件...)和应用程序代码中的子组件之间没有直接的循环引用.

注意我在 onUpdate() 方法中添加了几行来修复面板的最后一期没有更新最新内容。

this.revalidate(); // Pretty sure this is what's needed in your case
this.repaint();

此外,如果您正在创建的面板实际上与某些 GUI 组件组合没有任何不同,而只是与其显示的信息不同,那么也许您应该只传递表示先前存在的 GUI 组件现在应该显示的内容的数据.