1

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

我创建了一个 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

现在,我可以通过在每个面板的“下一步”或“后退”按钮操作侦听器中调用以下方法来在面板之间移动:

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

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

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

谢谢,钱德拉

4

2 回答 2

0

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

于 2012-04-20T23:05:33.153 回答
0

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

于 2013-11-02T16:53:27.033 回答