【问题标题】:Add JPanel to multiple JFrames将 JPanel 添加到多个 JFrame
【发布时间】:2013-12-12 02:45:35
【问题描述】:

我有一个包含大量上下文的 JPanel。

我希望能够将上述 JPanel 添加到两个 JFrame。有什么建议吗?

这是我想要的示例

public class Test
{
    public static void main(String [] args)
    {
        JPanel panel = new JPanel();
        panel.add(new JLabel("Hello world"));
        panel.validate();

        JFrame frame1, frame2;
        frame1 = new JFrame("One");
        frame2 = new JFrame("Two");
        frame1.add(panel);
        frame2.add(panel);
        frame1.validate();
        frame2.validate();
        frame1.setVisible(true);
        frame2.setVisible(true);
        frame1.pack();
        frame2.pack();
    }
}

我希望两个 JFrame 都显示 hello world,而不是一个显示它而另一个为空。如果 JPanel 中的数据更新,我希望两个 JFrame 都显示它。

感谢任何人的帮助,这一直困扰着我一段时间。

【问题讨论】:

    标签: java swing jframe jpanel


    【解决方案1】:

    我不认为你可以。 java.awt.Container 中的代码在您向其中添加 Component 时包含此花絮。在这里,阅读JFrame 获取Container 和您的JPanel 获取comp

            /* Reparent the component and tidy up the tree's state. */
            if (comp.parent != null) {
                comp.parent.remove(comp);
                if (index > component.size()) {
                    throw new IllegalArgumentException("illegal component position");
                }
            }
    

    (在java.awt.Container.addImpl(Component, Object, int)

    因此,当您将其添加到不同的Container 时,它会从旧的Container 中删除它。我想你可以重写这个基础级 AWT 代码,但我不推荐它。

    【讨论】:

    • 谢谢 :) 我非常希望这是可能的,但我想不可能:(
    【解决方案2】:

    所有 Swing 组件只能有一个父级,这意味着如果您将一个组件添加到另一个容器中,它将自动从第一个容器中移除...

    尝试类似...

        JPanel panel = new JPanel();
        panel.add(new JLabel("Hello world"));
    
        JFrame frame1, frame2;
        frame1 = new JFrame("One");
        frame1.add(panel);
        frame1.pack();
        frame1.setVisible(true);
    
        JPanel panel2 = new JPanel();
        panel2.add(new JLabel("Hello world"));
        frame2 = new JFrame("Two");
        frame2.add(panel2);
        frame2.pack();
        frame2.setVisible(true);
    

    相反

    【讨论】:

      【解决方案3】:

      听起来您想要同一事物的两个视图,因为它不适用于两个框架上的一个 JPanel,您是否考虑过创建两个面板,每个面板都引用您想要在每个面板中查看的事物,从而规避这个问题?

      粗略的想法如下。本质上,您不需要两个单独的 JPanel 来相互反映,您需要两个 JPanel 来反映其他内容。

      class Data<T> {
      .....stuff
        void update();
        T getdata();
      }
      
      class DataView<T> extends JPanel {
        private Data<T> data;
        ...stuff
        public DataView(T data) {
           this.data = data;
        }
        ...stuff
        protected void paintComponent(Graphics g) {
           drawData();
           ...stuff
        }
      }
      

      然后

      public static void main(String[] args ) {
        Data<String> data = new Data();
        DataView<String> dataViewOne = new DataView(data);
        DataView<String> dataViewTwo = new DataView(data);
        ..add to two seperate frames, pack set visible etc..
      }
      

      【讨论】:

        猜你喜欢
        • 2012-05-02
        • 2011-09-13
        • 1970-01-01
        • 2013-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多