2012-07-15 79 views
3

我正在使用卡片佈局,並且要使第一張卡片具有一個按鈕,並且在單擊它時將它帶到卡片2上,該卡片上有一個可將其取回卡片的按鈕1.這裏是我目前的代碼,我已經嘗試在actionPerformed方法中放入一些東西,但是我沒有取得任何成功。另外,我在button1.addActionListener(this)的行上接收到「this」的語法錯誤;和button2.addActionListener(this);我認爲這是因爲我的actionPerformed方法沒有正確設置。任何幫助獲得按鈕設置將不勝感激。使用jbutton的卡片之間的Java切換

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

public class Main implements ItemListener { 

JPanel cards; 

public void addComponentToPane(Container pane) {   
    //create cards 
    JPanel card1 = new JPanel(); 
    JPanel card2 = new JPanel(); 
    JButton button1 = new JButton("Button 1"); 
    JButton button2 = new JButton("Button 2"); 
    button1.addActionListener(this); 
    button2.addActionListener(this); 
    card1.add(button1); 
    card2.add(button2); 

    //create panel that contains cards 
    cards = new JPanel(new CardLayout()); 
    cards.add(card1); 
    cards.add(card2);   
    pane.add(cards, BorderLayout.CENTER); 
} 

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

public static void createAndShowGUI() { 
    //create and setup window 
    JFrame frame = new JFrame("Frame"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    //create and setup content pane 
    Main main = new Main(); 
    main.addComponentToPane(frame.getContentPane()); 

    //display window 
    frame.pack(); 
    frame.setVisible(true); 
} 

public void actionPerformed(ActionEvent ae) { 

}    

public static void main(String[] args) { 
    //set look and feel 
    try { 
     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 bold fonts 
    UIManager.put("swing.boldMetal", Boolean.FALSE);   

    //schedule job for the event dispatch thread creating and showing GUI   
    javax.swing.SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGUI(); 
     } 
    });  
} 

}

回答

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

public class CardTest implements ActionListener { 

    private JPanel cards; 
    private JButton button1; 
    private JButton button2; 

    public void addComponentToPane(Container pane) { 
     // create cards 
     JPanel card1 = new JPanel(); 
     JPanel card2 = new JPanel(); 
     button1 = new JButton("Button 1"); 
     button2 = new JButton("Button 2"); 
     button1.addActionListener(this); 
     button2.addActionListener(this); 
     card1.add(button1); 
     card2.add(button2); 

     // create panel that contains cards 
     cards = new JPanel(new CardLayout()); 
     cards.add(card1, "Card 1"); 
     cards.add(card2, "Card 2"); 
     pane.add(cards, BorderLayout.CENTER); 
    } 

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

    public static void createAndShowGUI() { 
     // create and setup window 
     JFrame frame = new JFrame("Frame"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     // create and setup content pane 
     CardTest main = new CardTest(); 
     main.addComponentToPane(frame.getContentPane()); 

     // display window 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public void actionPerformed(ActionEvent ae) { 
     if (ae.getSource() == button1) { 

      CardLayout cl = (CardLayout) (cards.getLayout()); 
      cl.show(cards, "Card 2"); 

     } else if (ae.getSource() == button2) { 

      CardLayout cl = (CardLayout) (cards.getLayout()); 
      cl.show(cards, "Card 1"); 
     } 
    } 

    public static void main(String[] args) { 
     // set look and feel 
     try { 
      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 bold fonts 
     UIManager.put("swing.boldMetal", Boolean.FALSE); 

     // schedule job for the event dispatch thread creating and showing GUI 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 
+0

感謝這麼多的幫助。我仍然收到「this」下面的錯誤說:方法javax.swing.AbstractButton中的addActionListener不能應用於給定的類型; 要求:java.awt.event.ActionListener 發現:noxia.CardTest 原因:實際參數noxia.CardTest無法通過方法調用轉換而轉換成java.awt.event.ActionListener – Jarod 2012-07-15 00:49:01

+0

更改類定義來實現_ActionListener_ – Reimeus 2012-07-15 00:51:40

+0

啊謝謝你這麼多,非常感謝:) – Jarod 2012-07-15 00:52:55

相關問題