Formulario.java:28: error: cannot find Symbol Botaoaction action = new Botaoaction(t);

Asked

Viewed 160 times

0

Ah, so I was creating a show with Swing, heunderlined text reads the name that the user puts, and then shows it to him, very simple thing, I’m still very new, but when I created the class "Botaoaction" (which was the one that showed the user’s name) I started the code and gave the following error

    Formulario.java:28: error: cannot find symbol
    BotaoAction action = new BotaoAction(t);
    ^

code used:

package swing;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Formulario{

    public static void main(String[] args){

       JFrame f = new JFrame();
       f.setTitle("Janela");
       f.setSize(300,200);
       f.setLocation(500,300);
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       JPanel p = new JPanel();

       JLabel one = new JLabel("Digite seu nome:");
       p.add(one);

       JTextField t = new JTextField(10);
       p.add(t);

       JButton b = new JButton("OK");
       BotaoAction action = new BotaoAction(t);
       b.addActionListener(action);
       p.add(b);

       f.add(p);
       f.setVisible(true);

     }

 }

code I used in the class "Botaoaction":

package swing;

public class BotaoAction implements ActionListener{

    private JTextField t;
    public BotaoAction(JTextField t){
        this.t = t;

    }

    public void actionPerformed(ActionEvent e){

        String nome = t.getText();

        JOptionPane.showMessageDialog(null, "Foi digitedo" + nome);

    }

}

1 answer

0

Here I tested and worked normally. If you are running through the command terminal or prompt, compiling and running the class within the "swing" directory itself, try using the command below:

java -cp ../ swing.Formulario

this will set the directory just above as the root of the run, and so you can run the class by informing the package of it together.

If it’s just for testing and you don’t really need a package hierarchy now, remove package swing of both classes, recompile and simply execute java Formulario directly from the swing folder, which will work in the same way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.