Java 如何在 NetBeans GUI Builder 中包含自定义面板?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/816286/
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 to include custom panel with NetBeans GUI Builder?
提问by Curyous
I have written a class that extends JPanel. Is it possible to use this in the NetBeans GUI Builder and have it survive all of the automatic code generation?
我编写了一个扩展 JPanel 的类。是否可以在 NetBeans GUI Builder 中使用它并让它在所有自动代码生成中幸存下来?
I have used the customised code option in the GUI builder to instantiate the object as the new class, but it seems like the declaration can't be changed from JPanel, so only methods that I have overridden get called, I can't call new ones that are not present in JPanel.
我在 GUI builder 中使用了自定义代码选项将对象实例化为新类,但似乎无法从 JPanel 更改声明,因此只有我覆盖的方法会被调用,我不能调用 new JPanel 中不存在的那些。
采纳答案by Curyous
Simply drag the class from the projects tree on to the form in the GUI designer.
只需将该类从项目树拖到 GUI 设计器中的表单上即可。
Just like it says in stackoverflow question 691890.
回答by Michael Myers
You can use the Palette Manager to add your component to the palette, then you can use it in the GUI builder as you would any other class.
See How can I use a custom subclass of a Swing component?in the NetBeans Wiki.
您可以使用调色板管理器将您的组件添加到调色板,然后您可以像使用任何其他类一样在 GUI 构建器中使用它。
请参阅如何使用 Swing 组件的自定义子类?在 NetBeans Wiki 中。
回答by JRL
Just provide a public no-arg constructor for your class. You can then add the class to the Palette by right clicking on it and selecting Add to Palette.
只需为您的类提供一个公共的无参数构造函数。然后,您可以通过右键单击该类并选择“添加到调色板”将其添加到调色板。