在主类中找不到符号

时间:2013-07-31 13:59:17

标签: java variables user-interface package

Calculator class :(包含算术方法)

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的符号错误。

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

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

之后,您需要WindowedCalculator调用Calculator来执行各种算术运算。

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