Java - pytanie do JTextField, JButton

0

Witam mam pytanie w sprawie JTextField,
Chciałbym za pomocą przycisku dodać do JTextField liczbę 2 ale też nie usuwać poprzedniej liczby np. 1 i czy jest jakaś możliwość żeby tego uniknąć?

Kod:

package com.Window;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class WindowClass extends JFrame implements ActionListener {

    private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, bDodaj, bMinus, bRownosc;
    private JTextField textRownania;

    public WindowClass(){
        setTitle("Kalkulator");
        setSize(330,350);
        setVisible(true);
        setLayout(null);
        /*
        * x55
        */
        textRownania = new JTextField("Podaj działanie...");
        add(textRownania);
        textRownania.setBounds(10, 20, 295, 40);

        b1 = new JButton("1");
        add(b1);
        b1.addActionListener((ActionListener) this);
        b1.setBounds(10, 80, 45, 45);

        b2 = new JButton("2");
        add(b2);
        b2.addActionListener((ActionListener) this);
        b2.setBounds(65, 80, 45, 45);

        b3 = new JButton("3");
        add(b3);
        b3.addActionListener((ActionListener) this);
        b3.setBounds(120, 80, 45, 45);

        b4 = new JButton("4");
        add(b4);
        b4.addActionListener((ActionListener) this);
        b4.setBounds(175, 80, 45, 45);

        b5 = new JButton("5");
        add(b5);
        b5.addActionListener((ActionListener) this);
        b5.setBounds(230, 80, 45, 45);

        b6 = new JButton("6");
        add(b6);
        b6.addActionListener((ActionListener) this);
        b6.setBounds(10, 135, 45 ,45);

        b7 = new JButton("7");
        add(b7);
        b7.addActionListener((ActionListener) this);
        b7.setBounds(65, 135, 45, 45);

        b8 = new JButton("8");
        add(b8);
        b8.addActionListener((ActionListener) this);
        b8.setBounds(120, 135, 45, 45);

        b9 = new JButton("9");
        add(b9);
        b9.addActionListener((ActionListener) this);
        b9.setBounds(175, 135, 45,45);

        b0 = new JButton("0");
        add(b0);
        b0.addActionListener((ActionListener) this);
        b0.setBounds(230, 135, 45, 45);

        bDodaj = new JButton("+");
        add(bDodaj);
        bDodaj.addActionListener((ActionListener) this);
        bDodaj.setBounds(10, 190, 100, 45);

        bMinus = new JButton("-");
        add(bMinus);
        bMinus.addActionListener((ActionListener) this);
        bMinus.setBounds(175, 190, 100, 45);

        bRownosc = new JButton("=");
        add(bRownosc);
        bRownosc.addActionListener((ActionListener) this);
        bRownosc.setBounds(10, 245, 265, 45);
    }

    public void actionPerformed(ActionEvent e){
        Object source = e.getSource();

        if(source == b1){
            textRownania.setText(btn1);
        }
        else if(source == b2){
            textRownania.
        }
    }
}
0

setText() usuwa wszystkie "poprzednie" znaki z pola tekstowego.

0

textRownania.setText(textRownania.getText()+" "+btn1);

1 użytkowników online, w tym zalogowanych: 0, gości: 1