0

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.

  • *"put some numbers in a JTextField"* Please offer the user a `JSpinner` with `SpinnerNumberModel` instead. *"but it still it isn't working"* So [edit] the question with the current code. – Andrew Thompson Jun 19 '18 at 02:01
  • You should add the panel before you `pack` the frame. It is most likely a small border on the top of the screen that you don't noticed. PS: No need of have `NotenEingabe` extending `JFrame` here. – AxelH Jun 19 '18 at 05:00

2 Answers2

4
    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);
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thank you for the answer but now I set the size to "pack" and added the component before I made the frame visible but it still it isn't working. Is there something else that isn't right? –  Jun 18 '18 at 19:59
  • 1
    Works fine for me. Update you question with the new code and make sure you compile the code – camickr Jun 18 '18 at 20:16
  • *"Works fine for me."* Same here. In fact, even the code exactly as copy/pasted worked here, but that may change according to VM version / mfr / OS. So if it's not working for you, I second the call to post updated code. – Andrew Thompson Jun 19 '18 at 01:59
  • @Paddy is packing the frame BEFORE adding the panel. So it does't work because he didn't use this answer fully. – AxelH Jun 19 '18 at 05:03
  • Thank you all for the help but now it is at least showing an error. I cannot say what the error is in english because I'm using Eclipse in german but is something like: "Main class notenTest.PopupInput could not be found or loaded. Cause: java.lang.ClassNotFoundException: notenTest.PopupEingabe." I just don't know what that really means. And if you are german here is the german error: Fehler: Hauptklasse notenTest.PopupEingabe konnte nicht gefunden oder geladen werden Ursache: java.lang.ClassNotFoundException: notenTest.PopupEingabe –  Jun 19 '18 at 14:31
  • Okay, everything is clear now. The path to the class was just messed up. Still thank you for your efforts! –  Jun 19 '18 at 15:24
-2

First of all when you extend your class from jframe you dont need to instantiate new jFrame just make a new instance of your class then access jframe with that or just delete extend Jframe and use functional approach.

The you didn't set de layout of pane Test this one then you can choose from veriety of layouts

panel.setLayout(newFlowLayout());
  • 2
    (1-) 1) You should not extend JFrame because you are not adding any new functionality to the frame. Creating an instance of JFrame is the proper solution. 2) The default layout manager of a JPanel is the FlowLayout. – camickr Jun 18 '18 at 20:15