简体   繁体   English

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

[英]Java: How can I detect the user has finalised their save name without using an actionListener?

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: 基本上,我不会从用户那里获取字符串,我创建了一个名为“框架”的类,其中有很多方法,例如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();
        }

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, 现在我知道这是行不通的,它只会使游戏卡住,我必须在外部终止它,我也理解为什么它不起作用,这不是问题,我也知道如何添加JButton,动作监听器并从那里开始工作,

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 //编辑:我知道方法getText()确实存在,对不起,如果它产生误导,我将对其进行修改

//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? 或理想地知道我错过的一支班轮?

I think what you want is the JOptionPane.showInputDialog method. 我认为您想要的是JOptionPane.showInputDialog方法。 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. 这将显示一个对话框,提示“ Type Something”和一个用于输入的文本字段。 Whatever the user types in the text field is returned by getUserInput() . getUserInput()返回用户在文本字段中键入的任何内容。

Honestly I'm not sure of having fully understood your problem. 老实说,我不确定是否完全了解您的问题。 Anyway, here's a tutorial on how to make dialogs properly in Swing. 无论如何,这是有关如何在Swing中正确制作对话框的教程

If you use 如果你使用

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

Your application main frame input is blocked until the JDialog showed is closed. 在关闭显示的JDialog之前,您的应用程序主机输入将被阻止。 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(). 如果您不想使用ActionListener或类似的东西(DocumentListener,...),则可以强制用户在JTextField中插入一个值,按ok按钮,并在showOptionDialog返回时手动检索JTextField的文本。使用getText()。

EDIT: I try to extend my answer a little bit. 编辑:我尝试扩大我的答案。 Extends a JDialog to create the desired dialog: 扩展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;
    }

}

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();
        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! 这是我使用的最后一种方法,比我以前使用的设置要干净得多,以前的设置涉及创建一个很小的框架,并添加一个文本字段和一个带有监听器的按钮!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用ActionListener检测是否选择了一个JRadioButton? - How do I detect if a single JRadioButton is selected or not using ActionListener? 如何保存ActionListener Java的登录信息 - How to save login information for an ActionListener Java 如何使用非抽象类实现ActionListener? Java的 - How can I implement ActionListener with a non abstract class? Java 如何在不使用 java 小程序检查且不使用 javascript 的情况下检测是否安装了 java 并知道其版本? - How can I detect if java is installed or not and knowing its version, without using java applet to check and without using javascript? 如何从 Java 中的 ActionListener 内部更改变量的值? - How can I change the value of a variable from inside an ActionListener in Java? 我该如何实现ActionListener? - How Can I implement an ActionListener to this? 如何在没有延迟的情况下检测网卡是否未与Java连接? - How can I detect if a network card is not connected with Java without delay? JButton上的Java Actionlistener没有名称? - Java Actionlistener on JButton with no name? 如何使用jbutton和actionlistener绘制椭圆形? - How can I draw an oval using jbuttons and actionlistener? 如何从另一个actionListener内部的actionListener访问变量? - How can I access a variable from an actionListener insider another actionListener?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM