【问题标题】:Reset or Reload a JPanel contents in Card Layout depending on the selections in other panels根据其他面板中的选择重置或重新加载卡片布局中的 JPanel 内容
【发布时间】:2012-04-20 20:17:00
【问题描述】:

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

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

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

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

谢谢, 钱德拉

【问题讨论】:

    标签: swing jpanel wizard cardlayout


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-22
        • 2017-04-09
        • 2011-09-04
        • 2018-06-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多