Search code examples
javaswingjframejtextfield

Program with JFrame won't start


I was trying to write a program, where you put some numbers in a JTextField an then it does something with it. I tried figuring out how to even make an input possible. But the way I'm trying it doesn't work despite Eclipse showing no errors. And yes, I know there is no way to stop this program but this is just a test.

import javax.swing.*;

    public class NotenEingabe  extends JFrame {     

    public static void main(String[]args) {
        JFrame frame = new JFrame();

        JPanel panel = new JPanel();

        JLabel label = new JLabel("text");

        JTextField field = new JTextField("text");

        panel.add(label);
        panel.add(field);

        frame.setTitle("Grade input");
        frame.pack();
        frame.add(panel);
        frame.setVisible(true);
    }
}

I hope it is a real problem and not simply my tiredness.


Solution

  •     frame.setVisible(true);
        frame.add(panel);
    

    Components should be added to the frame BEFORE the frame is made visible.

    The layout manager is not invoked so the components have a size of (0, 0) which means there is nothing to paint.

    frame.setTitle("Grade input");
    frame.add(panel);
    //frame.setSize(700, 700);
    frame.pack();
    frame.setVisible(true);