Java JTextArea退格并清除

Java JTextArea退格并清除,java,user-interface,jbutton,actionlistener,jtextarea,Java,User Interface,Jbutton,Actionlistener,Jtextarea,我正在使用计算器GUI,我需要帮助编写退格和清除按钮 下面是代码,names[0][3]是退格字符,names[0][4]是清除整个区域 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.BadLocationException; import javax.swing.text.Document; public class CalcGUI extends JFram

我正在使用计算器GUI,我需要帮助编写退格和清除按钮

下面是代码,
names[0][3]
是退格字符,
names[0][4]
是清除整个区域

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

public class CalcGUI extends JFrame
{   
    private Container contents;
    private final JPanel entryPanel, buttonPanel;
    private final JTextArea entryTA;
    private final JButton equalJB;
    private JButton [][] buttons;
    private final String [][] names =
    {{"7", "8", "9", "\u2190", "C"},
     {"4", "5", "6", "+", "-"},
     {"1", "2", "3", "*", "/"},
     {"0", ".", "(", ")", "^"}};

    public CalcGUI()
    {
        super("Calculator");

        contents = getContentPane();
        contents.setLayout(new BorderLayout());

        buttons = new JButton [4][5];
        buttonPanel = new JPanel(new GridLayout(4, 5));

        ButtonHandler bh = new ButtonHandler();

        for (int i = 0; i < names.length; i++)
        {
            for (int j = 0; j < names[i].length; j++)
            {
                buttons[i][j] = new JButton(names[i][j]);
                buttons[i][j].addActionListener(bh);
                buttonPanel.add(buttons[i][j]);
            }
        }

        contents.add(buttonPanel, BorderLayout.CENTER);

        entryTA = new JTextArea(3, 1);
        entryTA.setLineWrap(true);
        entryPanel = new JPanel(new GridLayout(1, 1));
        entryPanel.add(entryTA); 
        contents.add(entryPanel, BorderLayout.NORTH);

        equalJB = new JButton("=");
        equalJB.addActionListener(bh);
        contents.add(equalJB, BorderLayout.SOUTH);

        setSize(300, 300);
        setVisible(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private class ButtonHandler implements ActionListener
    {
        @Override
        public void actionPerformed (ActionEvent ae)
        {  
            for (int i = 0; i < names.length; i++)
            {
                for (int j = 0; j < names[i].length; j++)
                {
                    if (ae.getSource() == buttons[i][j])
                    {
                        if (ae.getSource() != buttons[0][3]
                            && ae.getSource() != buttons[0][4]) 
                        {
                            entryTA.append(names[i][j]);
                        }
                    } 
                    //backspace
                    else if (ae.getSource() == buttons[0][3])
                    {
                        try 
                        {
                            Document doc = entryTA.getDocument();
                            if (doc.getLength() > 0)
                            {
                                doc.remove(doc.getLength() - 1, 1);
                            }
                        } 
                        catch (BadLocationException ble) 
                        {
                            ble.printStackTrace();
                        }
                    }
                    //clear - works, as per @Frostsoft's solution
                    else if (ae.getSource() == buttons[0][4])
                    {
                        entryTA.setText(""); 
                    }
                }
            }
        }
    }

    public static void main(String [] args)
    {
        CalcGUI calc = new CalcGUI();
    }
}
对于输入
789
,只按一次backspace JButton:

3
removed
2
removed
1
removed

我会尝试做
entryTA.setText(“”)
清除文本区域,然后使用
entryTA.setText=(entryTA.getText()).substring(0,entryTA.getText().length()-1)
删除文本区域中的最后一个字符。这只是我头脑中的代码,但它应该可以正常工作。

我会尝试使用
entryTA.setText(“”)
清除文本区域,然后使用
entryTA.setText=(entryTA.getText()).substring(0,entryTA.getText().length()-1)
删除文本区域中的最后一个字符。这只是我脑海中的一段代码,但它应该可以正常工作。

Document#remove
似乎只适合我

在这种情况下,我更喜欢使用
Document#remove
,原因是它不会创建一堆临时
字符串
,从而提高效率,但(稍微)更快;)

考虑提供一个能证明你的问题的例子。这将减少混乱和更好的响应

已更新

问题是你的循环

for (int i = 0; i < names.length; i++) {
    for (int j = 0; j < names[i].length; j++) {
        //...
        else if (ae.getSource() == buttons[0][3]) {
            try {
                Document doc = entryTA.getDocument();
                if (doc.getLength() > 0) {
                    doc.remove(doc.getLength() - 1, 1);
                }
            } catch (BadLocationException ble) {
                ble.printStackTrace();
            }
        } //clear - works, as per @Frostsoft's solution
        else if (ae.getSource() == buttons[0][4]) {
            entryTA.setText("");
        }
    }
}
Document#remove
似乎只适合我

在这种情况下,我更喜欢使用
Document#remove
,原因是它不会创建一堆临时
字符串
,从而提高效率,但(稍微)更快;)

考虑提供一个能证明你的问题的例子。这将减少混乱和更好的响应

已更新

问题是你的循环

