Java:如何在不使用 actionListener 的情况下检测用户已完成其保存名称?
基本上我不想从用户那里获取字符串,我创建了一个名为“frames”的类,其中有很多方法,例如 exitChoice()、infoPop() 等...我希望创建一个名为getText(),这就是我到目前为止所拥有的:
public String getText()
{
JDialog textBox = new JDialog(frame, "Save Name", true);
JTextField inputField = new JTextField(18);
inputField.setText(save == null ? "new save" : save.saveName);
textBox.setBounds(width, height, 275, 70);
textBox.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
textBox.setLayout(new FlowLayout());
textBox.setAlwaysOnTop(true);
textBox.setResizable(false);
textBox.add(inputField);
textBox.setVisible(true);
return inputField.getText();
}
现在我知道这不起作用,它只会让游戏卡住,我必须从外部终止它,我也明白为什么它不起作用,那不是问题我也知道怎么添加一个 JButton,一个动作监听器并从那里开始工作,
基本上我试图创建一个干净简单的方法,它从用户那里获取一个字符串,该字符串全部包含在该方法中。
理想情况下,我想写一行,沿着
//EDIT: 我知道方法 getText() 确实存在,抱歉,如果它具有误导性,我会修改它,
//String saveName = new JTextField.getText();
String saveName = new JTextInputGetterBoxThing();
但据我所知,到目前为止这并不存在似乎不存在,有人有什么想法吗?或者最好知道我错过的一个班轮?
Basically I wan't to obtain a string from the user, I have created a class called "frames" in which I have a load of methods such as exitChoice(), infoPop(), ect... I wish to create one called getText(), and this is what I have so far:
public String getText()
{
JDialog textBox = new JDialog(frame, "Save Name", true);
JTextField inputField = new JTextField(18);
inputField.setText(save == null ? "new save" : save.saveName);
textBox.setBounds(width, height, 275, 70);
textBox.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
textBox.setLayout(new FlowLayout());
textBox.setAlwaysOnTop(true);
textBox.setResizable(false);
textBox.add(inputField);
textBox.setVisible(true);
return inputField.getText();
}
now I know this won't work, It simply gets the game stuck and I have to terminate it externally, I also understand why it doesn't work, that isn't the problem, I also know how to add a JButton, an action listener and work it from there,
Basically I am trying to create a clean simple method which obtains a String from the user which is ALL contained within the method.
Ideally I would like to write a line which reads along the lines of
//EDIT: I know the method getText() does exist, sorry if it is misleading, I will ammend it
//String saveName = new JTextField.getText();
String saveName = new JTextInputGetterBoxThing();
but as far as I have found so far this doesn't appear to exist, does anybody have any ideas? or ideally know of a one liner that I have missed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您想要的是 JOptionPane.showInputDialog 方法。像这样的东西吗?
这将显示一个带有提示“Type Something”的对话框和一个用于输入的文本字段。无论用户在文本字段中输入什么内容,
getUserInput()
都会返回。I think what you want is the
JOptionPane.showInputDialog
method. Something like this?This shows a dialog with the prompt "Type Something" and a text field for entry. Whatever the user types in the text field is returned by
getUserInput()
.老实说,我不确定是否完全理解你的问题。反正,
这里有一个关于如何在 Swing 中正确创建对话框的教程。
如果您使用
您的应用程序主框架输入将被阻止,直到 JDialog 显示关闭。
如果您不想使用 ActionListener 或类似的东西(DocumentListener,...),您可以强制用户在 JTextField 中插入一个值,按“确定”按钮,当 showOptionDialog 返回时,手动检索 JTextField 的文本与 getText() 一起使用。
编辑:
我尝试稍微扩展一下我的答案。
扩展 JDialog 以创建所需的对话框:
然后在需要的地方显示对话框,并在返回时检查字段文本:
Honestly I'm not sure of having fully understood your problem. Anyway,
here's a tutorial on how to make dialogs properly in Swing.
If you use
Your application main frame input is blocked until the JDialog showed is closed.
If you don't want to use an ActionListener or something similare (DocumentListener, ...) you can force the user to insert a value in the JTextField, press the ok button and the when showOptionDialog return, manually retrieve the text of the JTextField with getText().
EDIT:
I try to extend my answer a little bit.
Extends a JDialog to create the desired dialog:
Then show the dialog where you need it, and check the field text when it returns:
这是我使用的最终方法,比我使用的旧设置干净得多,旧设置涉及创建一个小框架,并添加一个文本字段和一个带有侦听器的按钮!
Here is the final method I am using, much cleaner than the old setup I had which involved creating a tiny frame, and adding a textfield and a button with a listener!