JFrame not presenting any Components

后端 未结 4 1740
遇见更好的自我
遇见更好的自我 2020-12-04 03:00

I am using the following code to create a very simple JFrame, but for some reason it doesn\'t show any components, just a blank frame. Why is this happening? I

相关标签:
4条回答
  • 2020-12-04 03:38

    Better your first add components to your variable "panel" and add then your finished panel to the .getContentPane().add().

    And the most important issue is that you better call frame.setVisible(true); at the end of your method.

    0 讨论(0)
  • 2020-12-04 03:41

    Put frame.setVisible(true); after adding components to JFrame, and it will show all the added components. Moreover, you should use specific layout rather than setting bounds for components. You can use a Layout Manager.

    0 讨论(0)
  • 2020-12-04 03:45

    You have to move frame.setVisible(true); to the end of the method; the visibility must be set to true after you have added the components.

    Alternatively, you can add the following to the end of your method:

    frame.revalidate();
    frame.repaint();
    

    to revalidate and repaint the frame with the newly added components although I recommend the former method.

    0 讨论(0)
  • 2020-12-04 04:00

    you can add this at the end;

    frame.pack()
    
    0 讨论(0)
提交回复
热议问题