Felipe2048
Felipe2048

Reputation: 11

Error on clicking button with action

So i have a java application development and i use an button action (when the button clicks), so called "Exit", i get this error:

illegal start of expresson at line 21

and here's the code:

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

Upvotes: 1

Views: 67

Answers (2)

TheCodingFrog
TheCodingFrog

Reputation: 3514

You need to put buttonActionPerformed outside 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);
    }
}

Upvotes: 1

Reimeus
Reimeus

Reputation: 159794

Java doesnt support nested methods. Remove buttonActionPerformed from the main method

Upvotes: 3

Related Questions