我有一个简单的程序,它应该接受来自文本字段或按钮的输入。
但是,该按钮完全没有响应。我错过了一些明显的东西吗?
public class BoxDiagram extends GraphicsProgram {
private static final int MAX_CHARS = 25;
public void init() {
addActionListeners();
nameField = new JTextField(MAX_CHARS);
nameField.addActionListener(this);
addButton = new JButton("Add");
add(nameField, SOUTH);
add(addButton, SOUTH);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == addButton || source == nameField) {
add(new GLabel("hello", getWidth() / 2, getHeight() / 2));
}
}
private JTextField nameField;
private JButton addButton;
}