0

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类:(包含主要方法)

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

4

2 回答 2

1

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

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

于 2013-07-31T14:01:31.523 回答
1

您尝试在 WindowedCalculator 上调用的方法是 JFrame 类的方法。您应该使 WindowedCalculator 扩展 JFrame。

之后,您将希望 WindowedCalculator 调用 Calculator 来执行各种算术运算。

但是,您的 Calculator 类中似乎有错误。当您传入 int 值时,它们将作为值传递。当您执行类似的操作时SolveTEMP += TEMP,您只是在方法中本地更改 SolveTEMP 的值。您可能应该将其更改为: return SolveTEMP + TEMP;,将方法的返回类型更改为int,并删除您已声明的类级别变量(即 SolveTEMP 和 TEMP)。

于 2013-07-31T14:12:08.843 回答