Illegal static declaration in inner class while using ActionListener

混江龙づ霸主 提交于 2021-01-29 03:08:12

问题


I'm trying to write a basic calculator GUI program for homework. 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. My problem is that I get the error code, "Illegal static declaration in inner class 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();
   }



}
}

回答1:


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? 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.



来源:https://stackoverflow.com/questions/55910832/illegal-static-declaration-in-inner-class-while-using-actionlistener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!