简体   繁体   English

使用ActionListener时内部类中的静态静态声明

[英]Illegal static declaration in inner class while using ActionListener

I'm trying to write a basic calculator GUI program for homework. 我正在尝试编写一个用于家庭作业的基本计算器GUI程序。 I'm trying to define a variable for each button pressed on the calculator, and make a calculation when all the variables are declared. 我正在尝试为计算器上按下的每个按钮定义一个变量,并在声明所有变量时进行计算。 Right now, it's only able to add/subtract/divide/multiply two numbers 0-9, but I want to make sure I can get that working before expanding it. 现在,它只能将两个数字0-9相加/相减/相除/相乘,但是我想确保在扩展之前可以正常工作。 My problem is that I get the error code, "Illegal static declaration in inner class Calculator.sign". 我的问题是我得到了错误代码“内部类Calculator.sign中的非法静态声明”。 I'm wondering how I can get past this error. 我想知道如何克服这个错误。

Thanks 谢谢

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/*
This program will display a calculator by GUI application. It accepts
floating point numbers.
*/
public class Calculator extends JFrame
{
   private final int WINDOW_WIDTH = 350;
   private final int WINDOW_HEIGHT = 350;
   private double num1 = 0;
   private double num2 = 0;
   private int sign;
   private double answer;

   JButton seven = new JButton("7");
   JButton eight = new JButton("8");
   JButton nine = new JButton("9");
   JButton mult = new JButton("x");
   JButton four = new JButton("4");
   JButton five = new JButton("5");
   JButton six = new JButton("6");
   JButton min = new JButton("-");
   JButton one = new JButton("1");
   JButton two = new JButton("2");
   JButton three = new JButton("3");
   JButton plus = new JButton("+");
   JButton zero = new JButton("0");
   JButton point = new JButton(".");
   JButton equ = new JButton("=");
   JButton div = new JButton("/");


   //contructor
   public Calculator()
   {
      setTitle("Calculator");

      setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      setLayout(new GridLayout(4,4));

      add(seven);
      add(eight);
      add(nine);
      add(mult);
      add(four);
      add(five);
      add(six);
      add(min);
      add(one);
      add(two);
      add(three);
      add(plus);
      add(zero);
      add(point);
      add(equ);
      add(div);

      seven.addActionListener(new ButtonListener());
      eight.addActionListener(new ButtonListener());
      nine.addActionListener(new ButtonListener());
      mult.addActionListener(new ButtonListener());
      four.addActionListener(new ButtonListener());
      five.addActionListener(new ButtonListener());
      six.addActionListener(new ButtonListener());
      min.addActionListener(new ButtonListener());
      one.addActionListener(new ButtonListener());
      two.addActionListener(new ButtonListener());
      three.addActionListener(new ButtonListener());
      plus.addActionListener(new ButtonListener());
      zero.addActionListener(new ButtonListener());
      point.addActionListener(new ButtonListener());
      equ.addActionListener(new ButtonListener());
      div.addActionListener(new ButtonListener());

      setVisible(true);
   }

   private class ButtonListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         if(e.getSource() == seven)
        {
            num1 = 7;
        }
        else if(e.getSource() == eight)
        {
            num1 = 8;
        }
        else if(e.getSource() == nine)
        {
            num1 = 9;
        }
        else if(e.getSource() == four)
        {
            num1 = 4;
        }
        if(e.getSource() == five)
        {
            num1 = 5;
        }
        else if(e.getSource() == six)
        {
            num1 = 6;
        }
        else if(e.getSource() == one)
        {
            num1 = 1;
        }
        else if(e.getSource() == two)
        {
            num1 = 2;
        }
        if(e.getSource() == three)
        {
            num1 = 3;
        }
        else if(e.getSource() == zero)
        {
            num1 = 0;
        }
      }
   }
   private class sign implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         if(e.getSource() == mult)
         {
            sign = 1;
         }
         else if(e.getSource() == div)
         {
            sign = 2;
         }
         else if(e.getSource() == plus)
         {
            sign = 3;
         }
         else if(e.getSource() == min)
         {
            sign = 4;
         }
      }
      private class ButtonListener2 implements ActionListener
      {
         public void actionPerformed(ActionEvent e)
         {
            if(e.getSource() == seven)
        {
            num2 = 7;
        }
        else if(e.getSource() == eight)
        {
            num2 = 8;
        }
        else if(e.getSource() == nine)
        {
            num2 = 9;
        }
        else if(e.getSource() == four)
        {
            num2 = 4;
        }
        if(e.getSource() == five)
        {
            num2 = 5;
        }
        else if(e.getSource() == six)
        {
            num2 = 6;
        }
        else if(e.getSource() == one)
        {
            num2 = 1;
        }
        else if(e.getSource() == two)
        {
            num2 = 2;
        }
        if(e.getSource() == three)
        {
            num2 = 3;
        }
        else if(e.getSource() == zero)
        {
            num2 = 0;
        }

        }
      }
      private class equals implements ActionListener
      {
         public void actionPerformed(ActionEvent e)
         {
            if(sign == 1)
            {
               answer = num1*num2;
               JOptionPane.showMessageDialog(null, answer);
            }
            else if(sign == 2)
            {
               answer = num1/num2;
               JOptionPane.showMessageDialog(null, answer);
            }
            else if(sign == 3)
            {
               answer = num1 + num2;
               JOptionPane.showMessageDialog(null, answer);
            }
            else if(sign == 4)
            {
               answer = num1 - num2;
               JOptionPane.showMessageDialog(null, answer);
            }
         }

   }

   /*
   Main Method
   */
   public static void main(String[] args)
   {
      new Calculator();
   }



}
}

I'm wondering how I can get passed this error. 我想知道如何通过此错误。

You can start by identifying the nature of the error. 您可以先确定错误的性质。 It's complaining about a static declaration, so what declaration could it be talking about? 它抱怨静态声明,那么它在说什么声明? That shouldn't be hard. 那不应该很难。 The compiler would have given you a line number for the error, but even without that, there's only one static declaration in the whole program: 编译器会为您提供错误的行号,但是即使没有该行号,整个程序中也只有一个静态声明:

  public static void main(String[] args) { new Calculator(); } 

Is that in class Calculator.sign ? 那是在Calculator.sign类中吗? Why yes, if you look carefully, you will see that it is. 是的,如果仔细看,您会发现事实确实如此。 Is that where it's supposed to be? 那是应该在的地方吗? I'm inclined to think not, but if that was indeed your intention then you'll need to think again, because an inner class (as opposed to a static nested class) may not have any static members. 我倾向于不考虑,但是如果这确实是您的意图,那么您将需要再考虑一下,因为内部类(与静态嵌套类相对)可能没有任何静态成员。

You may also want to look at whether there is anything else inside class Calculator.sign that should not, in fact, be there. 您可能还想查看在Calculator.sign类中是否存在其他实际上不应该存在的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM