gpt4 book ai didi

Java 编程挑战 : Creating a GUI Calculator-Like Interface

转载 作者:行者123 更新时间:2023-11-30 11:37:23 24 4
gpt4 key购买 nike

这可能是一个初级问题。但是,我已经读完了 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com