Why are my items not showing up in JFrame?

后端 未结 6 1065
日久生厌
日久生厌 2020-11-29 10:39

I\'m fairly new to JFrame and I want to know why my items are not showing up on the window. I know i dont have a ActionHandler but I just want my textfield\'s to show up on

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-29 11:40

    but when i run it i get this:

    You get an empty screen because you add the components to the frame after the frame is visible.

    1. As has already been suggested you need to use an appropriate layout manager. FlowLayout is the easiest to start with.
    2. invoke setVisible(true) AFTER adding the components to the frame.

    So the code should be more like:

    panel.add(...);
    panel.add(...);
    add(panel);
    pack();
    setVisible(true);
    

提交回复
热议问题