javaeclipse-mars

Issue with numbers not appearing in box when respective button is pressed


I'm making a simple calculator in GUI and I have all the code typed and ready, but I'm having trouble with making it so that when the user presses a number button, the respective number appears in the text box above. Do I need to use a radio button? Thanks in advance

I've tried action listeners but they didn't work (or I'm probably using them incorrectly). I've put the code for the 1 button.

JButton num1 = new JButton("1");
            num1 = b1;
            num1.setSize(50,50);
            num1.setLocation(20,200);
            num1.setBackground(Color.WHITE);

            num1.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e)
                    {

Solution

  • Just append the number to the JTextField(using setText(String) and getText()) in the actionPerformed function.