我正在尝试编写一个用于家庭作业的基本计算器GUI程序。我正在尝试为计算器上按下的每个按钮定义一个变量,并在声明所有变量时进行计算。现在,它只能将两个数字0-9相加/相减/相除/相乘,但是我想确保在扩展之前可以正常工作。我的问题是我得到了错误代码“内部类Calculator.sign中的非法静态声明”。我想知道如何克服这个错误。
谢谢
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();
}
}
}
答案 0 :(得分:0)
我想知道如何传递此错误。
您可以先确定错误的性质。它抱怨静态声明,那么它在说什么声明?那不应该很难。编译器会为您提供错误的行号,但是即使没有该行号,整个程序中也只有一个静态声明:
public static void main(String[] args) { new Calculator(); }
在Calculator.sign
类中吗?是的,如果仔细看,您会发现事实确实如此。那是应该在的地方吗?我倾向于不考虑,但是如果确实是您的意图,那么您将需要重新考虑,因为内部类(与静态嵌套类相对)可能没有任何静态成员。
您可能还想查看类Calculator.sign
中是否还有其他不应该存在的东西。