java 无论如何要“打包”一个JPanel?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26208723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:29:27  来源:igfitidea点击:

Is there anyway to "pack" a JPanel?

javajframejpaneldimensionspack

提问by MattS

Is there any thing I can do to make my JPanel packlike a JFrame, or do something like that. I want to do this instead of giving it dimensions. Please comment if any addition info is needed. Thanks.

有什么我可以做的事情让我的 JPanelpack像一个 JFrame,或者做类似的事情。我想这样做而不是给它尺寸。如果需要任何附加信息,请发表评论。谢谢。

回答by ControlAltDel

If I understand you correctly, your JPanel is not being automatically (re)sized, correct? In that case, you can use Component.validate (or JComponent.revalidate())

如果我理解正确,您的 JPanel 不会自动(重新)调整大小,对吗?在这种情况下,您可以使用 Component.validate(或 JComponent.revalidate())

回答by georges abitbol

Please try JPanel.updateUIand let me know if that helps.

请尝试JPanel.updateUI,如果有帮助,请告诉我。

You should make GUI calls in the Event Dispatcher Thread:

您应该在 Event Dispatcher Thread 中进行 GUI 调用:

EventQueue.invokeLater(() -> {
    someJPanel.updateUI();
});