如何将字符串添加到textPane而不是设置它们? [英] How to add strings to textPane instead of setting them?
问题描述
我正在尝试制作一个计算器。 http://i.imgur.com/exQLj4m.png
用户将按他们想要计算的数字后跟运算符一行,例如
'1 + 1-2 + 5'
然后Java会把它转换成它能理解的东西并得到答案。
但是当我尝试在TextPane上使用setText()时答案将显示,它不会在其中添加更多数字只是更改为指定的数字。当我按1时它显示1但是当我按2时它不显示12,它显示2.是否有像addText()方法?
这是我的1号按钮的代码。
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.
不要为每个按钮创建自定义ActionListener。使用通用侦听器。类似于:
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.
这篇关于如何将字符串添加到textPane而不是设置它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!