i dont what to you mean can you provide the whole completed code for the actionlistener and the method please
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
i dont what to you mean can you provide the whole completed code for the actionlistener and the method please
need help can someone give the code which i need to allow my calculator to allow the button created from the nested for loop to function with an actionlistener.
here the 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; final 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); } }
You must feel a little bit frustrated alex. Be patient and Norm will help you get through this. For now, in order to keep things simple, why don't you create new objects for each button rather than having a loop do it. With that in place, all you need are action listeners for each button.
Who holds the KEY to all knowledge?
@alex1994: Please don't open duplicate threads. Threads merged.
What is the code in the action listener supposed to do?
If you don't understand my answer, don't ignore it, ask a question.
the actionlistener fr the button in the loop are suppose, once you link on them; that particular number should show up in the JTextField
the code for the JTextField
// create p1 JPanel p1 = new JPanel(); p1.setLayout(new BorderLayout(5, 10)); p1.add(new JTextField(), BorderLayout.NORTH);
--- Update ---
the actionlistener for the button in the loop are suppose, once you have clicked on them; that particular number should show up in the JTextField;
code for JTextFeild
// create p1 JPanel p1 = new JPanel(); p1.setLayout(new BorderLayout(5, 10)); p1.add(new JTextField(), BorderLayout.NORTH);
The ActionEvent object passed to the actionlistener method contains a reference to the button that was pressed. Use the event class's getSource() method to get a reference to that button.
When you get the reference to the button object, you can call one of its methods to get the String that is shown on the button.
If you don't understand my answer, don't ignore it, ask a question.
how do use the getSource() could i have an example.
Topic continues in this thread.