-2

テキスト フィールドまたはボタンからの入力を受け入れる単純なプログラムがあります。

ただし、ボタンはまったく反応しません。明らかな何かが欠けていますか?

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;

}
4

1 に答える 1

2

このステートメントがありません:addButton.addActionListener(this);名前フィールドに追加しただけです。

于 2013-03-22T16:09:54.280 に答える