0

我是 Java 新手。我想显示一个带有两个按钮的 Jframe。单击每个按钮后,应显示不同的 JOptionPane 消息。但是,我Illegal start of expression在两个static class声明中都遇到了错误。

谁能解释为什么?我试过移动静态类,但仍然是同样的错误。

这是我的代码...

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

                    }

                }   



        }
4

1 回答 1

2

在方法中声明static类在 Java 中是非法的。您可以声明本地类,而无需static关键字。

public static void main(String[] args) {

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

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

或者您可以将它们完全排除在方法之外。要么在他们自己的编译单元中声明它们,即。一个java文件,或者作为内部类,或者作为一个static 嵌套类

有关的:

于 2013-09-16T00:56:52.977 回答