0

I'm new to Java. I want to display a Jframe with two buttons in it. Upon clicking each button a different JOptionPane message should show. However I'm getting the error Illegal start of expression on both the static class declarations.

can anyone explain why? I've tried moving the static classes but still the same error.

Here is my code...

import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

public class main { 

    public main() { 
    }


    public static void main(String[] args) { 

                    String name = JOptionPane.showInputDialog(null, "What's your name?", "Enter your name", JOptionPane.PLAIN_MESSAGE);
                    System.out.println("\nWelcome, "+ name + ".");  
                    System.out.print(terms);
                    JOptionPane.showMessageDialog(null, "Welcome to SkyWhale, " + name + ".\n" + deets + "\n" + terms , "Welcome, " + name + ".", JOptionPane.PLAIN_MESSAGE);

                    JFrame control = new JFrame("SkyWhale");
                      control.setVisible(true);
                      control.setSize(500,200);
                      control.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                      JPanel panel = new JPanel();
                      JButton chatbtn = new JButton("Live Chat");
                      JButton editorbtn = new JButton("Editor");
                      control.add(panel);
                      panel.add(chatbtn);
                      panel.add(editorbtn);

                      chatbtn.addActionListener(goToChat());
                      editorbtn.addActionListener(goToEdit());

                     static class goToEdit implements ActionListener { 
                        public void actionPerformed(ActionEvent e) 
                        {
                            JOptionPane.showMessageDialog(null, "Code goes here...", "Editor", JOptionPane.PLAIN_MESSAGE);
                        }
                      }
                     static class goToChat implements ActionListener { 
                        public void actionPerformed (ActionEvent e) 
                        {
                            JOptionPane.showMessageDialog(null, "Conversation...", "Live Chat", JOptionPane.PLAIN_MESSAGE);
                        }
                      }

                    }

                }   



        }

1 Answers1

2

Declaring static classes in a method is illegal in Java. You can declare local classes, without the static keyword.

public static void main(String[] args) {

    class Foo {
        public void bar() {
            System.out.println("inside Foo#bar()");
        }
    }

    Foo foo = new Foo();
    foo.bar();
}

Or you can take them completely out of the method. Either declare them in their own compilation unit, ie. a java file, or as inner classes, or as a static nested classes.

Related:

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724