问题:使用CardLayout时,当我运行应用程序并将其切换到所有组件都显示的其他卡上时。我希望与JPanel card3关联的组件在切换另一张卡时不显示。这可能吗?
我希望在切换cardlayout时不显示card3组件(基本上只有四个按钮)。
public class App2 implements ActionListener {
JPanel cards;
final static String BUTTONPANEL = "Switch Mode";
final static String TEXTPANEL = "Card with JTextField";
public void addComponentToPane(Container pane) {
JPanel comboBoxPane = new JPanel();
String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL };
JButton jb = new JButton("Switch Mode");
jb.setFocusable(false);
jb.addActionListener(this);
comboBoxPane.add(jb);
//Create the "cards".
JPanel card1 = new JPanel(new GridLayout(6,1));
comboBoxPane.add(card1);
JPanel card3 = new JPanel(new GridLayout(6,1));
comboBoxPane.add(card3);
card1.add(new JLabel("Current Mode: Edit"));
card1.add(new JLabel("Question"));
card1.add(new JTextField(30));
card1.add(new JLabel("Answer"));
card1.add(new JTextField(30));
card3.add(new JButton("Previous"));
card3.add(new JButton("Next"));
card3.add(new JButton("Add"));
card3.add(new JButton("Save"));
JPanel card2 = new JPanel(new GridLayout(6,1));
card2.add(new JLabel("Current Mode: Test"));
card2.add(new JLabel("Question"));
card2.add(new JTextField("", 20));
card2.add(new JLabel("Answer"));
card2.add(new JTextField("", 20));
cards = new JPanel(new CardLayout());
cards.add(card1, BUTTONPANEL);
cards.add(card2, TEXTPANEL);
pane.add(comboBoxPane, BorderLayout.PAGE_END);
pane.add(cards, BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, TEXTPANEL);
card3.setVisible(false);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Flash Cards");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
App2 demo = new App2();
demo.addComponentToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}