有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在JTextArea中比较行

我正在做一个项目,我创建了一个类似网店的应用程序。除了最后一件事,即阅读整个篮子(添加到篮子中的项目),让一切都正常工作

我有一个按钮,可以将项目添加到订单中(它们显示在jtextarea中)。 我有另一个按钮,可以完成购物,并打印您添加到jtextarea的项目。不幸的是,我只知道如何阅读jtextarea中的最后一行。所以基本上会发生什么:

customer orders x, amount 1
customer orders y, amount 3
customer orders z, amount 7

它只会打印最后一行。有什么建议吗? 这是密码

        btnFinish.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                int end = textArea.getDocument().getLength();
                int start = 0;
                try {
                    start = Utilities.getRowStart(textArea, end);
                } catch (BadLocationException e1) {
                    e1.printStackTrace();
                }

                while (start == end)
                {
                    end--;
                    try {
                        start = Utilities.getRowStart(textArea, end);
                    } catch (BadLocationException e1) {
                        e1.printStackTrace();
                    }
                }

                String text = null;
                try {
                    text = textArea.getText(start, end - start);
                } catch (BadLocationException e1) {
                    e1.printStackTrace();
                }
                System.out.println("(" + text + ")");  ```

共 (0) 个答案