如果字符串以负号开头,setText 将无法正常工作

setText doesn't work properly if the string starts with a negative sign

所以,我正在制作一个计算器。我试图在 JTextField 中显示等式,但如果等式以负号开头,则无法正确打印。

textfield.setText("-3+3");

给出输出 = 3+3-

但在

时是相同的等式
System.out.println("-3+3");

以正确的顺序给出输出

抱歉,如果这是一些菜鸟错误。

编辑:

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


}

发生这种情况是因为您设置了

textField.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

很简单。这通常用于阿拉伯语文本 (?),我认为它是倒置的。