Calculator类:(包含算术方法)

package mycalcgie;

import java.util.*;

class Calculator {

public int TEMP, SolveTEMP;

public static void Add (int TEMP, int SolveTEMP){

    SolveTEMP += TEMP;

}

public static void Subtract (int TEMP, int SolveTEMP){

    SolveTEMP -= TEMP;

}

public static void Divide (int TEMP, int SolveTEMP){

    SolveTEMP /= TEMP;

}

public static void Multiply (int TEMP, int SolveTEMP){

    SolveTEMP *= TEMP;

}


}


WindowedCalculator类:(包含GUI)

package mycalcgie;

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

class WindowedCalculator extends Calculator{

 JButton Num1;
 JButton Num2;
 JButton Num3;
 JButton Num4;
 JButton Num5;
 JButton Num6;
 JButton Num7;
 JButton Num8;
 JButton Num9;
 JButton Num0;
 JButton Equal;
 JButton AddBt;
 JButton Subtractbt;
 JButton Multiplybt;
 JButton Dividebt;
 JButton Solvebt;
 JButton Clearbt;
 JTextField Result;
 int TEMP;
 int SolveTEMP;

Boolean addBool = false ;
Boolean subBool = false ;
Boolean divBool = false ;
Boolean mulBool = false ;

String display = "";

public WindowedCalculator() {

    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(4, 3));
    p1.add(Num1 = new JButton("1"));
    p1.add(Num2 = new JButton("2"));
    p1.add(Num3 = new JButton("3"));
    p1.add(Num4 = new JButton("4"));
    p1.add(Num5 = new JButton("5"));
    p1.add(Num6 = new JButton("6"));
    p1.add(Num7 = new JButton("7"));
    p1.add(Num8 = new JButton("8"));
    p1.add(Num9 = new JButton("9"));
    p1.add(Num0 = new JButton("0"));
    p1.add(Clearbt = new JButton("C"));

    JPanel p2 = new JPanel();
    p2.setLayout(new FlowLayout());
    p2.add(Result = new JTextField(10));
    Result.setHorizontalAlignment(JTextField.RIGHT);
    Result.setEditable(true);

    JPanel p3 = new JPanel();
    p3.setLayout(new GridLayout(5, 1));
    p3.add(AddBt = new JButton("+"));
    p3.add(Subtractbt = new JButton("-"));
    p3.add(Multiplybt = new JButton("*"));
    p3.add(Dividebt = new JButton("/"));
    p3.add(Solvebt = new JButton("="));

    JPanel p = new JPanel();
    p.setLayout(new GridLayout());
    p.add(p2, BorderLayout.NORTH);
    p.add(p1, BorderLayout.SOUTH);
    p.add(p3, BorderLayout.EAST);


    Num1.addActionListener(new ListenToOne());
    Num2.addActionListener(new ListenToTwo());
    Num3.addActionListener(new ListenToThree());
    Num4.addActionListener(new ListenToFour());
    Num5.addActionListener(new ListenToFive());
    Num6.addActionListener(new ListenToSix());
    Num7.addActionListener(new ListenToSeven());
    Num8.addActionListener(new ListenToEight());
    Num9.addActionListener(new ListenToNine());
    Num0.addActionListener(new ListenToZero());

    AddBt.addActionListener(new ListenToAdd());
    Subtractbt.addActionListener(new ListenToSubtract());
    Multiplybt.addActionListener(new ListenToMultiply());
    Dividebt.addActionListener(new ListenToDivide());
    Solvebt.addActionListener(new ListenToSolve());
    Clearbt.addActionListener(new ListenToClear());
}


class ListenToClear implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    //display = Result.getText();
        Result.setText("");
        addBool = false ;
        subBool = false ;
        mulBool = false ;
        divBool = false ;

        TEMP = 0;
        SolveTEMP = 0;
    }
}
class ListenToOne implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        display = Result.getText();
        Result.setText(display + "1");
    }
}

class ListenToTwo implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        display = Result.getText();
        Result.setText(display + "2");
    }
}
class ListenToThree implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        display = Result.getText();
        Result.setText(display + "3");
    }
}
class ListenToFour implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        display = Result.getText();
        Result.setText(display + "4");
    }
}
class ListenToFive implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        display = Result.getText();
        Result.setText(display + "5");
    }
}
class ListenToSix implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        display = Result.getText();
        Result.setText(display + "6");
    }
}
class ListenToSeven implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        display = Result.getText();
        Result.setText(display + "7");
    }
}
class ListenToEight implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        display = Result.getText();
        Result.setText(display + "8");
    }
}
class ListenToNine implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        display = Result.getText();
        Result.setText(display + "9");
    }
}
class ListenToZero implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        display = Result.getText();
        Result.setText(display + "0");
    }
}

class ListenToAdd implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        TEMP = Integer.parseInt(Result.getText());
                Result.setText("+");
                addBool = true ;

    }
}
class ListenToSubtract implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        TEMP = Integer.parseInt(Result.getText());
        Result.setText("-");
        subBool =true;
    }
}
class ListenToMultiply implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        TEMP = Integer.parseInt(Result.getText());
        Result.setText("*");
        mulBool =true;

    }
}
class ListenToDivide implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        TEMP = Integer.parseInt(Result.getText());
        Result.setText("/");
        divBool =true;
    }
}

class ListenToSolve implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        SolveTEMP = Integer.parseInt( Result.getText() );

        if ( addBool == true  )
            WindowedCalculator.Add (TEMP, SolveTEMP);

        else if ( subBool == true  )
            WindowedCalculator.Subtract (TEMP, SolveTEMP);

        else if ( mulBool == true  )
            WindowedCalculator.Multiply (TEMP, SolveTEMP);

        else if ( divBool == true  )
            WindowedCalculator.Divide (TEMP, SolveTEMP);

    Result.setText(  Integer.toString( SolveTEMP ) );

    addBool = false ;
    subBool = false ;
    mulBool = false ;
    divBool = false ;

    }
}

/*public static void main(String[] args) {
// TODO Auto-generated method stub
    WindowedCalculator calc = new WindowedCalculator();
    calc.pack();
    calc.setLocationRelativeTo(null);
    calc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    calc.setVisible(true);
}*/


}


Tester类:(包含main方法)

package mycalcgie;

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;

class Tester extends WindowedCalculator {

public static void main(String[] args) {

    WindowedCalculator calc = new WindowedCalculator();
    calc.pack();
    calc.setLocationRelativeTo(null);
    calc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    calc.setVisible(true);

}
}


除Tester.java外,所有内容都可以编译。返回4找不到符号错误,提示variable calc of the type WindowedCalculator

最佳答案

没有类的实例,您将无法访问实例字段。

您可能希望将这些字段设为静态。

关于java - 在主类中找不到符号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17972503/

10-10 08:36