[英]Cannot find symbol in main class
Calculator
class: (contains the arithmetic methods) 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
class: (contains the GUI) 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
class: (contains the main method) 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);
}
}
Everything compiles except for Tester.java. 除了Tester.java之外,所有内容都会编译。 It is returning 4 cannot find symbol errors saying
variable calc of the type WindowedCalculator
. 它返回4找不到符号错误,表示
variable calc of the type WindowedCalculator
。
You cannot access instance fields without an instance of the class. 没有类的实例,您无法访问实例字段。
You probably want to make those fields static. 您可能希望将这些字段设为静态。
The methods that you are trying to call on WindowedCalculator are methods of the JFrame class. 您尝试在WindowedCalculator上调用的方法是JFrame类的方法。 You should make WindowedCalculator extend JFrame.
你应该让WindowedCalculator扩展JFrame。
After that, you will want WindowedCalculator to call Calculator to perform your various arithmetic operations. 之后,您将需要WindowedCalculator调用Calculator来执行各种算术运算。
However, it looks like you have errors in your Calculator class. 但是,您的Calculator类似乎有错误。 When you pass in int values, they are passed as values.
传入int值时,它们将作为值传递。 When you do something like
SolveTEMP += TEMP
, you are only changing the value of SolveTEMP locally, within your method. 当您执行
SolveTEMP += TEMP
,您只需在方法中本地更改SolveTEMP的值。 You should probably change that to: return SolveTEMP + TEMP;
您应该将其更改为:
return SolveTEMP + TEMP;
, change the method's return type to int
, and get rid of the class level variables you have declared (that is, SolveTEMP and TEMP). ,将方法的返回类型更改为
int
,并删除已声明的类级变量(即SolveTEMP和TEMP)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.