How can i solve the 'cannot find symbol' problem?

1.5k Views Asked by At

If I compile this, I get 'cannot find symbol' errors with the setDefaultCloseOperation, setSize and setVisible. My problem is I don't understand why. and This is the part of myhomework. But I can't even start next part Because of this problem

//AdderSubtracterFrame.java
//This class displays a Frame which can add or subtract two numbers
import javax.swing.JFrame;
public class AdderSubtracterProgram
{
    public static void main(String [] args)
    {
        AdderSubtracterFrame frame1 = new AdderSubtracterFrame("Adder and Subtracter");

        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.setSize(400,100);
        frame1.setVisible(true);
    }
}

I add AdderSubtracterFrame code two classes are in the same project. This project is to make a calculator

//AdderSubtracterFrame.java
//This class displays a Frame which can add or subtract two numbers

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

import java.awt.GridLayout;
import java.awt.FlowLayout;

public class AdderSubtracterFrame
{
    private JTextField num1TextField;
    private JTextField num2TextField;
    private JTextField resultTextField;

    private JButton clearButton;
    private JButton addButton;
    private JButton subtractButton;
    private JButton buttonPanel;

    //Constructor
    public AdderSubtracterFrame(String title)
    {
        //Set the title of the AdderSubtracterFrame by
        //using the superclass JFrame constructor
        super();

        //Set the JFrame to be a 2*2 Grid
        //Set a gap of 5pixels between each row and column.
        setLayout(new GridLayout(2,2,5,5));

        //Create and instance of the components appearing in the 2*2 grid
        num1TextField = new JTextField("0",5);//Begin with 0
        num2TextField = new JTextField("0",5);//Begin with 0
        resultTextField = new JTextField((5));//Begin empty
        buttonPanel = getButtonPanel();

        //Add components to 2*2 Grid
        add(num1TextField);
        add(num2TextField);
        add(resultTextField);
        add(buttonPanel);
    }

    //Create and return a panel containing the buttons
    private JPanel getButtonPanel()
    {
        JPanel myPanel = new JPanel();
        myPanel.setLayout(new FlowLayout());

        //Create an instance of each button
        addButton = new JButton("+");
        subtractButton = new JButton("-");
        clearButton = new JButton("CLEAR");

        //Add the 3button to myPanel
        myPanel.add(addButton);
        myPanel.add(subtractButton);
        myPanel.add(clearButton);

        return myPanel;

    }
}
2

There are 2 best solutions below

2
On

The main problem is, that AdderSubtracterFrame does not extend JFrame. Therefore, you can't use JFrame functionality like setVisible().

Declare your AdderSubtracterFrame like this to fix:

public class AdderSubtracterFrame extends JFrame {

Some other things I found:

  • You call super() in your constructor but you don't pass the title. Change to super(title).
  • Don't use fixed sizes in your Java Swing GUI. Use layout managers (like you do) and call pack() on the frame to layout the frame according to the components inside the frame.
  • The Java Swing GUI should be running on the Event Dispatch Thread (EDT). See Concurrency in Swing.
0
On

Make AdderSubtracterFrame extend JFrame, you only have to change this one line of code from

public class AdderSubtracterFrame {

to

public class AdderSubtracterFrame extends JFrame {