2015-07-02 52 views
1

所以我有一个Java应用程序开发,我使用一个按钮动作(当按钮点击),所谓的“退出”,我得到这个错误:错误的点击与动作按钮

illegal start of expresson at line 21

和这里是代码:

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); 
     } 
    } 
} 
+0

你应该重读[编写事件监听器(https://docs.oracle.com/javase/tutorial/uiswing/events/删除buttonActionPerformed ) – Codebender

回答

3

Java不支持嵌套方法。从main方法

1

你需要把buttonActionPerformedmain

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); 
    } 
} 
+0

谢谢!这不是错误! – Felipe2048