[英]How can I enable a text field when a button is clicked?
before I start, I'm a beginner programmer. 在开始之前,我是一名初学者。
How can I enable a text field when a button is clicked. 单击按钮时如何启用文本字段。
I have two frames, one that has the JFields and the other for the exception. 我有两个框架,一个具有JFields,另一个具有例外。 When the exception occurs > setEditable(false) but what statement should I make to enable the JFields once the user click on okay button -that i've made in the exception-?
当异常发生时> setEditable(false),但是一旦用户单击“确定”按钮(我在异常中创建的) ,我应该做出什么声明来启用JFields?
I've tried to add static boolean to exception frame, and inside the action performed of this class I initialized that boolean to true . 我尝试将静态布尔值添加到异常框架,并且在此类执行的动作中,我将该布尔值初始化为true 。
in the other class, I added an if statment, if that boolean is true, then setEditable(true) 在另一个类中,我添加了一个if语句,如果该布尔值为true,则setEditable(true)
-========- The point of this program, that when the exception occurs the user cannot enter anything in the fields until he closes the exception window. -=========-此程序的要点是,发生异常时,用户只有在关闭异常窗口后才能在字段中输入任何内容。
I wish you'd help me. 我希望你能帮助我。
With all love, programmers. 怀着所有的爱,程序员。
The code of action performed for THE EXCEPTION WINDOW FRAME ( having Okay button. ) 对“例外窗口”框架执行的操作代码(具有“确定”按钮。)
public void actionPerformed(ActionEvent e){
{
allow=true; //static boolean
Container TheFrame = OKButton.getParent();
do TheFrame = TheFrame.getParent();
while (!(TheFrame instanceof JFrame));
((JFrame) TheFrame).dispose();
}
The code of action performed for THE MAIN PROGRAM (having three fields, an exception will occur once the user enters non digits ) I added some comments to clarify. 我对主要程序执行的操作代码(具有三个字段,一旦用户输入非数字将发生异常),我添加了一些注释来澄清。
public void actionPerformed(ActionEvent event) {
try{
r =Double.parseDouble(RField.getText());
s=Double.parseDouble(SField.getText());
h=Double.parseDouble(HField.getText());
Cone C = new Cone(r,s,h);//class cone
if (event.getSource() instanceof JButton) {
JButton clickedButton = (JButton) event.getSource();
if (clickedButton == VolumeButton) {
Result.append("VOLUME = "+C.volume()+ "\n");
ifV= true;//this's for clearing the fields for new entries.
}
if (clickedButton == AreaButton) {
Result.append("SURFACE AREA = "+C.surfaceArea()+ "\n");
ifA= true;//this's for clearing the fields for new entries.
}
if(ifA&&ifV){ // clearing the fields for new entries.
SField.setText(CLEAR);
HField.setText(CLEAR);
RField.setText(CLEAR);
ifV=false; ifA= false;}
}
SList.addShape(C);
}
catch(NumberFormatException e){
//Object of type "Exception__" already created
Ex.setVisible(true);//class "Exception__" is the one i've made for Exception window
SField.setText(CLEAR);
HField.setText(CLEAR);
RField.setText(CLEAR);
SField.setEditable(false);
HField.setEditable(false);
RField.setEditable(false);
}/*here, if the user clicked on -that okay in Exception window-
and variable allow initialized to "true" those statements should extend. I guess?
- everything worked correctly except for this ?*/
if(Ex.allow){
SField.setEditable(true);
HField.setEditable(true);
RField.setEditable(true); }
}
THANK YOU ALL IT FINALLY WORKED. 谢谢您的全部工作。
I added 我加了
Ex.allow(SField,HField,RField);
to the catch. 抓住。
and added this method in class Exception__: 并在Exception__类中添加了此方法:
public void allow(JTextField js,JTextField jh,JTextField jr){
HField =jh;
SField =js;
RField =jr;
}
finally, to the action performed of class Exception__: 最后,对Exception__类执行的操作:
SField.setEditable(true);
HField.setEditable(true);
RField.setEditable(true);
WOHOOOO. 哇哦 It feels so awesome lol.
感觉真棒。 Thanks all.
谢谢大家 should I delete my question or leave it for others who might face the same problem as mine?
我应该删除我的问题还是将其留给可能与我面临相同问题的其他人? :P
:P
Your question needs a lot more detail. 您的问题需要更多细节。 But if all you want to to show an 'exception window' and allow the user to do anything else only after she dismisses this window, I think all you need is a MessageDialog: See
JOptionPane
但是,如果您只想显示一个“例外窗口”并允许用户仅在关闭该窗口后才能执行其他操作,那么我认为您所需要的只是一个MessageDialog:请参见
JOptionPane
If you need more details to be displayed you can create your own modal JDialog
. 如果需要显示更多详细信息,可以创建自己的模态
JDialog
。
See How to Make Dialogs 请参阅如何制作对话框
Make the text field hiden by writing: 通过输入以下内容隐藏文本字段:
jTextfield.setVisible(fasle); jTextfield.setVisible(fasle);
in the constructor of your form code. 在表单代码的构造函数中。 than use the button event " Action -> Action Performed " and write the code:
而不是使用按钮事件“操作->执行的操作”并编写代码:
jTextfield.setVisible(true); jTextfield.setVisible(true);
and thus your text field will be visible only after the button will be clicked. 因此您的文本字段仅在单击按钮后可见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.