user5928703 user5928703 - 1 month ago 10
Java Question

Need help calling a class in a JButton in java

This is a program that is part of a larger overall program. Just running a few test lines of codes just to make sure something works before I get 100 lines down. Kinda of new to programming. What I am trying to do is when the

JButton
in my swing program is pressed it does a variety of things, one is calling a class that takes the text from a
jTextField
and outputs it to a
Jpanel
. The problem I am getting an error saying symbol cannot be found. Here the part of the code that is relevant to my question. I am trying to get (
Name test1 = new Name();
) to run.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JFrame frame1 = new JFrame("Test");
frame1.setVisible(true);
frame1.setSize(800,800);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel label1 = new JLabel("Test run, just checking");

JPanel panel1 = new JPanel();
panel1.add(label1);

Name test1 = new Name();


frame1.add(panel1);


public String Name()
{

String g = jTextField3.getText();

return g;

};

Answer

The line , public String Name() will define a method in Java and not a class. You are using new for a method name , Name test1 = new Name(); which is invalid, new is used to create an instance object of class.

You need to directly call method without new and type of test1 would be a String not Name, String test1 = Name();