根据其他面板中的选项重置或重新加载卡布局中的JPanel内容

时间:2012-04-20 20:17:00

标签: swing jpanel wizard cardlayout

我正在使用卡片布局来创建一系列面板。

我创建了一个JFrame类。并且对于这个框架类的实例,获取了contentPane并设置了CardLayout。将所有面板添加到此contentPane。

public class JFrameClass1234 extends javax.swing.JFrame {
public static JFrameClass1234 jFrameInstance1234;
public static JPanelFirstScreen jPanelFirstScreen;
public static JPanelSecondScreen jPanelSecondScreen;
public static JPanelThirdScreen jPanelThirdScreen;
public static JPanelFourthScreen jPanelFourthScreen;
public static JPanelFifthScreen jPanelFifthScreen;

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {

           jFrameInstance1234 = new JFrameClass1234();

           jPanelFirstScreen = new JPanelFirstScreen();
           jPanelSecondScreen = new JPanelSecondScreen();
           jPanelThirdScreen = new JPanelThirdScreen();
           jPanelFourthScreen = new JPanelFourthScreen();
           jPanelFifthScreen = new JPanelFifthScreen();

           final Container pane = jFrameInstance1234.getContentPane();
           pane.add(jPanelFirstScreen,card1);
           pane.add(jPanelSecondScreen,card2);
           pane.add(jPanelThirdScreen,card3);
           pane.add(jPanelFourthScreen,card4);
           pane.add(jPanelFifthScreen,card5);
           CardLayout cl = (CardLayout)(pane.getLayout());
           cl.show(pane, card1);
           jFrameInstance1234.setVisible(true);
           }
      });
  } 

}//end of class

现在,我可以通过在每个面板的“Next”或“Back”按钮动作列表器中调用以下方法来在面板之间移动:

//For going back to previous panel
private void gotoPreviousPanel() {
final Container pane = JFrameClass1234.jFrameInstance1234.getContentPane();
CardLayout cl = (CardLayout) (pane.getLayout());
cl.show(pane, JFrameClass1234.card1);
JFrameClass1234.JFrameinstance1234.setVisible(true);
}

//For going to next panel
private void gotoNextPanel() {
    final Container pane = JFrameClass1234.jFrameInstance1234
            .getContentPane();
    CardLayout cl = (CardLayout) (pane.getLayout());
    cl.show(pane, JFrameClass1234.card3);
    JFrameClass1234.jFrameInstance1234.setVisible(true);
}

但是当我在面板之间移动时,值会保留(不会重置或重新加载)。我想在面板之间来回移动时重置或重新加载面板。 或者说第二个面板中的“文本框”应该在显示时重新加载,具体取决于在第一个面板中选择的“ComboBox”选项。有些人能帮助我实现这个目标吗?

什么是理想的设计(逻辑)我必须遵循这种情况?有没有针对我情况的文件?我使用卡片布局的方式是否正确?否则建议我正确的设计。

谢谢, 钱德拉

2 个答案:

答案 0 :(得分:0)

您可以使用属性声明一个 Singleton 类(来自您拥有的不同UI面板)。另外,使用方法updateUIFields()声明接口,并让所有面板都实现此接口。在更新一个面板(当前显示的面板)中的字段时更新单例对象。在切换到卡中的其他面板之前,请根据您在单件对象中存储的选择/值重新加载或重置。在接口方法中实现此逻辑。如果需要,重置Singleton对象中的值。

答案 1 :(得分:0)

您可以尝试将祖先Listener()添加到正在刷新的JPanel组件中。