ボタンをクリックしたときに何をする必要がありますか?彼はボタン内のテキストを教えてくれますか? このコードでは、ボタンをクリックすると、最後の "i" var 値が返されるためです...この場合、彼は私に "5" を与えます。
for(int i; i < 5; i++) {
JButton button = new JButton();
button.setText("" + i);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
// TODO add your handling code here:
System.out.print("\n Test: " + button.getText());
}
});
button.setSize(60,20);
button.setLocation(100, 140);
button.setVisible(true);
this.add(button);
this.revalidate();
this.repaint();
}