This is probably an elementary question. However, I have completed reading the 7th Chapter of Java Programming for the Absolute Beginner and have approached the Challenges section. I cannot quite get the clear button to work for the challenge question.
The question asks:
Create a numerical keypad that uses Buttons to update an uneditable TextField by appending the clicked number to the end of its current number. Use a BorderLayout for the Frame. At BorderLayout.NORTH, place the TextField. In the center, create a panel that uses a GridLayout to lay out Buttons 1 through 9 in a three by three grid. At BorderLayout.SOUTH, create another Panel that has the zero key and also a “clear” key that deletes the current number in the TextField."
I think that my main problem is within the TextArea append method. I know that I was supposed to use a TextField, however it seems to be impossible to append within a TextField according to research I have done.
An answer to this question can potentially aid many new Java programmers in understanding basic GUI and Event Handling.
import java.awt.*;
import java.awt.event.*;
public class CalcFacade extends GUIFrame
implements ActionListener, TextListener {
TextField tf;
TextArea ta;
Panel p1, p2;
Label clear;
Button b1, b2, b3, b4, b5, b6, b7, b8, b9, c, b0;
public CalcFacade() {
super("Calculator Facade");
setLayout(new BorderLayout());
Button b1 = new Button("1");
b1.addActionListener(this);
Button b2 = new Button("2");
b2.addActionListener(this);
Button b3 = new Button("3");
b3.addActionListener(this);
Button b4 = new Button("4");
b4.addActionListener(this);
Button b5 = new Button("5");
b5.addActionListener(this);
Button b6 = new Button("6");
b6.addActionListener(this);
Button b7 = new Button("7");
b7.addActionListener(this);
Button b8 = new Button("8");
b8.addActionListener(this);
Button b9 = new Button("9");
b9.addActionListener(this);
Button b0 = new Button("0");
b0.addActionListener(this);
Button c = new Button("Clear");
c.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clear.setText("");
}
});
tf = new TextField(100);
add(tf);
tf.setEnabled(false);
tf.addActionListener(this);
tf.addTextListener(this);
setVisible(false);
ta = new TextArea("", 10, 30);
add(ta);
ta.setEnabled(true);
setVisible(true);
Panel p1 = new Panel();
p1.setLayout(new GridLayout(3, 3));
p1.setBackground(Color.gray);
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(b7);
p1.add(b8);
p1.add(b9);
Panel p2 = new Panel();
p2.setBackground(Color.gray);
p2.add(b0);
p2.add(c);
add(ta, BorderLayout.NORTH);
add(p1, BorderLayout.CENTER);
add(p2, BorderLayout.SOUTH);
pack();
setSize(400, 300);
setVisible(true);
}
public static void main(String args[]) {
CalcFacade cf = new CalcFacade();
}
public void actionPerformed(ActionEvent e) {
tf.setText(""
+((Button)e.getSource()).getLabel());
}
public void textValueChanged(TextEvent e) {
ta.append(tf.getText());
}
}
I thank you very much ahead of time for all of your assistance.