2013-02-15 87 views
1

這是一個示例演示程序,我從Oracle的Java教程網站獲得。最初的方法itemStateChanged()開始實施這樣的:CardLayout在下次調用時不會切換面板

public void itemStateChanged(ItemEvent evt) { 
    CardLayout cl = (CardLayout)(cards.getLayout()); 
    cl.show(cards, (String)evt.getItem()); 
} 

我改成了這樣:

public void itemStateChanged(ItemEvent evt) { 
    CardLayout cl = (CardLayout)(cards.getLayout()); 
    cl.next(cards); 
} 

根據的JavaDoc API,下一個()方法應該翻轉面板,但它是沒有發生。這是完整的代碼:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class CardLayoutDemo implements ItemListener { 
    JPanel cards; //a panel that uses CardLayout 
    final static String BUTTONPANEL = "Card with JButtons"; 
    final static String TEXTPANEL = "Card with JTextField"; 

    public void addComponentToPane(Container pane) { 
     //Put the JComboBox in a JPanel to get a nicer look. 
     JPanel comboBoxPane = new JPanel(); //use FlowLayout 
     String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL }; 
     JComboBox cb = new JComboBox(comboBoxItems); 
     cb.setEditable(false); 
     cb.addItemListener(this); 
     comboBoxPane.add(cb); 

     //Create the "cards". 
     JPanel card1 = new JPanel(); 
     card1.add(new JButton("Button 1")); 
     card1.add(new JButton("Button 2")); 
     card1.add(new JButton("Button 3")); 

     JPanel card2 = new JPanel(); 
     card2.add(new JTextField("TextField", 20)); 

     //Create the panel that contains the "cards". 
     CardLayout cl = new CardLayout(); 
     cards = new JPanel(cl); 
     cards.add(card1, BUTTONPANEL); 
     cards.add(card2, TEXTPANEL); 
     cl.show(cards, BUTTONPANEL); 

     pane.add(comboBoxPane, BorderLayout.PAGE_START); 
     pane.add(cards, BorderLayout.CENTER); 
    } 

    public void itemStateChanged(ItemEvent evt) { 
     CardLayout cl = (CardLayout)(cards.getLayout()); 
     cl.next(cards); 
    } 

    /** 
    * Create the GUI and show it. For thread safety, 
    * this method should be invoked from the 
    * event dispatch thread. 
    */ 
    private static void createAndShowGUI() { 
     //Create and set up the window. 
     JFrame frame = new JFrame("CardLayoutDemo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //Create and set up the content pane. 
     CardLayoutDemo demo = new CardLayoutDemo(); 
     demo.addComponentToPane(frame.getContentPane()); 

     //Display the window. 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     /* Use an appropriate Look and Feel */ 
     try { 
      //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 
      UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); 
     } catch (UnsupportedLookAndFeelException ex) { 
      ex.printStackTrace(); 
     } catch (IllegalAccessException ex) { 
      ex.printStackTrace(); 
     } catch (InstantiationException ex) { 
      ex.printStackTrace(); 
     } catch (ClassNotFoundException ex) { 
      ex.printStackTrace(); 
     } 
     /* Turn off metal's use of bold fonts */ 
     UIManager.put("swing.boldMetal", Boolean.FALSE); 

     //Schedule a job for the event dispatch thread: 
     //creating and showing this application's GUI. 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 

原始代碼是在這裏:CardLayoutDemo.java

回答

4

itemStateChanged事件實際上是被稱爲兩倍(這是正常的行爲),所以佈局管理器開關從當前窗格中,到下一個,然後到下一個(這是原來的)。

您應該改用ActionListener

相關問題