При выполнении операций +, — , * и / не очищать экран - Java
Формулировка задачи:
Всем доброго дня!
Подскажите , что нужно сделать в коде ниже, чтобы при нажатии на операцию +,-,/,* числа не исчезали...Сейчас такая последовательность: нажимаю 5, потом + и число пять пропадает, нажимаю дальше 5, потом равно и выводится результат...
А нужно нажимаю 5, потом + и число не исчезает...
public class CalcFrame extends JFrame {
JTextArea display= new JTextArea();
JPanel buttonPanel = new JPanel(new GridLayout(5,3));
JButton button0 = new JButton("0");
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
JButton button6 = new JButton("6");
JButton button7 = new JButton("7");
JButton button8 = new JButton("8");
JButton button9 = new JButton("9");
JButton buttonSum = new JButton("+");
JButton buttonBack = new JButton("C");
JButton buttonDivide = new JButton("/");
JButton buttonSub = new JButton("-");
JButton buttonMul = new JButton("*");
JButton buttonStart = new JButton("=");
int firstValue = 0;
String operation = "+";
CalcFrame() {
setBounds(300, 300, 300, 300);
button0.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
display.setText(display.getText() + "0");
}
});
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
display.setText(display.getText()+"1");
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
display.setText(display.getText()+"2");
}
});
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
display.setText(display.getText()+"3");
}
});
button4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
display.setText(display.getText()+"4");
}
});
button5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
display.setText(display.getText()+"5");
}
});
button6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
display.setText(display.getText()+"6");
}
});
button7.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
display.setText(display.getText()+"7");
}
});
button8.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
display.setText(display.getText()+"8");
}
});
button9.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
display.setText(display.getText()+"9");
}
});
buttonBack.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String temp = display.getText();
display.setText(temp.substring(0,temp.length()-1));
}
});
buttonSum.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
firstValue = Integer.valueOf(display.getText());
display.setText("");
operation = "+";
}
});
buttonMul.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
firstValue = Integer.valueOf(display.getText());
display.setText("");
operation = "*";
}
});
buttonDivide.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
firstValue = Integer.valueOf(display.getText());
display.setText("");
operation = "/";
}
});
buttonSub.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
firstValue = Integer.valueOf(display.getText());
display.setText("");
operation = "-";
}
});
buttonStart.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int secondValue = Integer.valueOf(display.getText());
if("+".equals(operation)){
display.setText((firstValue+secondValue)+"");
}
if("-".equals(operation)){
display.setText((firstValue-secondValue)+"");
}
if("*".equals(operation)){
display.setText((firstValue*secondValue)+"");
}
if("/".equals(operation)){
display.setText((firstValue/secondValue)+"");
}
firstValue = 0;
operation = "+";
}
});
setLayout(new BorderLayout());
add(display, BorderLayout.NORTH);
add(buttonPanel,BorderLayout.CENTER);
add(buttonStart,BorderLayout.SOUTH);
buttonPanel.add(button0);
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
buttonPanel.add(button4);
buttonPanel.add(button5);
buttonPanel.add(button6);
buttonPanel.add(button7);
buttonPanel.add(button8);
buttonPanel.add(button9);
buttonPanel.add(buttonSum);
buttonPanel.add(buttonSub);
buttonPanel.add(buttonMul);
buttonPanel.add(buttonBack);
buttonPanel.add(buttonDivide);
setVisible(true);
}
public static void main(String[] args) {
new CalcFrame();
}
}
функция очищения была изначально...калькулятор не мой, я просто тренируюсь на нём...я пока новичок и плохо представляю по каким поисковым словам искать сие потребство
Решение задачи: «При выполнении операций +, — , * и / не очищать экран»
textual
Листинг программы
int secondValue = Integer.valueOf(display.getText().substring(display.getText().indexOf(operation)));