How to add strings to textPane instead of setting them?

假装没事ソ 提交于 2019-12-13 05:00:51

问题


I'm trying to make a calculator. http://i.imgur.com/exQLj4m.png The user will press the number they would like to calculate followed by the operator all in one line e.g '1+1-2+5' then Java would convert that into something it can understand and get the answer. But when I try you use setText() on the TextPane where the answer will show, it doesn't put more numbers in it just changes to the specified number. When I press 1 it shows 1 but when I press 2 it doesn't show 12, it shows 2. Is there like an addText() method? Here's my code for the number 1 button.

JButton btnNewButton = new JButton("1");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            answer.setText("1");
        }});    

回答1:


Here's my code for the number 1 button.

Don't create custom ActionListeners for every button. Use a generic listener. Something like:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class CalculatorPanel extends JPanel
{
    private JTextField display;

    public CalculatorPanel()
    {
        Action numberAction = new AbstractAction()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                display.setCaretPosition( display.getDocument().getLength() );
                display.replaceSelection(e.getActionCommand());
            }
        };

        setLayout( new BorderLayout() );

        display = new JTextField();
        display.setEditable( false );
        display.setHorizontalAlignment(JTextField.RIGHT);
        add(display, BorderLayout.NORTH);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout( new GridLayout(0, 5) );
        add(buttonPanel, BorderLayout.CENTER);

        for (int i = 0; i < 10; i++)
        {
            String text = String.valueOf(i);
            JButton button = new JButton( text );
            button.addActionListener( numberAction );
            button.setBorder( new LineBorder(Color.BLACK) );
            button.setPreferredSize( new Dimension(50, 50) );
            buttonPanel.add( button );

            KeyStroke pressed = KeyStroke.getKeyStroke(text);
            InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
            inputMap.put(pressed, text);
            button.getActionMap().put(text, numberAction);
        }
    }

    private static void createAndShowUI()
    {
//      UIManager.put("Button.margin", new Insets(10, 10, 10, 10) );

        JFrame frame = new JFrame("Calculator Panel");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add( new CalculatorPanel() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

The above code also shows how you can append the text to a text component.




回答2:


use

  answer.setText(answer.getText()+"1");

instead of

   answer.setText("1");

this will surely solve your problem.



来源:https://stackoverflow.com/questions/20829733/how-to-add-strings-to-textpane-instead-of-setting-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!