java - JTextfield - 字符串拆分和 intparse

标签 java string swing parsing split

我正在开发一个海龟图形项目,我正在尝试从 jtextfield (commField) 检索用户输入,该输入应该类似于:“forward 100”,但是当程序运行后,即使输入了正确的命令,它也会进入消息错误对话框。经过几个小时的大脑转动后,我正在努力找出原因,因此寻求任何帮助。如果需要更多代码才能得到好的答案,也许我关注的是错误的事情。

commField.addActionListener(new ActionListener() {


        public void actionPerformed(ActionEvent e) {


            else if(commField.getText().contains("forward")) {
                String cForward = commField.toString();
                String[] cForwardArray = cForward.split("\\s+");
                try {
                    int distance = Integer.parseInt(cForwardArray[1]);
                    graphicsPanel.forward(distance);

                } 
                catch (Exception ev) {
                    JOptionPane.showMessageDialog(textArea, 
                            "Invaild or missing parameter, check the help section\n"
                            + "for more information on Commands");
                }
            }
            else if(commField.getText().contains("backward")) {
                String cBackward = commField.toString();
                String[] cBackwardArray = cBackward.split("\\s+");
                try {
                    int distance = Integer.parseInt(cBackwardArray[1]);
                    graphicsPanel.backward(distance);
                    graphicsPanel.repaint();
                } 
                catch (Exception ev) {
                    JOptionPane.showMessageDialog(textArea, 
                            "Invaild or missing parameter, check the help section\n"
                            + "for more information on Commands");
                }
            }



        }
    });

下面是完整的代码块,供需要的人引用:

commField.addActionListener(new ActionListener() {


        public void actionPerformed(ActionEvent e) {

            if(commField.getText().contains("penup")) {
                graphicsPanel.penUp();
            }
            else if(commField.getText().contains("pendown")) {
                graphicsPanel.penDown();
            }
            else if(commField.getText().contains("turnright")) {
                graphicsPanel.turnRight();
            }
            else if(commField.getText().contains("turnleft")) {
                graphicsPanel.turnLeft();
            }
            else if(commField.getText().contains("forward")) {
                String cForward = commField.toString();
                String[] cForwardArray = cForward.split("\\s+");
                try {
                    int distance = Integer.parseInt(cForwardArray[1]);
                    graphicsPanel.forward(distance);

                    System.out.println(commField);

                } 
                catch (Exception ev) {
                    JOptionPane.showMessageDialog(textArea, 
                            "Invaild or missing parameter, check the help section\n"
                            + "for more information on Commands");
                }
            }
            else if(commField.getText().contains("backward")) {
                String cBackward = commField.toString();
                String[] cBackwardArray = cBackward.split("\\s+");
                try {
                    int distance = Integer.parseInt(cBackwardArray[1]);
                    graphicsPanel.backward(distance);
                    graphicsPanel.repaint();
                } 
                catch (Exception ev) {
                    JOptionPane.showMessageDialog(textArea, 
                            "Invaild or missing parameter, check the help section\n"
                            + "for more information on Commands");
                }
            }

            else if(commField.getText().contains("black")) {
                graphicsPanel.black(Color.black);
            }
            else if(commField.getText().contains("green")) {
                graphicsPanel.green(Color.green);
            }
            else if(commField.getText().contains("red")) {
                graphicsPanel.red(Color.red);
            }
            else if(commField.getText().contains("reset")) {
                graphicsPanel.clear();
            }
            else {
                JOptionPane.showMessageDialog(textArea, "Invalid command, try again");
            }

            commField.setText("");
            graphicsPanel.repaint();


        }
    });

最佳答案

兴趣线:

String cForward = commField.toString();

JTextField.toString() 不返回 JTextField 的内容。

我检查了我的 java 8 源代码并发现了以下内容:

public String toString() {
    return getClass().getName() + '[' + paramString() + ']';
}

但是,toString() 方法旨在成为一个调试实用程序。除非明确记录其行为,否则不建议以编程方式依赖它。

为了检索其文本内容,JTextField 提供了一个单独的方法:JTextComponent#getText() 。因此,该行应更改为:

String cForward = commField.getText();

关于java - JTextfield - 字符串拆分和 intparse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50205584/

相关文章:

java - 为什么java程序中的输出打印值300

java - 如何在 Hibernate 中的同一个表上建模 ManyToOne 和 ManyToMany 关系?

javascript - 如何在javascript中拆分缓冲区

java - 如何获取字符串数组中字符的索引位置

java - 我能否以编程方式找到句柄在 JTree 行中的位置?

java - FFmpeg 命令帮助和文档指针

c++ - 快速字符串搜索?

java - J对话框没有获得焦点

java - 如何在Java上制作一个完美的整数对话框?

java - 有人可以解释一下 eclipse java 项目的 .settings 文件夹包含什么吗?