• Post Reply Bookmark Topic Watch Topic
  • New Topic

what i have done  RSS feed

 
Manal Ahmad
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • 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: 9439
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Paul mentioned in your previous post, the more specific your question is, the more likely it is that you will get help. "It doesn't work for me" is not specific. What are you trying to accomplish and what is not working? Does it compile? Does it not work as expected?
It is obvious that you are working on a homework assignment and you won't learn anything if we hand you the answer. Do your part and we'll help you along.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, it will help a lot if you keep related questions in the same thread. That way other people can catch up on the discussion so far and jump into the conversation. So please use a single thread to post updates and additional questions to a related project.

Layne
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please use the CODE UBB tags to enclose code. Doing so will make reading your code much easier.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!