- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这可能是一个初级问题。但是,我已经读完了 Java Programming for the Absolute Beginner 的第 7 章,并接近了挑战部分。我不太明白解决挑战问题的明确按钮。
问题是:
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."
我认为我的主要问题出在 TextArea append 方法中。我知道我应该使用 TextField,但是根据我所做的研究,似乎不可能在 TextField 中追加。
这个问题的答案可能会帮助许多新的 Java 程序员理解基本的 GUI 和事件处理。
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());
}
}
非常感谢您的所有帮助。
最佳答案
TextField 不需要只监听按钮的事件。
要将数字添加到末尾,只需将文本设置为 TextField 中已有的内容加上按钮标签即可。
只有一个 ActionListener 和一个 actionPerformed 方法;识别按钮并适本地设置 TextField 即 c.addActionListener(this);
public void actionPerformed(ActionEvent e)
{
Button b = (Button) e.getSource();
if (b.getLabel().equals("Clear"))
{
tf.setText("");
}
else
{
tf.setText(tf.getText() + b.getLabel());
}
}
关于Java 编程挑战 : Creating a GUI Calculator-Like Interface,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14076524/
我是一名优秀的程序员,十分优秀!