2013-03-18 66 views
0

我得到這個錯誤,這真的令人沮喪的我從該位腳本...Java錯誤申報的serialVersionUID內的JPanel

JPanel menu = new JPanel() { 
      private static final long serialVersionUID = 1L; 
      JTextArea output = new JTextArea(5, 30) { 

       /** 
       * 
       */ 
       private static final long serialVersionUID = 4714318125998709253L; 
       this.setEditable(false); 
      }; 
      JScrollPane scrollPane = new JScrollPane(output); 

//   public void run() { 
//    System.out.println("lol"); 
//    JPanel menu = this; 
//    JButton restart = new JButton("Restart"); 
//    menu.add(restart); 
//   } 

     }; 

我想聲明setEditable JTextArea的輸出...任何想法的內?

編輯: 錯誤是:

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: 
    Syntax error on token ";", < expected 

    at citadelRPG.Server.createAndShowGUI(Server.java:94) 
    at citadelRPG.Server.access$0(Server.java:16) 
    at citadelRPG.Server$1.run(Server.java:162) 
    at java.awt.event.InvocationEvent.dispatch(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$000(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 

出現在聲明的serialVersionUID的分號。

回答

3

聲明

this.setEditable(false); 

需要在方法,靜態初始化或構造,而不是在匿名執行JTextAreaoutput的類塊。如果你真的希望有output類這裏面實現,你可以覆蓋isEditable

@Override 
public boolean isEditable() { 
    return false; 
} 
1

this.setEditable(false);需要一個構造函數或方法的範圍內確定。它不能在這些情境之外被調用。

我覺得很奇怪,人們堅持以這種方式實施課程,但那只是我。

您沒有向JPanel添加任何新功能,爲什麼不創建它,JTextareaJScrollPane並將它們完全添加。它會讓你的代碼更具可讀性並解決這些愚蠢的錯誤。

就個人而言,一個更好的解決辦法是......

JPanel menu = new JPanel() { 
    private static final long serialVersionUID = 1L; 
}; 
JTextArea output = new JTextArea(5, 30) { 
    private static final long serialVersionUID = 4714318125998709253L; 
}; 
output.setEditable(false); 
JScrollPane scrollPane = new JScrollPane(output); 
menu.add(scrollPane);