Java:如何在不使用 actionListener 的情况下检测用户已完成其保存名称?

发布于 2024-12-04 12:47:17 字数 1175 浏览 0 评论 0原文

基本上我不想从用户那里获取字符串,我创建了一个名为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暗藏城府 2024-12-11 12:47:17

我认为您想要的是 JOptionPane.showInputDialog 方法。像这样的东西吗?

public class GetUserInput {

    public static String getUserInput() {
        return JOptionPane.showInputDialog("Type Something");
    }

    public static void main(String[] args) {
        System.out.println("User Input: " + getUserInput());
    }
}

这将显示一个带有提示“Type Something”的对话框和一个用于输入的文本字段。无论用户在文本字段中输入什么内容,getUserInput() 都会返回。

I think what you want is the JOptionPane.showInputDialog method. Something like this?

public class GetUserInput {

    public static String getUserInput() {
        return JOptionPane.showInputDialog("Type Something");
    }

    public static void main(String[] args) {
        System.out.println("User Input: " + getUserInput());
    }
}

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().

命比纸薄 2024-12-11 12:47:17

老实说,我不确定是否完全理解你的问题。反正,
这里有一个关于如何在 Swing 中正确创建对话框的教程

如果您使用

int ret = JOptionPane.showOptionDialog(new JDialog(), ...);

您的应用程序主框架输入将被阻止,直到 JDialog 显示关闭。
如果您不想使用 ActionListener 或类似的东西(DocumentListener,...),您可以强制用户在 JTextField 中插入一个值,按“确定”按钮,当 showOptionDialog 返回时,手动检索 JTextField 的文本与 getText() 一起使用。

编辑:
我尝试稍微扩展一下我的答案。
扩展 JDialog 以创建所需的对话框:

public class CustomDialog extends JDialog{
    private JPanel panel;
    private JTextField field;
    public CustomDialog(){
        panel = new JPanel(); //create a panel possibly with a LayoutManager
        field = new JTextField();
    }
    public JTextField getField(){
        return this.field;
    }

}

然后在需要的地方显示对话框,并在返回时检查字段文本:

CustomDialog dialog = new CustomDialog();
int ret = JOptionPane.showOptionDialog(dialog, ...);

String text = dialog.getField().getText();

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

int ret = JOptionPane.showOptionDialog(new JDialog(), ...);

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:

public class CustomDialog extends JDialog{
    private JPanel panel;
    private JTextField field;
    public CustomDialog(){
        panel = new JPanel(); //create a panel possibly with a LayoutManager
        field = new JTextField();
    }
    public JTextField getField(){
        return this.field;
    }

}

Then show the dialog where you need it, and check the field text when it returns:

CustomDialog dialog = new CustomDialog();
int ret = JOptionPane.showOptionDialog(dialog, ...);

String text = dialog.getField().getText();
小红帽 2024-12-11 12:47:17
        public String getSaveName()
        {
            boolean textGot = false;
            String returnText = null;
            while (!textGot)
                {
                    returnText = (String) JOptionPane.showInputDialog(hub.frame, "Enter the name of your save:\n", "Save Box",
                            JOptionPane.PLAIN_MESSAGE, null, null, save == null ? "new save" : save.saveName);

                    if ((returnText != null) && (returnText.length() > 0))
                        {
                            textGot = true;
                        }
                }
            return returnText;
        }

这是我使用的最终方法,比我使用的旧设置干净得多,旧设置涉及创建一个小框架,并添加一个文本字段和一个带有侦听器的按钮!

        public String getSaveName()
        {
            boolean textGot = false;
            String returnText = null;
            while (!textGot)
                {
                    returnText = (String) JOptionPane.showInputDialog(hub.frame, "Enter the name of your save:\n", "Save Box",
                            JOptionPane.PLAIN_MESSAGE, null, null, save == null ? "new save" : save.saveName);

                    if ((returnText != null) && (returnText.length() > 0))
                        {
                            textGot = true;
                        }
                }
            return returnText;
        }

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文