2015-06-22 106 views
0

我想要做的就是設置卡片佈局樣式按鈕。從理論上講,你應該點擊按鈕,另一個(在這種情況下)名字會被假設出來直到完成。 (當用戶關閉)試圖讓cardLayout工作

我有這樣的,我覺得它應該TOTALLY工作,但JGrasp是給我的錯誤:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
public class JDisappearingFriends extends JFrame implements ActionListener 
{ 
    private JButton intro = new JButton("Click to see Friends!"); 
    private JButton nb = new JButton("Mini Vinny"); 
    private JButton sb = new JButton("Makayla"); 
    private JButton eb = new JButton("Aurora"); 
    private JButton wb = new JButton("Alyssa"); 
    private JButton cb = new JButton("And My Bestest Friend: SAMMY!!"); 
    CardLayout cardLayout = new CardLayout(); 
    public JDisappearingFriends() 
    { 
     setLayout(new CardLayout()); 
     add("Click to see Friends!", intro); 
     add("MiniVinny", nb); 
     add("Makayla", sb); 
     add("Aurora", eb); 
     add("Alyssa", wb); 
     add("And Finally My Best Friend Sammy!", cb); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     intro.addActionListener(this); 
     nb.addActionListener(this); 
     sb.addActionListener(this); 
     eb.addActionListener(this); 
     wb.addActionListener(this); 
    } 
    public void actionPreformed(ActionEvent e) 
    { 
     cardLayout.next(getContentPane()); 
    } 
    public static void main(String[] args) 
    { 
     JDisappearingFriends jbl = new JDisappearingFriends(); 
     jbl.setSize(400, 400); 
     jbl.setVisible(true); 
    } 
} 

當我嘗試編譯,我得到一個錯誤信息,上市如下:

JDisappearingFriends.java:8: error: JDisappearingFriends is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener 

公共類JDisappearingFriends擴展JFrame中實現的ActionListener ^ 如果任何人都可以把我一個提示,我將不勝感激!

回答

2

你的錯誤消息提供您用的是什麼問題,一個很好的跡象:

JDisappearingFriends.java:8: error: JDisappearingFriends is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener

actionPreformed是拼寫錯誤,應該是actionPerformed

你也應該得到應用使用@Override註解

@Override 
public void actionPreformed(ActionEvent e) 
{ 
    cardLayout.next(getContentPane()); 
} 

這有助於當你重寫其他類的方法,並提供編譯時警衛以確保您不會意外拼錯方法名稱

您還應該從EDT的上下文中創建您的UI。有關更多詳細信息,請參閱Initial Threads

+0

HAHAHAHAhahahaha OMG謝謝你sooo,我簡直不敢相信那麼簡單!過分看起來像是瘋狂!謝謝,現在我可以停止擊中我的頭在鍵盤上! –

+0

@Stuckonsameproblem哦,我不知道,我發現我的頭對着鍵盤的包裝往往寫更好的代碼;) – MadProgrammer