2015-07-02 72 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