为什么我收到错误?

时间:2014-07-10 21:11:32

标签: java swing compiler-errors calculator

我正在尝试用Java制作计算器。在了解了ActionListeners之后,我尝试将一个ActionListener添加到一个按钮,但是在我这样做并尝试编译该文件之后,我收到了一个错误。我的代码如下。

import javax.swing.*;//import the packages needed for gui
import java.awt.*;
public class Window {
public static void main(String[] args) {
    JFrame Window = new JFrame("Window");//makes a JFrame
    Window.setSize(300,350);
    Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel Panel = new JPanel (new FlowLayout());//makes the panel, textfield and buttons
    JTextField TextField = new JTextField(20);
    JButton openbracket = new JButton("(");
    JButton closebracket = new JButton(")");
    JButton clearbutton = new JButton("C");
    JButton arcsin = new JButton("arcsin");
    JButton arccos = new JButton("arccos");
    JButton arctan = new JButton("arctan");
    JButton sin = new JButton("sin");
    JButton cos = new JButton("cos");
    JButton tan = new JButton("tan");
    JButton log = new JButton("log");
    JButton seven = new JButton("7");
    JButton eight = new JButton("8");
    JButton nine = new JButton("9");
    JButton four = new JButton("4");
    JButton five = new JButton("5");
    JButton six = new JButton("6");
    JButton one = new JButton("1");
    JButton two = new JButton("2");
    JButton three = new JButton("3");
    JButton zero = new JButton("0");
    JButton radixpoint = new JButton(".");
    JButton equal = new JButton("=");
    String values = null;
    public class Listener implements ActionListener {
    public void actionPerformed(ActionEvent e) {          
        values = values+"3";
        TextField.setText(values);
    }
    Panel.add(TextField);//adding all the things
    Window.add(Panel);
    Panel.add(openbracket);
    Panel.add(closebracket);
    Panel.add(clearbutton);
    Panel.add(arcsin);
    Panel.add(arccos);
    Panel.add(arctan);
    Panel.add(sin);
    Panel.add(cos);
    Panel.add(tan);
    Panel.add(log);
    Panel.add(nine);
    Panel.add(eight);
    Panel.add(seven);
    Panel.add(six);
    Panel.add(five);
    Panel.add(four);
    three.addActionListener(Listener);
    Panel.add(three);
    Panel.add(two);
    Panel.add(one);
    Panel.add(zero);
    Panel.add(radixpoint);
    Panel.add(equal);
    Window.setVisible(true);
}
}

我得到的错误是:

Window.java:33:非法开始表达         public class Listener实现ActionListener {         ^ Window.java:38:预料到了  Panel.add(TextField); //添加所有内容           ^ Window.java:38:预料到了  Panel.add(TextField); //添加所有内容                     ^ Window.java:39:预料到了         Window.add(面板);                   ^ Window.java:39:预料到了         Window.add(面板);                         ^ Window.java:40:预料到了     Panel.add(openbracket);              ^ Window.java:40:预料到了     Panel.add(openbracket);                          ^ Window.java:41:预料到了     Panel.add(closebracket);              ^ Window.java:41:预料到了     Panel.add(closebracket);                           ^ Window.java:42:预料到     Panel.add(clearbutton);              ^ Window.java:42:预料到     Panel.add(clearbutton);                          ^ Window.java:43:期待     Panel.add(反正弦);              ^ Window.java:43:期待     Panel.add(反正弦);                     ^ Window.java:44:期待     Panel.add(反余弦);              ^ Window.java:44:期待     Panel.add(反余弦);                     ^ Window.java:45:预期     Panel.add(反正切);              ^ Window.java:45:预期     Panel.add(反正切);                     ^ Window.java:46:期待     Panel.add(SIN);              ^ Window.java:46:期待     Panel.add(SIN);                  ^ Window.java:47:期待     Panel.add(COS);              ^ Window.java:47:期待     Panel.add(COS);                  ^ Window.java:48:期待     Panel.add(TAN);              ^ Window.java:48:期待     Panel.add(TAN);                  ^ Window.java:49:期待     Panel.add(日志);              ^ Window.java:49:期待     Panel.add(日志);                  ^ Window.java:50:预期     Panel.add(9张);              ^ Window.java:50:预期     Panel.add(9张);                   ^ Window.java:51:期待     Panel.add(8);              ^ Window.java:51:期待     Panel.add(8);                    ^ Window.java:52:期待     Panel.add(7);              ^ Window.java:52:期待     Panel.add(7);                    ^ Window.java:53:期待     Panel.add(6);              ^ Window.java:53:期待     Panel.add(6);                  ^ Window.java:54:期待     Panel.add(5);              ^ Window.java:54:期待     Panel.add(5);                   ^ Window.java:55:期待     Panel.add(4);              ^ Window.java:55:期待     Panel.add(4);                   ^ Window.java:56:期待         three.addActionListener(监听器);                                ^ Window.java:56:期待         three.addActionListener(监听器);                                         ^ Window.java:57:期待     Panel.add(3);              ^ Window.java:57:期待     Panel.add(3);                    ^ Window.java:58:期待     Panel.add(二);              ^ Window.java:58:期待     Panel.add(二);                  ^ Window.java:59:期待     Panel.add(之一);              ^ Window.java:59:期待     Panel.add(之一);                  ^ Window.java:60:预料到     Panel.add(零);              ^ Window.java:60:预料到     Panel.add(零);                   ^ Window.java:61:期待     Panel.add(radixpoint);              ^ Window.java:61:期待     Panel.add(radixpoint);                         ^ Window.java:62:期待     Panel.add(相等);              ^ Window.java:62:期待     Panel.add(相等);                    ^ Window.java:63:期待     Window.setVisible(真);                      ^ Window.java:63:非法启动类型     Window.setVisible(真);                       ^ Window.java:65:解析时到达文件末尾 }  ^ 为什么我会收到这些错误?请解释原因。

1 个答案:

答案 0 :(得分:3)

您无法在java文件中声明多个公共类。

如果这是我的程序,我会使用私有内部类或匿名内部类,创建一个实例并将其添加到按钮中。

如,

ActionListener numberButtonListener = new ActionListener() {
  public void actionPerformed(ActionEvent e) {
     //  TODO: put some code here
  }
};

// assuming a List<AbstractButton> of the number buttons
for (AbstractButton numberButton : numberButtonList) {
  numberButton.addActionListener(numberButtonListener);
} 

对于您今后的问题,您希望改进代码缩进,以便更容易阅读和理解。


另外

根据评论,此代码:

public class Window {
public static void main(String[] args) {
    JFrame Window = new JFrame("Window");//makes a JFrame

您的班级名称和变量名称相同。这不好,可能会让您和编译器感到困惑。变量名也应以小写字母开头。此外,您将希望从静态主方法中获取大部分代码,并进入其所属的面向对象领域。您将要通过一个体面的介绍来阅读OOP和Java书籍,例如&#34; Thinking in Java&#34;。事情的顺序应该是:1)首先学习基础Java,然后2)学习Java中的子专业主题,如Swing,或数据库,或......