deeppatel1986
asked on
In the numeric keypad program, if i press sum number it should be displayed in the label in the top white space. How do i do that? Please some one help me asap
In the numeric keypad program, if i press sum number it should be displayed in the label in the top white space. How do i do that? Please some one help me asap
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class NumericKeyPad extends JPanel {
//-----------------------------------------------------------------
// Creates several bordered panels and displays them.
//-----------------------------------------------------------------
public static void main(String[] args) {
JFrame frame = new JFrame("Numeric Keypad");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 3));
panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
panel.setBackground(Color.white);
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton b11 = new JButton("");
JButton b0 = new JButton("0");
JButton b12 = new JButton("");
panel.add(b1);
panel.add(b2);
panel.add(b3);
panel.add(b4);
panel.add(b5);
panel.add(b6);
panel.add(b7);
panel.add(b8);
panel.add(b9);
panel.add(b11);
panel.add(b0);
panel.add(b12);
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(panel,BorderLayout.CENTER);
JButton clearBt = new JButton("Clear");
JTextField number = new JTextField();
number.setColumns(15);
frame.getContentPane().add(number,BorderLayout.NORTH);
frame.getContentPane().add(clearBt,BorderLayout.EAST);
frame.pack();
frame.setVisible(true);
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Following link can be helpful.
make a generic listener for all butons
http://www.roseindia.net/java/example/java/awt/KeyPress.shtml
make a generic listener for all butons
http://www.roseindia.net/java/example/java/awt/KeyPress.shtml
ASKER
Hey savan i need actionListener for the clear button. Please help ASAP
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class NumericKeyPad extends JPanel {
//-----------------------------------------------------------------
// Creates several bordered panels and displays them.
//-----------------------------------------------------------------
int preInt=0;
public static void main(String[] args) {
JFrame frame = new JFrame("Numeric Keypad");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 3));
panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
panel.setBackground(Color.white);
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton b11 = new JButton("");
JButton b0 = new JButton("0");
JButton b12 = new JButton("");
panel.add(b1);
panel.add(b2);
panel.add(b3);
panel.add(b4);
panel.add(b5);
panel.add(b6);
panel.add(b7);
panel.add(b8);
panel.add(b9);
panel.add(b11);
panel.add(b0);
panel.add(b12);
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(panel,BorderLayout.CENTER);
JButton clearBt = new JButton("Clear");
final JTextField number = new JTextField();
number.setColumns(15);
frame.getContentPane().add(number,BorderLayout.NORTH);
frame.getContentPane().add(clearBt,BorderLayout.EAST);
ActionListener actionListener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// get the number entered
String digit = e.getActionCommand();
// append number to the text field
number.setText(number.getText()+digit);
}
};
ActionListener actionListener1 = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//String clear = e.getActionCommand();
number.setText("");
}
};
b1.addActionListener(actionListener);
b2.addActionListener(actionListener);
b3.addActionListener(actionListener);
b4.addActionListener(actionListener);
b5.addActionListener(actionListener);
b6.addActionListener(actionListener);
b7.addActionListener(actionListener);
b8.addActionListener(actionListener);
b9.addActionListener(actionListener);
b11.addActionListener(actionListener);
b0.addActionListener(actionListener);
b12.addActionListener(actionListener);
clearBt.addActionListener(actionListener);
frame.pack();
frame.setVisible(true);
}
}
> clearBt.addActionListener( actionList ener);
change to:
clearBt.addActionListener( actionList ener1);
change to:
clearBt.addActionListener(
ASKER
Thanks a lot
Open in new window