user2288575
user2288575

Reputation: 23

My GUI is not showing up fully

When I run my program, it only shows a few lines, and then it shows gray under it. Can someone explain to me why this happens? I wanted the grid layout to have 8 rows, which should include the labels and text boxes. I am not sure why only a few of them appear.

public class Application extends JFrame {
    private JPanel panel;
    private JLabel label1, label2, label3, label4, label5, label6, label7,
            label8;
    private JTextField text1, text2, text3, text4, text5, text6, text7, text8;

    public Application() {

        JFrame gui = new JFrame();
        gui.setLayout(new GridLayout(8, 2));
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        gui.setTitle("Vacation Expenses");
        gui.setSize(500, 500);

        panel = new JPanel();
        gui.add(panel);

        label1 = new JLabel("Number of days on the trip");
        label2 = new JLabel("Amount of airfare");
        label3 = new JLabel("Amount of car rental fees");
        label4 = new JLabel(
                "Number of miles driven, if a private vehicle was used");
        label5 = new JLabel("Amount of parking fees, if any");
        label6 = new JLabel("Amount of taxi charges, if any");
        label7 = new JLabel("Conference or seminar registration fees, if any");
        label8 = new JLabel("Lodging charges, per night");

        text1 = new JTextField("0", 10);
        text2 = new JTextField("0", 10);
        text3 = new JTextField("0", 10);
        text4 = new JTextField("0", 10);
        text5 = new JTextField("0", 10);
        text6 = new JTextField("0", 10);
        text7 = new JTextField("0", 10);
        text8 = new JTextField("0", 10);

        panel.add(label1);
        panel.add(text1);
        panel.add(label2);
        panel.add(text2);
        panel.add(label3);
        panel.add(text3);
        panel.add(label4);
        panel.add(text4);
        panel.add(label5);
        panel.add(text5);

        // JButton button = new JButton("Button");
        // panel.add(button);

        gui.setVisible(true);

    }

    public static void main(String[] args) {
        new Application();
    }
}

Upvotes: 0

Views: 188

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209102

You should be setting the GridLayout to your panel and not the frame. The panel is the container for the components, so should be the one with the GridLayout

Get rid of gui.setLayout(new GridLayout(8, 2));

And use panel = new JPanel(new GridLayout(8, 2));


Side Notes

  • Also note you haven't added all your components. You've only added five of each. You're forgetting to add the other three.

  • Also, your class is already a JFrame. There's no need to create another one. Choose one or the other. Either use the instance JFrame and don't extends JFrame or extend JFrame and don't use the extra instance. I'd go with the former.

  • Also, it's best to pack() your frame, instead of setSize(). The pack() should be done after adding all your components.

  • Also, Swing apps should be run from the Event Dispatch Thread. You can accomplish this by wrapping the main method content in a SwingUtilities.invokeLater(...). See more at Initial Threads

Upvotes: 2

Related Questions