简体   繁体   English

在另一个类的框架中启用Jbutton-GUIBuilder

[英]Enabling Jbutton in frame from another class - GUIBuilder

I have created a JFrame using Netbeans IDE. 我已经使用Netbeans IDE创建了一个JFrame。 I have disabled the submit button once it is pressed to process one action which i dealt in another class. 一旦按下它来处理我在另一堂课中处理的一个动作,我就禁用了提交按钮。 If the action successfully completed i need to enable a button named Next in that frame and if it fails i need to enable Submit button again in that same frame. 如果操作成功完成,则需要在该框架中启用一个名为“下一步”的按钮,如果失败,则需要在同一框架中再次启用“提交”按钮。 since they were private i cant access from other class. 由于它们是私人的,因此我无法从其他班级访问。 If i create object for that Jframe class button has not been enabled and i think entire frame has been called once again by Calling the function Initcomponents(). 如果我没有为该Jframe类按钮创建对象,并且我认为通过调用函数Initcomponents()再次调用了整个框架。

1) how to get enable the button in that frame after some action has been carried over in another class. 1)如何在另一个类中执行了某些动作后,启用该框架中的按钮。

Normally, I normal provide a means by which the main form can register a ChangeListener to the child form. 通常,我通常提供一种方法,通过该方法主窗体可以将ChangeListener注册到子窗体。

I then supply a method in the child form that returns a boolean indicating that the required information has been filled or (ie isFormValid ) 然后,我在子窗体中提供一个方法,该方法返回一个boolean值,该boolean指示已填充了所需的信息,或者(即isFormValid

When any particular part of the child form changes, it fires a change event, notifying the registered listeners that the child form has changed (in this case the parent form). 当子窗体的任何特定部分发生更改时,它将触发一个更改事件,通知已注册的侦听器该子窗体已更改(在本例中为父窗体)。

The parent form then checks the state of the child form and can make dissensions about what it should do... 然后,父表单会检查子表单的状态,并可能对应采取的措施产生分歧。

This is the basic concept of an Observer Pattern , which is seen through out the Swing API. 这是观察者模式的基本概念,可以通过Swing API看到。

It decouples you code and prevents unnecessary exposure of objects (you don't want the child form to have the ability to modify the parent form :P) 它解耦了您的代码,并防止了不必要的对象暴露(您不希望子窗体具有修改父窗体:P的能力)

You need a way to access the buttons, or rather a method from your other class that will enable the buttons for you. 您需要一种访问按钮的方法,或者是您其他类中的一种将为您启用按钮的方法。

Or you can have a return value indicating success or failure and depending on that you can change the button status form the actionPerformed method. 或者,您可以具有一个指示成功或失败的返回值,并可以根据该值从actionPerformed方法更改按钮状态。

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

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