简体   繁体   English

如何在 JTextField 中多次捕获用户输入

[英]How to capture user input multiple times in a JTextField

Could you help me with this little problem?你能帮我解决这个小问题吗? I'm trying to make a menu system which shows the options in a JEditorPane, it's something like this:我正在尝试制作一个菜单系统,在 JEditorPane 中显示选项,它是这样的:

Welcome欢迎

Select an option.选择一个选项。 1.) New register. 1.) 新注册。 2.) New input. 2.) 新输入。 3.) Exit. 3.) 退出。

the options are chosen by the user through a JTextField, when "1" is entered it shows another menu:选项由用户通过 JTextField 选择,当输入“1”时,它会显示另一个菜单:

New register新注册

1.) Option X. 2.) Option Y. 3.) Back. 1.) 选项 X。 2.) 选项 Y。 3.) 返回。

and so on, the problem is that I don't know how I can capture the user's input, advance to the next menu, and re-capture the user's input all in a JTextField.等等,问题是我不知道如何捕获用户的输入,前进到下一个菜单,并在 JTextField 中重新捕获用户的输入。

textField.addActionListener(new ActionListener () {

        public void actionPerformed(ActionEvent e) {
            String cap = "";

            cap = textField.getText();

            switch(cap) {

            case "1":
                paintEditorPane("Welcome");

                    // here is my problem, I don't know how to re-capture JTextField input
                 switch(cap){

                 case "1":
                       paintEditorPane("NewRegister");
                       break;
                    }
            break;
            }
        }
    });

Here's Basic.这是基本的。 Now you have to make many cases to judge states.现在你必须制作很多案例来判断状态。

public static class MainPanel extends JPanel{
    private JTextArea textArea;

    public MainPanel() {
        this.setLayout(new BorderLayout());
        this.textArea = new JTextArea();// you can use constructor to set Text but I like use method "setText".
        this.textArea.addKeyListener(new keyHandler());
        this.textArea.setText("Welcome\r\nSelect an option. 1.) New register. 2.) New input. 3.) Exit.\r\n");
        this.textArea.setCaretPosition(this.textArea.getText().length());// move caret to last
        this.add(this.textArea, BorderLayout.CENTER);
    }

    public void addText(String text) {textArea.setText(textArea.getText() + "\r\n" + text +"\r\n");}

    public class keyHandler extends KeyAdapter{
        @Override
        public void keyReleased(KeyEvent e) {
            switch(e.getKeyCode()){
            case KeyEvent.VK_1 : addText("New register"); break;
            case KeyEvent.VK_2 : addText("New input"); break;
            case KeyEvent.VK_3 : addText("Exit"); break;
            }
        }
    }
}

在此处输入图片说明

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM