我收到这个错误,让我从这个脚本中感到非常沮丧...
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);
// }
};
我正在尝试在JTextArea输出中声明setEditable ......有什么想法吗?
编辑: 错误是:
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的分号上。
答案 0 :(得分:3)
声明
this.setEditable(false);
需要在方法,静态初始化器或构造函数中,而不是在JTextArea
output
的匿名实现的类块中。如果您真的希望在output
类中使用此实现,则可以覆盖isEditable
@Override
public boolean isEditable() {
return false;
}
答案 1 :(得分:1)
此this.setEditable(false);
需要在构造函数或方法的上下文中定义。它不能在这些背景之外调用。
我觉得很奇怪人们坚持用这种方式实施课程,但那只是我。
您未向JPanel
添加任何新功能,因此,为什么不创建它,JTextarea
,JScrollPane
并完全添加它们。它将使您的代码更具可读性并解决这些愚蠢的错误。
就个人而言,更好的解决方案是......
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);