我正在使用Java开发人员的Eclipse IDE,Kepler Service Release 2,并将jdk1.8.0安装为JRE。
我正在使用Java傻瓜一体机学习Java SE 8,并且在编写程序时使用Swing,出现此错误:syntax error on token "-", -- expected
使用此代码:
button1 = new JButton("Click me!");
button1.addActionListener(e -> button1Click() );
panel1.add(button1);
exitButton = new JButton("Exit");
exitButton.addActionListener(e -> exitButtonClick() );
panel1.add(exitButton);
this.add(panel1);
button1和exitButton已经被声明为JButton,并且我已经导入了javax.swing。*和java.awt.event。*。
我该如何解决?
如果您使用开普勒,请安装此更新:http://wiki.eclipse.org/JDT/Eclipse_Java_8_Support_For_Kepler
然后设置
windows > preferences > java > compiler > compiler compliance level > 1.8
[留在这里,因为我是从Google到这里的,后来修复了我自己的错误:Syntax error on token "-", -- expected
错误也会在以下情况下引发:->
(而不是在lambda表达式中使用- >
)(即,在两个字符之间有一个空格,在这种情况下,Java怀疑您正在尝试使用--
)。