[英]Make button recognize text in JTextField
My goal here is to make two checkboxes become enabled when someone types the word "Physique" into a certain JTextField. 我的目标是在有人在某个JTextField中键入“ Physique”一词时启用两个复选框。
SO: 所以:
Press button
Button reads text in JTextField
if text in field is equal to Physique
then enable Checkboxes Three and Four.
However, I cannot make it respond when text is entered in the JTextField 但是,当在JTextField中输入文本时,我无法使其响应
String greatOne = skillGreat1JTextField.getText();
if ( greatOne.equals( "Physique" ) ) {
physStress3CheckBox.setEnabled( true );
physStress4CheckBox.setEnabled( true );
}
This triggers when I press an Apply button, but for some reason. 当我按下“应用”按钮时,这会触发,但是由于某些原因。 This if statement doesn't seem to perform because the Checkboxes never become enabled when Physique is entered into the JTextField
此if语句似乎未执行,因为在将Physique输入JTextField中时,复选框从未启用
Anyone have a clue why this won't perform? 有人知道为什么它不执行吗? I'm at a loss.
我很茫然。
[EDIT] [编辑]
Here's some more of the code. 这是更多代码。 When I press a button named Apply, this should perform.
当我按下名为“应用”的按钮时,这应该执行。
public void applyJButtonActionPerformed( ActionEvent event ) {
String greatOne = skillGreat1JTextField.getText();
if ( greatOne.equals( "Physique" ) ) {
physStress3CheckBox.setEnabled( true );
physStress4CheckBox.setEnabled( true );
}
}
That's the IF statement, here's the code for the button: 那是IF语句,这是按钮的代码:
applyJButton.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event ) {
applyJButtonActionPerformed( event );
}
}
);
Also, in reply to Rudi, Should I be using an OnClickListener? 另外,作为对Rudi的答复,我应该使用OnClickListener吗? I've been using an ActionListener.
我一直在使用ActionListener。
[LAST EDIT] [最后编辑]
I've figured it out. 我知道了。 It turns out my issue was a conflict with something that happens later in the event, I just took out the line of code that was Turning them back off after it had just turned them on.
事实证明,我的问题是与事件稍后发生的事情发生冲突,我只是取出了代码行, 将它们打开后又将其关闭 。 Thanks everyone for all of your help.
感谢大家的所有帮助。
System.out.println("Some text");
This one line of text solved my entire problem. 这段文字解决了我的整个问题。 Thanks again everyone!
再次感谢大家! You've saved my FATE games.
您已保存我的FATE游戏。
If you want to enable/disable check boxes when you click on an "apply" button , you can follow @anonymous advice and put the test on an ActionListener
. 如果要在单击“应用”按钮时启用/禁用复选框,则可以遵循@anonymous建议并将测试放在
ActionListener
。
If you want to enable/disable check boxes when some text is typed , you have to put a listener on the text field. 如果要在键入某些文本时启用/禁用复选框,则必须在文本字段上放置一个侦听器。 Basically, every time the text changes, the component must fire an event to check the word and enable/disable the check boxes.
基本上,每次文本更改时,组件都必须触发一个事件以检查单词并启用/禁用复选框。 You can do a thing like this in the init part of your code:
您可以在代码的init部分中执行以下操作:
skillGreat1JTextField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent arg0) {
checkWord();
}
@Override
public void insertUpdate(DocumentEvent arg0) {
checkWord();
}
@Override
public void changedUpdate(DocumentEvent arg0) {
checkWord();
}
private void checkWord() {
String greatOne = skillGreat1JTextField.getText();
if (greatOne.equals("Physique")) {
physStress3CheckBox.setEnabled(true);
physStress4CheckBox.setEnabled(true);
}
}
});
You can find some guide on on listeners here , and specifically on document listeners here . 你可以找到一些指南上的听众这里就文件的听众,尤其在这里 。
I hope you have registered an ActionListener to the apply
button. 我希望你注册一个ActionListener添加到
apply
按钮。 It should be something like this... 应该是这样的...
applyBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if("Physique".equals(skillGreat1JTextField.getText())) {
physStress3CheckBox.setEnabled(true);
physStress4CheckBox.setEnabled(true);
}
}
});
OP here. 在这里操作。 The problem was within the same action.
问题出在同一行动之内。
I had an IF statement later in the button (That didn't seem important at the time) that referred to a different Checkbox that would Unlock all of the skill buttons: 我稍后在按钮中有一个IF语句(当时似乎并不重要),它引用了另一个Checkbox来解锁所有技能按钮:
if ( !unlockSkillsJCheckBox.isSelected() ) {
skillSuperb1JTextField.setEditable( false );
skillSuperb2JTextField.setEditable( false );
skillSuperb3JTextField.setEditable( false );
skillSuperb4JTextField.setEditable( false );
skillSuperb5JTextField.setEditable( false );
skillGreat2JTextField.setEditable( false );
skillGreat3JTextField.setEditable( false );
skillGreat4JTextField.setEditable( false );
skillGreat5JTextField.setEditable( false );
skillGood3JTextField.setEditable( false );
skillGood4JTextField.setEditable( false );
skillGood5JTextField.setEditable( false );
skillFair4JTextField.setEditable( false );
skillFair5JTextField.setEditable( false );
skillAverage5JTextField.setEditable( false );
consEXTRA2JTextField.setEditable( false );
physStress3CheckBox( false ); //THE PROBLEM LIES HERE.
physStress4Checkbox( false ); //THE PROBLEM LIES HERE.
mentStress3CheckBox.setEnabled( false );
mentStress4CheckBox.setEnabled( false );
}
The if statement was running perfectly and functioning as it was supposed to. if语句可以正常运行并按预期运行。 But because of those two lines, as soon as they turned on, they turned right back off again.
但是由于这两条线,它们一旦打开,便又重新右移。
Although I did learn that System.out.println("Some text"); 尽管我确实了解到System.out.println(“ Some text”); can be your best friend.
可以成为你最好的朋友。 Use this to test for logic errors.
使用它来测试逻辑错误。
My error was a logic error that happens later in the same action listener I had. 我的错误是逻辑错误,稍后会在与我相同的动作侦听器中发生。 Hope this helps anyone having my same problem!
希望这可以帮助任何遇到我同样问题的人! Thanks Stack!
谢谢堆栈!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.