java 在 NetBeans GUI 设计器中重用 JPanel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/691890/
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
Reusing a JPanel in NetBeans GUI Designer
提问by Andrew Coleson
This is in NetBeans 6.5, Java 6.
这是在 NetBeans 6.5、Java 6 中。
I have the following hierarchy in the NetBeans GUI Designer:
我在 NetBeans GUI 设计器中有以下层次结构:
JFrame
JTabbedPane
JPanel X
<...>
JPanel
JButton
JPanel Y
<...>
JButton
Question:
问题:
JPanel Y is identical to JPanel X, so I'd like to simply reuse JPanel X in both places, but how do I do this inside the GUI Builder?
JPanel Y 与 JPanel X 相同,所以我想在两个地方简单地重用 JPanel X,但我如何在 GUI Builder 中执行此操作?
Attempts:
尝试:
I tried copy-pasting JPanel X, but it creates a full "deep" copy (JPanel X1, etc), duplicating everything in JPanel X.
我尝试复制粘贴 JPanel X,但它创建了一个完整的“深度”副本(JPanel X1 等),复制了 JPanel X 中的所有内容。
Some googling indicated it might be possible to add it to the Palette, but I haven't found a way to add a simple JPanel to the palette (as opposed to a complete JFrame).
一些谷歌搜索表明可能可以将它添加到调色板,但我还没有找到一种方法将简单的 JPanel 添加到调色板(而不是完整的 JFrame)。
回答by James Schek
Create a separate JPanel class. Customize JPanel as needed. You can then drag-and-drop the JPanel class onto the Form Designer. This is exactly what I do.
创建一个单独的 JPanel 类。根据需要自定义 JPanel。然后您可以将 JPanel 类拖放到表单设计器上。这正是我所做的。
Alternatively, you can click on Use Bean and then type in the name of the class.
或者,您可以单击“使用 Bean”,然后键入类的名称。
Alternatively, you can do an Add from the pallete and it will scan your JAR for any "beans". It should pick up your custom JPanel as well.
或者,您可以从托盘中执行添加,它会扫描您的 JAR 以查找任何“bean”。它也应该选择您的自定义 JPanel。

