Java计算器如何制作退格

时间:2014-10-22 20:19:38

标签: java

几天前我开始学习编程。我试着制作一个计算器,但我有一个问题: 我不知道如何制作退格JButton。

buusun.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {
                tf.setText(text.getText().length()-1); 
            }

        });

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

试试这个:

    buusun.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent arg0)
        {
            tf.setText(text.getText().substring(0, text.getText().length() - 1)); 
        }

    });

它使用string.substring(start, end)方法 请注意,您可能需要调整所使用的确切变量,因为我不确定您是否需要从tftext获取值,但这应该提供您想要的内容的要点。