【问题标题】:How to add external JPanel in JFrame?如何在 JFrame 中添加外部 JPanel?
【发布时间】:2013-06-01 10:58:56
【问题描述】:

我正在研究大型项目。如您所见,我有一个主要的 JFrame 和大约 20 个菜单项。每个菜单项必须弹出一个新窗口。一开始我创建了一个 JLayeredPanel,然后我将每个菜单项分配给 JFrame 内的一个 JPanel。然后我在 JLayeredPanel 中放置了 25 个面板...默认所有面板都设置为不可见,例如:

panel1.setVisible(false);
panel2.setVisible(false);

等等

当用户单击一个菜单项时,它的 JPanel 将可见,而其余的则不可见。它看起来很乱,我有 5000 行代码。我使用了 InternalFrame 和 TabbedPane,但我对它们不满意。我想将我的代码拆分为不同的 JPanel 类并将它们分配给主 JFrame。我的意思是当用户单击每个菜单项时,它将调用外部 JPanel 并将其呈现在主 JFrame 上的 JPanel 上。我在 netbeans 中使用设计模式,它为我做了一切,但简单的结构是这样的,它不起作用:

 public class NewJPanel extends JPanel{
    //I have added buttons and etc on this panel
    ......

    }

public class  frame extends JFrame(){

        JPanel panel = new JPanel();
        .....
        Public frame(){
             frame.add(panel);
        }
        ......
        //When use click on the any button on the panel
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            //this is not working

            NewJPanel fi = new NewJPanel ();
            panel1.add(fi);

            //or I tested this way separately but it did not work

           panel1.remove();
           panel1 = new NewJPanel();
           add(panel);
           invalidate();
        }       


}

请给我任何建议,我可以如何以专业的方式在分班中控制这个程序。

【问题讨论】:

标签: java swing user-interface jframe jpanel


【解决方案1】:

让代码更好

public class NewJPanel extends JPanel{
//I have added buttons and etc on this panel
......

}

 public class  frame extends JFrame(){

    JPanel panel = new JPanel();
    .....
    Public frame(){
         //frame.add(panel); you dont need call frame because extends JFrame in frame class
         add(panel);

    ......
    //When use click on the any button on the panel
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        //this is not working

        NewJPanel fi = new NewJPanel();
        add(fi);

        //or I tested this way separately but it did not work

       /*panel1.remove();
       panel1 = new NewJPanel();
       add(panel);
       invalidate();you must define panel1 before use it,like  :JPanel panel1 = new JPanel();*/
    }       

 } 

【讨论】:

    【解决方案2】:

    请给我任何建议,我可以如何以专业的方式控制这个项目。

    好的。

    您应该将所有 JPanel 放在 JTabbedPane 中。 JTabbedPane 将被添加到 JFrame。

    JFrame、JTabbedPane 和每个 JPanel 都将在一个单独的类中构造。

    您使用 Swing 组件,而不是扩展它们。扩展 Swing 组件的唯一原因是重写组件方法之一。

    您还应该为每个 JPanel 创建模型类,以及为应用程序创建一个模型类。

    阅读article 了解如何将 Swing GUI 组合在一起。

    【讨论】:

    • 是否必须将每个 JPanel 作为复合(成员函数)放入 JTabbedPane 中?我的问题是将单独的 JPanel 类连接到单独的 JTabbedPane 类?
    • @Bernard:是的。 JTabbedPane 的每个选项卡都需要一个单独的 JPanel 实例。您可以在多个窗格上使用相同的 JPanel,但您必须为每个窗格创建一个单独的实例。
    • 感谢您的回复。对不起,我对java有点陌生。正如你所说,JTabbedPane 应该有单独的类。 JTabbedPane 应该从什么扩展?
    • @Bernard:您不会扩展 Swing 组件。你使用它们。
    • 你说得对。所以我只是创建一个新框架并创建一个 Java 面板并在那里定义我的 JTabbedPane。我把 JTabbedPane 放在了 JFrame 上。最后为每个表创建一个 JPanel,我把它放在 JTabbedPane 中。对吗?
    【解决方案3】:
    1. 从 JFrame.getContentPane.remove(myPanel) 中删除 JPanel

    2. 添加一个带有常量的新 JPanel,一切都取决于使用的 LayoutManager 及其在 API 中实现的方法

    3. 调用 JFrame.(re)validate() 和 JFrame.repaint() 作为最后的代码行,如果一切都完成了,这些通知器会正确地重新绘制可用区域

    4. 再次使用 CardLayout,没有明显的性能或内存问题

    【讨论】:

    • :感谢您的回复。添加带有常量的JPanel是什么意思?你的意思是 JPanel 应该定义为常量还是 JPanel 中的元素应该是常量?
    • 带有常量的JPanel,例如当您通过GridLayout 删除了放置在网格中的第3 个JPanel 时,您需要使用该常量将另一个JPanel 放到第3 个。再次区域,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多