简体   繁体   English

访问在方法中创建的Swing组件

[英]Accessing swing components created in method

I have a class that builds my gui. 我有一堂课来建立我的GUI。 One part of the gui is a jtabbed pane, the panels for this are created in a method, they are all the same and have a jtextarea and some jbuttons. gui的一部分是一个jtabbed窗格,其面板是用一种方法创建的,它们都是相同的,并具有jtextarea和一些jbutton。

How can I access the components in each tab from other methods/classes? 如何从其他方法/类访问每个选项卡中的组件?

You can put this components into class fields. 您可以将此组件放入类字段。 For example if you need to access button make field 例如,如果您需要访问按钮制作字段

private Button myButton;

Create it in method createUI() and then use it in method doSomething() . 在方法createUI()创建它,然后在方法doSomething()使用它。

Other way is to store the container only (eg panel) and then access its components using panel.getComponents() . 另一种方法是仅存储容器(例如面板),然后使用panel.getComponents()访问其组件。 Use this method only if you really have very similar components and do not have to distinguish among them. 仅当您确实具有非常相似的组件并且不必区分它们时才使用此方法。 For example you want to change color of all components etc. 例如,您想更改所有组件的颜色等。

You can use a combination of getTabCount() , getTitleAt() , indexOfTab() , and getComponent() . 您可以结合使用getTabCount()getTitleAt()indexOfTab()getComponent() See more info here 在这里查看更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM