简体   繁体   English

如何在JFrame中添加外部JPanel?

[英]How to add external JPanel in JFrame?

在此输入图像描述

I'm working on large scale program. 我正在做大规模的计划。 As you can see I have one main JFrame and about 20 menu items on that. 正如您所看到的,我有一个主要的JFrame和大约20个菜单项。 Each menu item must pop up a new window. 每个菜单项都必须弹出一个新窗口。 At the beginning I have created a JLayeredPanel and then I assigned each menu item to one JPanel which is inside JFrame.Then I put 25 panel in JLayeredPanel... Default all the panels are set to invisible like: 一开始我创建了一个JLayeredPanel,然后我将每个菜单项分配给JFrame里面的一个JPanel。然后我将25个面板放在JLayeredPanel中...默认所有面板都设置为隐形,如:

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

so on 等等

When user click on one menu item, its JPanel will be visible and rest are invisible. 当用户单击一个菜单项时,其JPanel将可见,其余部分不可见。 It looks messy and I have 5000 lines code. 它看起来很乱,我有5000行代码。 I used InternalFrame and TabbedPane but I'm not happy with them. 我使用了InternalFrame和TabbedPane,但我对它们并不满意。 I want to split my code in different JPanel classes and assign them to the main JFrame. 我想将我的代码拆分到不同的JPanel类中,并将它们分配给主JFrame。 I mean when user clicked on each menu item it will call the external JPanel and render it on the JPanel on the main JFrame. 我的意思是当用户点击每个菜单项时,它将调用外部JPanel并在主JFrame上的JPanel上呈现它。 I am using design mode in netbeans and it does everything for me but the simpled structure is like this and it is not working: 我在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();
        }       


}

please give me any suggestion how I can control this program in splited classes in professional way. 请给我任何建议如何以专业的方式控制分裂课程中的这个程序。

  1. remove JPanel from JFrame.getContentPane.remove(myPanel) 从JFrame.getContentPane.remove(myPanel)中删除JPanel

  2. add a new JPanel with constants, everyhing depends of used LayoutManager and its methods implemented in API 添加一个带常量的新JPanel,每次使用依赖于使用的LayoutManager及其在API中实现的方法

  3. call JFrame.(re)validate() and JFrame.repaint() as last code lines, if everything is done, these notifiers correctly repaint available area 调用JFrame。(重新)validate()和JFrame.repaint()作为最后的代码行,如果一切都完成,这些通知符正确地重绘可用区域

  4. again to use CardLayout, there isn't signoficant performance or memory issue 再次使用CardLayout,没有明显的性能或内存问题

Please give me any suggestion how I can control this program in splited classes in proressional way. 请给我任何建议如何以proressional方式控制分裂课程中的这个程序。

Ok. 好。

You should put all of your JPanels in a JTabbedPane. 您应该将所有JPanel放在JTabbedPane中。 The JTabbedPane would be added to the JFrame. JTabbedPane将添加到JFrame中。

The JFrame, JTabbedPane, and each JPanel would be constructed in a separate class. JFrame,JTabbedPane和每个JPanel将在一个单独的类中构建。

You use Swing components, rather than extending them. 您使用Swing组件,而不是扩展它们。 The only reason you extend a Swing component is if you override one of the component methods. 扩展Swing组件的唯一原因是,如果覆盖其中一个组件方法。

You should also create model classes for each of the JPanels, as well as a model class for the application. 您还应该为每个JPanel创建模型类,以及为应用程序创建模型类。

Read this article to see how to put a Swing GUI together. 阅读本文 ,了解如何将Swing GUI放在一起。

make's code better 使代码更好

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();*/
    }       

 } 

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

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