for (int i = 0; i < names.length; i++) {
    for (int j = 0; j < names[i].length; j++) {
        //...
        else if (ae.getSource() == buttons[0][3]) {
            try {
                Document doc = entryTA.getDocument();
                if (doc.getLength() > 0) {
                    doc.remove(doc.getLength() - 1, 1);
                }
            } catch (BadLocationException ble) {
                ble.printStackTrace();
            }
        } //clear - works, as per @Frostsoft's solution
        else if (ae.getSource() == buttons[0][4]) {
            entryTA.setText("");
        }
    }
}


谢谢,您的clear代码简单有效。退格一号不起作用。我尝试了
entryTA.setText(entryTA.getText().substring(0,entryTA.getText().length()-1))应该可以工作,但是它抛出了一个
StringIndexOutOfBoundsException
我不明白为什么,因为
子字符串开始于第一个索引0,结束于
length()-1
。尝试通过添加一些sysout来调试它,返回字符串的长度,这样您可能会看到字符串比您想象的长。然后调整…长度()-随便。或者试着做长度2?尝试不同的方法,然后发布整个异常返回的内容?我运行程序,输入
123
,然后点击backspace按钮。我添加了
System.out.println(“长度:+entryTA.getText().length())
查看长度是多少,它返回
长度:3
长度:2
长度:1
长度:0
。这就是为什么会出现异常,我想,但我对
JTextArea
不够熟悉,不知道它为什么会这样做。你也可以试试这个
entryTA.setText((entryTA.getText()).substring(0,entryTA.getText().length()-1))idk,如果这会改变它,但值得一试。谢谢,你的代码很简单而且有效。退格一号不起作用。我尝试了
entryTA.setText(entryTA.getText().substring(0,entryTA.getText().length()-1))应该可以工作,但是它抛出了一个
StringIndexOutOfBoundsException
我不明白为什么,因为
子字符串开始于第一个索引0,结束于
length()-1
。尝试通过添加一些sysout来调试它,返回字符串的长度,这样您可能会看到字符串比您想象的长。然后调整…长度()-随便。或者试着做长度2?尝试不同的方法,然后发布整个异常返回的内容?我运行程序,输入
123
,然后点击backspace按钮。我添加了
System.out.println(“长度:+entryTA.getText().length())
查看长度是多少,它返回
长度:3
长度:2
长度:1
长度:0
。这就是为什么会出现异常,我想,但我对
JTextArea
不够熟悉,不知道它为什么会这样做。你也可以试试这个
entryTA.setText((entryTA.getText()).substring(0,entryTA.getText().length()-1))
idk,如果这会改变它,但值得一试。要获得更好的帮助,请展示您使用文档的尝试。@HovercraftFullOfEels我在主要帖子中添加了它。我不确定我是否正确地执行它,但是它总是抛出异常。这样我们可以亲身体验您的问题。@fullofeels请参阅主帖子。我添加了完整的代码,因为它不太长,这样人们就可以按我看到的方式运行它。要获得更好的帮助,请展示您使用文档的尝试。@HovercraftFullOfEels我在主要帖子中添加了它。我不确定我是否正确地执行它,但是它总是抛出异常。这样我们可以亲身体验您的问题。@fullofeels请参阅主帖子。我添加了完整的代码,因为它不太长,这样人们就可以按我看到的方式运行它。谢谢你的回复。昨晚我不得不离开电脑,但我已经在主帖子中发布了完整的代码,因为没有那么长。我尝试了您的解决方案,它没有抛出异常,但它只是为我删除了所有内容。我认为问题与条件
doc.getLength()>0
有关。它会继续删除字符,直到条件为false为止。@pez在我的示例中,它只是一个
if
条件,每次单击只能执行一次……我知道,您的独立代码运行良好。你能看看我上面的代码吗?一旦我实现了你的代码,它就会运行多次。谢谢你的回复。昨晚我不得不离开电脑,但我已经在主帖子中发布了完整的代码,因为没有那么长。我试过你的
for (int i = 0; i < names.length; i++) {
    for (int j = 0; j < names[i].length; j++) {
        //...
        else if (ae.getSource() == buttons[0][3]) {
            try {
                Document doc = entryTA.getDocument();
                if (doc.getLength() > 0) {
                    doc.remove(doc.getLength() - 1, 1);
                }
            } catch (BadLocationException ble) {
                ble.printStackTrace();
            }
        } //clear - works, as per @Frostsoft's solution
        else if (ae.getSource() == buttons[0][4]) {
            entryTA.setText("");
        }
    }
}
public void actionPerformed(ActionEvent ae) {
    for (int i = 0; i < names.length; i++) {
        for (int j = 0; j < names[i].length; j++) {
            if (ae.getSource() == buttons[i][j]) {
                if (ae.getSource() != buttons[0][3]
                                && ae.getSource() != buttons[0][4]) {
                    entryTA.append(names[i][j]);
                }
            }
        }
    }

    if (ae.getSource() == buttons[0][3]) {
        try {
            Document doc = entryTA.getDocument();
            if (doc.getLength() > 0) {
                doc.remove(doc.getLength() - 1, 1);
            }
        } catch (BadLocationException ble) {
            ble.printStackTrace();
        }
    } else if (ae.getSource() == buttons[0][4]) {
        entryTA.setText("");
    }

}