I am trying to add the ActionListener coding to my code, but i can't seem to figure out the rest of the code, am i missing something, i'll provide my code and my attempted ActionListener coding that i tried implementing in there, any help would be nice, thanks.
import java.awt.event.*; import java.awt.*; import javax.swing.*; public class Keypad extends JFrame implements ActionListener { private static final int i = 1; public Keypad() { JPanel p1 = new JPanel(); p1.setLayout(new GridLayout(4, 3, 5, 5)); //Adding Buttons for (int i = 1; i<= 9; i++) { p1.add (new Button("" + i)); } p1.add(new JButton("*")); p1.add(new JButton("" + 0)); p1.add(new JButton("#")); JPanel p2 = new JPanel(new BorderLayout()); p2.add(new JTextField(""), BorderLayout.NORTH); p2.add(p1, BorderLayout.CENTER); add(p2, BorderLayout.WEST); add(new JButton("Clear"), BorderLayout.CENTER); } //Main Method public static void main(String[] args){ Keypad frame = new Keypad(); frame.setTitle("Phone"); frame.setSize(300, 350); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
Here is my attempted ActionListener Coding: (it doesn't work though)
String[] btnLabels = "1,2,3,4,5,6,7,8,9,0,#,* ".split(","); { for( int i = 1; i< btnLabels.length; i++ ); JButton btn = new JButton( btnLabels[i] ); btn.addActionListener( this ); add( btn ); } public void actionPerformed( ActionEvent evt ) { if( evt.getSource() instanceof JButton ) { JButton btn = (JButton)evt.getSource(); AbstractButton label = null; if( btn.getText().equalsIgnoreCase("clear")) label.setText(""); } }