java中textpane用法,如何将字符串添加到textPane而不是设置它们?

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");

}});

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值