如何在 Java 运行时将面板动态添加到其他面板?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/169799/
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
How do I dynamically add Panels to other panels at runtime in Java?
提问by BFreeman
I'm trying to get into java again (it's been a few years). I never really did any GUI coding in java. I've been using Netbeans to get started with this.
我正在尝试再次进入 Java(已经有几年了)。我从来没有真正用 Java 做过任何 GUI 编码。我一直在使用 Netbeans 来开始这个。
When using winforms in C# at work I use a usercontrols to build parts of my UI and add them to forms dynamically.
在工作中在 C# 中使用 winforms 时,我使用用户控件来构建我的 UI 部分并将它们动态添加到表单中。
I've been trying to use JPanels
like usercontrols in C#. I created a JPanel
form called BlurbEditor
. This has a few simple controls on it. I am trying to add it to another panel at run time on a button event.
我一直在尝试JPanels
在 C# 中使用类似用户控件。我创建了一个JPanel
名为BlurbEditor
. 这有一些简单的控件。我试图在按钮事件的运行时将它添加到另一个面板。
Here is the code that I thought would work:
这是我认为可行的代码:
mainPanel.add(new BlurbEditor());
mainPanel.revalidate();
//I've also tried all possible combinations of these too
//mainPanel.repaint();
//mainPanel.validate();
This unfortunately is not working. What am I doing wrong?
不幸的是,这不起作用。我究竟做错了什么?
采纳答案by BFreeman
I figured it out. The comments under the accepted answer here explain it: Dynamically added JTable not displaying
我想到了。此处接受的答案下的评论对此进行了解释: 动态添加的 JTable 不显示
Basically I just added the following before the mainPanel.add()
基本上我只是在 mainPanel.add() 之前添加了以下内容
mainPanel.setLayout(new java.awt.BorderLayout());
回答by Daniel Spiewak
Try mainPanel.invalidate()
and then if necessary, mainPanel.validate()
. It also might be worth checking that you're doing this all in the event dispatch thread, otherwise your results will be spotty and (generally) non-deterministic.
尝试mainPanel.invalidate()
,然后在必要时,mainPanel.validate()
。也可能值得检查您是否在事件调度线程中执行所有这些操作,否则您的结果会参差不齐且(通常)不确定。
回答by Daniel Hiller
As with all swing code, don't forget to call any gui update within event dispatch thread. See thisfor why you must do updates like this
与所有 Swing 代码一样,不要忘记在事件调度线程中调用任何 gui 更新。看到这个为什么你必须做这样的更新
// Do long running calculations and other stuff outside the event dispatch thread
while (! finished )
calculate();
SwingUtilities.invokeLater(new Runnable(){
public void run() {
// update gui here
}
}
回答by Leigh Caldwell
Swing/AWT
components generally have to have a layout before you add things to them - otherwise the UI
won't know where to place the subcomponents.
Swing/AWT
在向组件添加内容之前,组件通常必须具有布局 - 否则UI
将不知道将子组件放置在哪里。
BFreeman has suggested BorderLayout
which is one of the easiest ones to use and allows you to 'glue' things to the top, bottom, left, right or center of the parent.
BFreeman 建议使用BorderLayout
哪个是最容易使用的方法之一,它允许您将东西“粘合”到父项的顶部、底部、左侧、右侧或中心。
There are others such as FlowLayout
which is like a textarea
- it adds components left-to-right at the top of the parent and wraps onto a new row when it gets to the end.
还有其他的,例如FlowLayout
which 就像 a textarea
- 它在父级的顶部从左到右添加组件,并在到达末尾时换行到新行。
The GridBagLayout
which has always been notorious for being impossible to figure out, but does give you nearly all the control you would need. A bit like those HTML
tables we used to see with bizarre combinations of rowspan, colspan, width and height attributes - which never seemed to look quite how you wanted them.
在GridBagLayout
这一直是臭名昭著的是无法弄清楚,但不会给你几乎所有你需要的控制。有点像HTML
我们以前看到的那些带有 rowspan、colspan、width 和 height 属性的奇怪组合的表格——它们似乎从来都不是你想要的样子。
回答by Flavio Lozano Morales
mainPanel.add(new BlurbEditor());
mainPanel.add(new BlurbEditor());
mainPanel.validate();
mainPanel.validate();
mainPanel.repaint();
mainPanel.repaint();
回答by Dimitris
I was dealing with similar issue, I wanted to change the panel contained in a panel on runtime
After some testing, retesting and a lot of failing my pseudo-algorithm is this:
我正在处理类似的问题,我想在运行时更改面板中包含的面板
经过一些测试、重新测试和很多失败,我的伪算法是这样的:
parentPanel : contains the panel we want to remove
childPanel : panel we want to switch
parentPanelLayout : the layout of parentPanel
editParentLayout() : builds parentPanel with different childPanel and NEW parentPanelLayout every time
parentPanel : 包含我们要移除的面板
childPanel : 我们要切换的面板
parentPanelLayout : parentPanel 的布局
editParentLayout() : 每次使用不同的 childPanel 和 NEW parentPanelLayout 构建 parentPanel
parentPanel.remove(childPanel);
editParentLayout();
parentPanel.revalidate();
parentPanel.repaint();