So, i am making a calculator. I was trying to display the equation in a JTextField, but the equation doesn't print correctly if it starts with a negative sign.
textfield.setText("-3+3");
gives output = 3+3-
but the same equation when in
System.out.println("-3+3");
gives output in the correct order
sorry if it is some noob mistake.
EDIT:
package practice;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class fun extends JFrame implements ActionListener , FocusListener{
public static void main(String[] args){
new fun();
}
JTextField display2 = new JTextField(20);
JTextField display = new JTextField(20);
JButton btnSol = new JButton("=");
public fun(){
JFrame fr = new JFrame();
fr.setSize(450,450);
fr.setLocationRelativeTo(null);
fr.setResizable(false);
fr.setTitle("Calculator");
JPanel pnl = new JPanel();
display.setEditable(false);
display2.setEditable(false)
pnl.add(display);
pnl.add(display2);
pnl.add(btnSol);
btnSol.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String x = display2.getText();
System.out.println(x);
display.setText(x);
int sum = 0;
String[] arrOfStr = x.split("\\+");
for (String a : arrOfStr) {
System.out.println(a);
sum += Integer.parseInt(a);
display2.setText(Integer.toString(sum));
}
}
});
fr.add(pnl);
fr.setVisible(true);
}
That happens because you've set
textField.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
Pretty simple. That's usually used for Arabic text (?), which I suppose is inverted.