T
T
Tanqwer2021-08-09 00:02:32
Java
Tanqwer, 2021-08-09 00:02:32

Problem with interactions between classes in different files. There is no connection between the main class of the program and the class with the listener. Where is the mistake?

These classes do not see each other. Where is the mistake? Am I calling an object of another class correctly?

listener

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;

public class CalculatorEngine implements ActionListener {
  char selectedAction;
  JButton clickedButton;
  String buttonName;
  Double buffer;
  Double result; // используемые переменные 
  
  public void actionPerformed(ActionEvent e) { // Листенер
    Calculator clc = new Calculator();
    clickedButton = (JButton) e.getSource();
    buttonName = clickedButton.getText();
    if (buttonName == "+") {
      selectedAction = '+';
      result = Double.parseDouble(clc.fieldNum.getText());
      clc.fieldNum.setText(" ");
    }
  }
}


Error while compiling listener

CalculatorEngine.java:18: error: cannot find symbol
result = Double.parseDouble(clc.fieldNum.getText());
^
symbol: variable fieldNum
location: variable clc of type Calculator
CalculatorEngine.java:19: error: cannot find symbol
clc.fieldNum.setText(" ");
^
symbol: variable fieldNum
location: variable clc of type Calculator
2 errors


Main code

import javax.swing.*;
import java.awt.GridLayout;
import java.awt.BorderLayout;

public class Calculator {
  public JPanel calcPanel() { //метод для создания панелей
  
  // панели 
  JPanel panelMain = new JPanel(); // Главная панель
  panelMain.setLayout(new BorderLayout());
  
  JPanel panelNum = new JPanel(); // Числовая панель
  panelNum.setLayout(new GridLayout(4,3));
  
  JPanel panelMathSymbols = new JPanel();
  panelMathSymbols.setLayout(new GridLayout(4,1));
  
  // Кнопки и текстовое поле
  JTextField fieldNum = new JTextField(30); // Текстовое поле 
  JButton button0=new JButton("0"); // Кнопки
  JButton button1=new JButton("1");
  JButton button2=new JButton("2");
  JButton button3=new JButton("3");
  JButton button4=new JButton("4");
  JButton button5=new JButton("5");
  JButton button6=new JButton("6");
  JButton button7=new JButton("7");
  JButton button8=new JButton("8");
  JButton button9=new JButton("9");
  JButton buttonPoint = new JButton(".");
  JButton buttonEqual=new JButton("=");
  JButton buttonPlus=new JButton("+");
  JButton buttonMinus=new JButton("-");
  JButton buttonDivide=new JButton("/");
  JButton buttonMultiply=new JButton("*");
  
  // Добавление компонентов на панели/схема
  // Добавление во вложенные панели
  panelNum.add(button1);
  panelNum.add(button2);
  panelNum.add(button3);
  panelNum.add(button4);
  panelNum.add(button5);
  panelNum.add(button6);
  panelNum.add(button7);
  panelNum.add(button8);
  panelNum.add(button9);
  panelNum.add(button0);
  panelNum.add(buttonPoint);
  panelNum.add(buttonEqual); // Числовая панель
  
  panelMathSymbols.add(buttonPlus);
  panelMathSymbols.add(buttonMinus);
  panelMathSymbols.add(buttonDivide);
  panelMathSymbols.add(buttonMultiply); // Математические символы
  
  // Заполнение Главной панели
  panelMain.add("North",fieldNum);
  panelMain.add("Center",panelNum);
  panelMain.add("East",panelMathSymbols);
  
  // Добавление листенеров
  CalculatorEngine clcEng = new CalculatorEngine(); // ссылка на класс
  
  buttonPlus.addActionListener(clcEng);
  
  // Возврат главной панели
  return panelMain;
  }
  
  public void calcWindow (JPanel randomPanel) { // создание окна
    JFrame progWin = new JFrame();
    progWin.add(randomPanel);
    progWin.pack();
    progWin.setVisible(true);
  }

  // Старт
  public static void main(String[] args) {
    Calculator c1 = new Calculator();
    JPanel progPanel = c1.calcPanel();
    c1.calcWindow(progPanel);
  }
}

Errors when compiling the main program

.\CalculatorEngine.java:18: error: cannot find symbol
result = Double.parseDouble(clc.fieldNum.getText());
^
symbol: variable fieldNum
location: variable clc of type Calculator
.\CalculatorEngine.java:19: error: cannot find symbol
clc.fieldNum.setText(" ");
^
symbol: variable fieldNum
location: variable clc of type Calculator
2 errors

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-08-09
@Tanqwer

The Calculator class does not have a fieldNum field.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question