使用ActionListener时内部类中的静态静态声明 [英] Illegal static declaration in inner class while using ActionListener

查看:69
本文介绍了使用ActionListener时内部类中的静态静态声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个用于家庭作业的基本计算器GUI程序.我正在尝试为在计算器上按下的每个按钮定义一个变量,并在声明所有变量时进行计算.目前,它只能将两个数字0-9相加/相减/相除/相乘,但是我想确保在扩展之前可以正常工作.我的问题是我得到了错误代码内部类Calculator.sign中的非法静态声明".我想知道如何克服这个错误.

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.

谢谢

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

Calculator.sign 类中的那个吗?是的,如果仔细看,您会发现它是正确的.那是应该在的地方吗?我倾向于不考虑,但是如果这确实是您的意图,那么您将需要再考虑一下,因为内部类(与静态嵌套类相对)可能没有任何静态成员.

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.

您可能还想查看类 Calculator.sign 中是否还有其他不应该存在的东西.

You may also want to look at whether there is anything else inside class Calculator.sign that should not, in fact, be there.

这篇关于使用ActionListener时内部类中的静态静态声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