[英]How to make JTextField appear after a button click and only accept the same length of characters as another string -Java GUI
I had posted a similar question before but it got deleted by accident. 我之前曾发布过类似的问题,但被意外删除。
My JTextField is supposed to appear after clicking a button which it does but only after minimizing the window. 我的JTextField应该在单击按钮后出现,但只有在最小化窗口之后才会出现。 Another problem with it is that it accepts single characters when it's supposed to accept a phrase of only a certain length.
另一个问题是,当它只应接受一定长度的短语时,它将接受单个字符。
Here's a bit of the code since it's so long: 因为它太长了,所以这里有一些代码:
static String answer = "MY PUZZLE", puzzle = "M- -U----";
//inside constructor
guess = new JTextField("Write in Caps");
guess.setVisible(false);
guess.addActionListener(this);
board.add(guess);
//actionperformed
guess.setVisible(true);
gueStr = guess.getText();
if (gueStr.length() != answer.length()) //if it is not the same length
{
gueStr = "";
}
the String "puzzle" is changed if a letter button (code not shown here) that is a character from the "answer" String is pressed or if the user's guess is the same phrase as the answer String 如果按下字母按钮(此处未显示代码)作为“答案”字符串中的字符,或者用户的猜测与答案字符串相同,则更改字符串“拼图”
if ( gueStr.equals(answer)) //if the guess is the answer
{
puzzle = answer;
}
for(int x=0; x < answer.length(); x++) //go through answer
{
if(letter == answer.charAt(x)) //if the letter pressed matches a character in answer
{
puzzle = puzzle.substring(0,x) + letter + puzzle.substring(x+1); //substitute in letter
}
}
If more code is needed for understanding, I can post it. 如果需要更多代码来理解,我可以发布它。 I'd be happy for a few pointers :D
我很乐意提供一些指导:D
Edit: 编辑:
Thanks for all help guys, but I haven't been successful in understanding the second part yet. 感谢所有帮助人员,但是我还没有成功理解第二部分。
Right now, I can get the textfield to show, but the problem lies with accepting a certain character length. 现在,我可以显示文本字段,但是问题在于接受一定的字符长度。 It has to be the exact same length as another string, no more, no less.
它的长度必须与另一个字符串的长度完全相同,不能多于或少于。
I have tried implementing it with the links you guys gave but only ended up getting confused (sorry). 我曾尝试用你们提供的链接来实现它,但最后却感到困惑(抱歉)。 Can someone please offer a specific example?
有人可以提供一个具体的例子吗?
Edit: 编辑:
Resolved after using validate() and adding guess.setActionCommand("1") . 使用validate()并添加guess.setActionCommand(“ 1”)之后解决 。
Use InputVerifier
to validate your text input 使用
InputVerifier
验证您的文本输入
Refer below example : 请参考以下示例:
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JTextField;
class PassVerifier extends InputVerifier {
public boolean verify(JComponent input) {
JTextField tf = (JTextField) input;
String text = tf.getText();
// do your validation
// if (text.equals(answer)) //if the guess is the answer
// {
// puzzle = answer;
// }
// for (int x = 0; x < answer.length(); x++) //go through answer
// {
// if (letter == answer.charAt(x)) //if the letter pressed matches a character in answer
// {
// puzzle = puzzle.substring(0, x) + letter + puzzle.substring(x + 1); //substitute in letter
// }
// }
return true; // return true or false according to validation
}
}
Set to InputVerifier
to your JTextField
将
InputVerifier
设置为JTextField
JTextField tf1 = new JTextField ("Type \"pass\" here");
tf1.setInputVerifier(new PassVerifier());
frame.validate()
The validate method is used to cause a container to lay out its subcomponents again.
validate方法用于使容器再次布置其子组件。 It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.
显示容器后,修改此容器的子组件(添加到容器或从容器中删除,或更改与布局有关的信息)时,应调用该方法。
This should work instead of minimizing the window. 这应该起作用而不是最小化窗口。
Also for the second part, the API provides an example on how to custumize text fields: https://docs.oracle.com/javase/8/docs/api/javax/swing/JTextField.html 同样在第二部分中,API提供了有关如何自定义文本字段的示例: https ://docs.oracle.com/javase/8/docs/api/javax/swing/JTextField.html
Do something similar to how they only accepts uppercase characters, but instead check the length with string.length(). 执行类似于它们仅接受大写字符的方式,而是使用string.length()检查长度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.