2013-06-05 128 views
0

enter image description here如何在JFrame中添加外部JPanel?

我正在从事大型项目。正如你所看到的,我有一个主要的JFrame和大约20个菜单项。每个菜单项都必须弹出一个新窗口。一开始,我已经创建了一个JLayeredPanel,然后给我分配每个菜单项一个的JPanel这是内部JFrame.Then我把25面板JLayeredPanel ......默认所有的面板被设置为不可见,如:

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

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

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


} 

请给我任何建议,我怎么能以专业的方式在splited类控制这个程序。

+0

是'Public'有效或'延伸的JFrame()'? –

+0

@yannishristofakis是的,它是公开的。 – Bernard

+0

这是您关于此主题的第3个问题。退出创建新问题,并按照使用'CardLayout'的其他帖子的建议。绝对没有理由使用JLayeredPane。 – camickr

回答

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

  2. 添加的新JPanel与常量,everyhing取决于使用的布局管理,并在API

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

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

+0

:谢谢你的回复。通过将常量添加到JPanel中,你的意思是什么?你的意思是JPanel应该定义为一个常量或JPanle内的元素应该是一个常量? – Bernard

+0

带有常量的JPanel,例如,当您通过GridLayout删除第三个JPanel放置在网格中时,则需要使用该常量将另一个JPanel放置到第三个JPanel。地区再次, – mKorbel

1

请给我任何建议,我怎么能在proressional方式splited类控制这个程序。

好的。

你应该把所有的JPanels放在一个JTabbedPane中。 JTabbedPane将被添加到JFrame中。

JFrame,JTabbedPane和每个JPanel将构建在一个单独的类中。

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

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

请阅读article以了解如何将Swing GUI放在一起。

+0

我是否必须将每个JPanel作为复合(成员函数)在JTabbedPane中?我的问题是连接单独的JPanel类到单独的JTabbedPane类? – Bernard

+0

@Bernard:是的。 JTabbedPane的每个选项卡都需要一个单独的JPanel实例。您可以在多个窗格上使用相同的JPanel,但必须为每个窗格创建单独的实例。 –

+0

感谢您的回复。对不起,我对Java有点新。正如你所说,应该为JTabbedPane设立单独的类。 JTabbedPane应该从哪些方面扩展? – Bernard

0

化妆的代码更好

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

}