GUI not adding panels

beginner using GUI and cannot figure out why this is not adding the fields in my GUI. It opens the frame but it is empty. No errors are flagged either. Any help is appreciated.

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

public class Project3 extends JFrame {

    public Project3() {

        //set layout
        setLayout(new GridLayout(5, 2, 5, 5));

        //add radio panel
        JPanel radioPanel = new JPanel();

        //set Radio Buttons
        JRadioButton iter = new JRadioButton("Iteration", true);
        JRadioButton recur = new JRadioButton("Recursion", true);

        //add radio buttons to group
        ButtonGroup radioGroup = new ButtonGroup();
        radioGroup.add(iter);
        radioGroup.add(recur);
        radioPanel.add(iter);
        radioPanel.add(recur);
        recur.setSelected(true);

        //add another panel
        JPanel secondPanel = new JPanel();
        secondPanel.setLayout(new GridLayout(4, 2, 5, 5));

        //add text fields and button
        JLabel enter = new JLabel("Enter n:");
        JTextField eight = new JTextField(8);
        //?????????????????????????????
        JButton compute = new JButton("Compute");
        JLabel result = new JLabel("Result:");
        JTextField eight1 = new JTextField(8);
        JLabel efficiency = new JLabel("Efficiency:");
        JTextField eight2 = new JTextField(8);

        //add items to panel
        secondPanel.add(enter);
        secondPanel.add(eight);
        secondPanel.add(compute);
        secondPanel.add(result);
        secondPanel.add(eight1);
        secondPanel.add(efficiency);
        secondPanel.add(eight2);

        add(radioPanel);
        add(secondPanel);

    }

    public static void main(String[] args) {

        // Create frame
        JFrame frame = new JFrame("Project 3");
        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }

}

Answer

You just create a plain JFrame. You need to create an instance of the Project3 class you wrote.

I.e.

public static void main(String[] args) {

    // Create frame
    Project3 frame = new Project3();
    frame.setTitle("Project 3");
    frame.setSize(400, 300);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

Leave a Reply

Your email address will not be published. Required fields are marked *