error: can not find a symbol Java

1

I have a problem.

I want to recover panel2 of this program to open it in another one by:

public JPanel getPanel2()
{
    return panel2;
}

To display that panel2 in the other program I have this:

if(e.getSource()== miCalcu)
            {
                panel.setVisible(false);
                panel= calcul.getPanel2();
                panel.setVisible(true);
                add(panel);
                setVisible(true);

            }

But I need that getPanel2 that does not recognize me.

I get the error mentioned above. I have used this same function for other programs and open these, in another click on a JMenuItem , the rest, work perfectly with that instruction, I recover the panel2 without problem and with the same instruction. But this brought me this error. Here a fragment of the code (436 lines in total). If necessary, I write everything.

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.Panel;

public class Calculadora extends JFrame implements ActionListener
{
    private JTextField tfNumero = new JTextField(8);

    private JButton bSuma, bResta, bMultiplica, bDivide, bIgual, bClear, bPunto;
    private JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
    private JButton bFactorial, bExpo, bRaiz, bCuadrado;
    private Panel panel1, panel2;

    private float n1, n2;
    private String operacion = "";
    private String strNumero = "";
    private int base;
    int sum,c;
    int x;
    String resSuma,resRes,resMul,resDiv,resExpo;

    public Calculadora()
    {
        super("Calculadora");

        panel1 = new Panel();
        panel2 = new Panel();
        panel1.setLayout(new GridLayout(1,2));
        panel2.setLayout(new GridLayout(5,4));


        panel1.add(tfNumero);

        bClear = new JButton("clear");
        panel1.add(bClear);     


        b7 = new JButton("7");
        panel2.add(b7);

        b8 = new JButton("8");
        panel2.add(b8);

        b9 = new JButton("9");
        panel2.add(b9);

        bSuma = new JButton("+");
        panel2.add(bSuma);

        b4 = new JButton("4");
        panel2.add(b4);

        b5 = new JButton("5");
        panel2.add(b5);

        b6 = new JButton("6");
        panel2.add(b6);

        bResta = new JButton("-");
        panel2.add(bResta);

        b1 = new JButton("1");
        panel2.add(b1);

        b2 = new JButton("2");
        panel2.add(b2);

        b3 = new JButton("3");
        panel2.add(b3);

        bMultiplica = new JButton("x");
        panel2.add(bMultiplica);

        b0 = new JButton("0");
        panel2.add(b0);

        bPunto = new JButton(".");
        panel2.add(bPunto);

        bIgual = new JButton("=");
        panel2.add(bIgual);

        bDivide = new JButton("/");
        panel2.add(bDivide);

        bFactorial = new JButton("x!");
        panel2.add(bFactorial);

        bRaiz = new JButton("sqrt(x)");
        panel2.add(bRaiz);

        bCuadrado = new JButton("x^2");
        panel2.add(bCuadrado);

        bExpo = new JButton("y^x");
        panel2.add(bExpo);


        b0.addActionListener(this);
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        b5.addActionListener(this);
        b6.addActionListener(this);
        b7.addActionListener(this);
        b8.addActionListener(this);
        b9.addActionListener(this);
        bClear.addActionListener(this);
        bSuma.addActionListener(this);
        bResta.addActionListener(this);
        bMultiplica.addActionListener(this);
        bDivide.addActionListener(this);
        bIgual.addActionListener(this);
        bPunto.addActionListener(this);
        bFactorial.addActionListener(this);
        bExpo.addActionListener(this);
        bRaiz.addActionListener(this);
        bCuadrado.addActionListener(this);


        setLayout(new FlowLayout());
        add(panel1);
        add(panel2);
        setSize(350,220);
        setVisible(true);
    }

    public JPanel getPanel2()
            {
                return panel2;
            }
    
asked by Cruzhn 16.03.2018 в 03:08
source

1 answer

0

Acruzand greetings.

The error you mention is about a compilation error , as I mentioned in the comment.

What happens is that you are importing the class java.awt.Panel and you are declaring panel1 and panel2 as that type of Panel , however, in the getPanel2 you are indicating that returns a javax.swing.JPanel . When you see the packages you import, only , java.awt.Panel appears, but javax.swing.JPanel does not appear.

These are your import :

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.Panel;

As you can see, in the end you are using java.awt.Panel , you should use javax.swing.JPanel , so that you compile correctly. What you should do is remove the import from java.awt.Panel and change it to javax.swing.JPanel and also change all the references in your variables and in the constructor.

Variables:

private JPanel panel1, panel2;

And in the constructor:

panel1 = new JPanel();
panel2 = new JPanel();
    
answered by 16.03.2018 / 18:09
source