简体   繁体   中英

How to write in one textField instead of all fields

I'm developing a complex number calculator. I made 5 text fields, and when I click on a button to write a number it shows the text on all fields so it will be the same number on all fields.

This is the code of one button:

JButton btn0 = new JButton("0");
btn0.setFont(new Font("Tahoma",Font.BOLD,15));
btn0.setBounds(199, 228, 80, 30);
frame.getContentPane().add(btn0);
btn0.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        String num = textField.getText() + btn0.getText();
        textField.setText(num);

        String num1 = field.getText() + btn0.getText();
        field.setText(num1);

        String num2 = field2.getText() + btn0.getText();
        field2.setText(num2);

        String num3 = field3.getText() + btn0.getText();
        field3.setText(num3);

        String num4 = field4.getText() + btn0.getText();
        field2.setText(num4);
    }
});

When user clicks btn0 , this actionPerformed() method you have mentioned here get executed. Inside this actionPerformed() method you are setting values of all text fields ( textField , field , field2 etc.). So, it is not surprising that text is set in all text fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM