如何在运行时向使用 Netbeans 可视化编辑器创建的 Swing UI 添加组件?
我目前正在编写一个应用程序,用户在某些时候可以单击在运行时生成的按钮。 我知道如何从头开始编写所有 swing 代码,但我想利用 Netbeans 的可视化编辑器。
生成的 UI 代码进入 initComponents() 方法,我无法修改它,因为它是从可视形式自动重新生成的。
我想要一个在设计时使用可视化编辑器放置的面板,我可以在运行时添加按钮,以便它们很好地适应布局,但我不知道如何以方便的方式访问该面板方式。 此外,除了使用面板之外,可能还有其他方法。
基本上来说:
- 如何在运行时定位 Swing 组件?
- 是否有更好的方法将运行时创建的组件集成到生成的 Swing UI 中?
感谢您的帮助。
I am currently writing an application where the user has, at some point, to click a button which have been generated at run time. I know how to do it when writing all my swing code from scratch, but I'd like to take advantage of Netbeans' visual editor.
The generated UI code goes into an initComponents() method I can't modify since it is regenerated automatically from the visual form.
I'd like to have a panel I place at design time using the visual editor in which I could add the buttons at run time so that they fit nicely in the layout, but I don't know how to access the panel in a convenient way. Besides, there may be another method than using a panel.
So basically :
- How do I locate a Swing component at run time ?
- Is there a better way of integrating components created at run time in a generated Swing UI ?
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NetBeans 生成的 GUI 类将所有组件存储在私有变量中。 您可以将一个方法添加到返回面板的生成类中,即使您进行其他设计,它也会保留。
如果您要使用生成的 UI,那么最好在该 UI 中使用 JPanel 来为您自己的组件“开辟”空间。 否则,您将不得不担心组件如何影响 UI 放置的组件的布局。
NetBeans-generated GUI classes store all the components in private variables. You can add a method into the generated class that returns the panel, and it will remain even as you do additional design.
If you're going to use a generated UI, then it's probably best to use a JPanel within that UI to "carve out" space for your own components. Otherwise, you'll have to worry about how your components affect the layout of the components placed by the UI.
仅仅因为您使用 NetBeans 生成的 GUI 类并不意味着您必须使用面板的组布局。 我发现将其切换为 BorderLayout 特别是在我想要添加一些动态用户界面代码的情况下很有帮助。
Just because you are using NetBeans generated GUI classes doesn't mean that you have to use the Group layout for the panels. I find that switching it to a BorderLayout helps especially in cases where I want to add some dynamic user interface code.
可以通过右键单击 GUI 设计器中的组件,选择属性并点击“源”选项卡,或者右键单击组件并选择“修改源”(或类似的内容),将私有更改为受保护/公共设置适当的访问修饰符。
或者只是通过 getXYZComponent() 方法导出它们。
定位组件应该太困难了,因为您与设计人员一起构建了它,因此了解每个组件。
例如,如果您有一个 JTabbedPane 并希望在用户点击按钮或类似内容时向其中添加选项卡,您只需发出 myJTabbedPane.add(myCustomComponent); 瞧,一个新选项卡出现了。
还可以使用上面提到的“修改源”对话框来修改自动生成的代码和/或用于自动生成的代码,这非常有用。
It is possible to change private to protected/public by either right clicking on a component in the GUI-Designer, choosing properties and hitting the Source-tab or right clicking on a component and choosing "Modify Source" (or something like that) and setting the appropriate access modifier.
Or just export them via a getXYZComponent() method.
Locating the component should provide as being too difficult, as you built it with the designer and thus know each component.
For example, if you had a JTabbedPane and wanted to add tabs to it when the user hits a button or something like that, you would simply issue myJTabbedPane.add(myCustomComponent); et voila, a new tab appears.
It is also possible to modify the auto-generated code and/or the code used for auto-generation by using the "Modify source" dialog mentioned above, which can be really useful.