Bookmark Topic Watch Topic
  • New Topic

what i have done  RSS feed

 
Manal Ahmad
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Report post to moderator
i have alredy post the guestion wth the title (how can i solve it )


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class KeyPad extends JPanel implements ActionListener{

private final static int NBUTTONS = 12;
private JButton buttons[];
private String labels[] = // An array of button labels
{ "1","2","3",
"4","5","6",
"7","8","9",
"C","0","."};

/**
* KeyPad() constructor creates the keypad and sets this.kpc
* as a reference to its client.
*/
public KeyPad(KeyPadClient kpc) {
this.kpc = kpc;
buttons = new JButton[NBUTTONS]; // Create the array itself
setLayout( new GridLayout(4,3));
for(int k = 0; k < buttons.length; k++) { // For each button
buttons[k] = new JButton(labels[k]); // Create a labeled button
buttons[k].addActionListener(this); // and a listener
add(buttons[k]); // and add it to the panel
} // for

}


public void actionPerformed(ActionEvent e) {
String keylabel = ((JButton)e.getSource()).getText();
kpc.keypressCallback(keylabel);
}
}





the second class is


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator extends JFrame implements ActionListener{

int arr[] = new int[10];
private JLabel number = new JLabel("Enter a number : ") ;
private JTextField input = new JTextField(10);
private JTextArea display = new JTextArea(10,10);

private JButton insert = new JButton("insert");
private JButton add = new JButton("+");
private JButton sub = new JButton("-");
private JButton mult = new JButton("*");
private JButton divi = new JButton("/");
private JButton equ = new JButton("=");
private KeyPad kpc = new KeyPad(this);



public Calculator() {
getContentPane().setLayout(new FlowLayout());
getContentPane().add(input);
getContentPane().add(kpc);
getContentPane().add(display);
getContentPane().add(sub);
getContentPane().add(mult);
getContentPane().add(divi);
getContentPane().add(equ);
getContentPane().add(insert);
input.setEditable(false);
display.setLineWrap(true);
display.setEditable(false);
mult.addActionListener(this);
sub.addActionListener(this);
divi.addActionListener(this);
equ.addActionListener(this);
insert.addActionListener(this);
} // Converter()
public void keypressCallback(String s) {
if (s.equals("C"))
input.setText(""); // Clear the input
else
input.setText(input.getText() + s); // Type the key
}

public void actionPerformed( ActionEvent e ) {
String buttonLabel = ((JButton)e.getSource()).getText();

if (buttonLabel.equals("insert")) {
displayOriginal.append(input.getText()+" ");

}

if (buttonLabel.equals("+")) {
display.append(" "+"+"+" " );


}

}



public static void main(String args[]) {
Calculator f = new Calculator();
f.setSize(300, 400);
f.setVisible(true);

} // main()


}

but its doesnt work with me
 
Joe Ess
Bartender
Posts: 9441
12
Linux Mac OS X Windows
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!