简体   繁体   English

Java Swing:JPanel仅在最大化JFrame窗口或更改其大小时添加组件

[英]Java Swing: JPanel only adds components when JFrame window is maximized or it's size changed

I have a Java program that creates a JFrame, a JPanel, and several JTextAreas. 我有一个Java程序,它创建一个JFrame,一个JPanel和几个JTextAreas。 I set the text of the JTextAreas and then add them to the JPanel. 我设置了JTextAreas的文本,然后将它们添加到JPanel。 When the program loads, I only see the empty JFrame. 程序加载时,我只看到空的JFrame。

However, if I maximize, or change the size of the program window, the JTextAreas, and their respective texts appear on the screen. 但是,如果我最大化或更改程序窗口的大小,则JTextAreas及其各自的文本将显示在屏幕上。

How can I correct this so that the program loads, with the text areas populated, without the need to mess with the window? 我该如何纠正这个问题,以便程序加载文本区域而无需弄乱窗口? My apologies in advance for being a Swing noob. 谨向成为Swing新手致歉。

My guess: You need to call setVisible(true) on the JFrame after adding all components to it. 我的猜测: 将所有组件添加到JFrame 之后 ,需要在JFrame上调用setVisible(true)

Next time, or this time if this doesn't answer your question, please post pertinent code. 下次,或者如果这次仍不能回答您的问题,请发布相关代码。 We're terrible at reading minds or guessing at code not shown. 在阅读思想或猜测未显示的代码时,我们很糟糕。 Please check out the Jon Skeet blog: Asking the Perfect Question . 请查看Jon Skeet博客: 提出完美问题 It will give you great suggestions on how to create better more complete questions and thus get better answers. 它会为您提供有关如何创建更好的更完整的问题,从而获得更好的答案的很好的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM