i help some help with coding the action listener for my button (btBody) which create button displaying 1 to 9 in a nested for loop; the button should allow the user to click on them and to display the number clicked on in a JTextField;
my code;
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class EX_7_2 extends JFrame { public EX_7_2() { setLayout(new BorderLayout(5, 10)); // create p1 JPanel p1 = new JPanel(); p1.setLayout(new BorderLayout(5, 10)); p1.add(new JTextField(), BorderLayout.NORTH); JPanel p3 = new JPanel(); p3.setLayout(new GridLayout(1, 3, 5, 5)); JButton btBackspace = new JButton ("Backspace"); btBackspace.setBackground(Color.WHITE); btBackspace.setForeground(Color.RED); JButton btCE = new JButton ("CE"); btCE.setBackground(Color.WHITE); btCE.setForeground(Color.RED); JButton btC = new JButton ("C"); btC.setBackground(Color.WHITE); btC.setForeground(Color.RED); p3.add(btBackspace); p3.add(btCE); p3.add(btC); p1.add(p3, BorderLayout.CENTER); // create p4 JPanel p2 = new JPanel(); p2.setLayout(new GridLayout(4, 3, 5, 5)); int num; for (int iRow = 3; iRow>0; iRow--) { for (int iCol = 1; iCol<4; iCol++) { num = 3*(iRow-1) + iCol; JButton btBody = new JButton("" + num); btBody.setBackground(Color.WHITE); btBody.setForeground(Color.BLUE); p2.add(btBody); btBody.addActionListener(new ActionListener() { @Override /**HandleItemEvent*/ public void actionPerformed(ActionEvent e) { } }); } } JButton btZero = new JButton ("0"); btZero.setBackground(Color.WHITE); btZero.setForeground(Color.BLUE); JButton btPlusMinus = new JButton ("+/-"); btPlusMinus.setBackground(Color.WHITE); btPlusMinus.setForeground(Color.BLUE); JButton btDot = new JButton ("."); btDot.setBackground(Color.WHITE); btDot.setForeground(Color.BLUE); p2.add(btZero); p2.add(btPlusMinus); p2.add(btDot); JPanel p5 = new JPanel(); p5.setLayout(new GridLayout(4, 2, 5, 5)); JButton btBack = new JButton ("/"); btBack.setBackground(Color.WHITE); btBack.setForeground(Color.RED); JButton btsprt = new JButton ("sprt"); btsprt.setBackground(Color.WHITE); btsprt.setForeground(Color.BLUE); JButton btStar = new JButton ("*"); btStar.setBackground(Color.WHITE); btStar.setForeground(Color.RED); JButton btPercentage = new JButton ("%"); btPercentage.setBackground(Color.WHITE); btPercentage.setForeground(Color.BLUE); JButton btMinus = new JButton ("-"); btMinus.setBackground(Color.WHITE); btMinus.setForeground(Color.RED); JButton btOnex = new JButton ("1/x"); btOnex.setBackground(Color.WHITE); btOnex.setForeground(Color.BLUE); JButton btPlus = new JButton ("+"); btPlus.setBackground(Color.WHITE); btPlus.setForeground(Color.RED); JButton btEquals = new JButton ("="); btEquals.setBackground(Color.WHITE); btEquals.setForeground(Color.RED); p5.add(btBack); p5.add(btsprt); p5.add(btStar); p5.add(btPercentage); p5.add(btMinus); p5.add(btOnex); p5.add(btPlus); p5.add(btEquals); JPanel p4 = new JPanel(); p4.setLayout(new BorderLayout(5, 10)); p4.add(p2, BorderLayout.CENTER); p4.add(p5, BorderLayout.EAST); add(p1, BorderLayout.NORTH ); add(p4, BorderLayout.CENTER); } public static void main(String[] args) { EX_7_2 frame = new EX_7_2(); frame.setTitle("Calculator"); frame.setSize(350, 375); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }