Java 单击带有操作的按钮时出错
因此,我有一个java应用程序开发,我使用一个按钮动作(当按钮点击时),即所谓的“退出”,我得到以下错误:Java 单击带有操作的按钮时出错,java,swing,Java,Swing,因此,我有一个java应用程序开发,我使用一个按钮动作(当按钮点击时),即所谓的“退出”,我得到以下错误: 在第21行非法启动expresson 下面是代码: package apptutorial; import javax.swing.*; import java.awt.event.*; public class AppDev extends JFrame { public static void main(String[] args) { JFrame myF
在第21行非法启动expresson
下面是代码:
package apptutorial;
import javax.swing.*;
import java.awt.event.*;
public class AppDev extends JFrame {
public static void main(String[] args) {
JFrame myFrame = new JFrame();
String myTitle = "Alpha Application";
JButton button = new JButton("Exit");
myFrame.setTitle(myTitle);
myFrame.setSize(400, 300);
myFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
myFrame.setVisible(true);
myFrame.add(button);
button.setSize(100,50);
button.setVisible(true);
private void buttonActionPerformed(ActionEvent evt) {
System.exit(0);
}
}
}
Java不支持嵌套方法。从
main
方法中删除按钮执行的操作
Java不支持嵌套方法。从main
方法中删除buttonActionPerformed
,您需要将buttonActionPerformed
置于main
public class AppDev extends JFrame {
public static void main(String[] args) {
JFrame myFrame = new JFrame();
String myTitle = "Alpha Application";
JButton button = new JButton("Exit");
myFrame.setTitle(myTitle);
myFrame.setSize(400, 300);
myFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
myFrame.setVisible(true);
myFrame.add(button);
button.setSize(100,50);
button.setVisible(true);
}
private void buttonActionPerformed(ActionEvent evt) {
System.exit(0);
}
}
您需要将
按钮操作执行
置于main
public class AppDev extends JFrame {
public static void main(String[] args) {
JFrame myFrame = new JFrame();
String myTitle = "Alpha Application";
JButton button = new JButton("Exit");
myFrame.setTitle(myTitle);
myFrame.setSize(400, 300);
myFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
myFrame.setVisible(true);
myFrame.add(button);
button.setSize(100,50);
button.setVisible(true);
}
private void buttonActionPerformed(ActionEvent evt) {
System.exit(0);
}
}
你应该重读你应该重读