执行的swing JAVA操作不起作用
我正在为他创建按钮,当我点击他的时候。。什么都没发生。。。当我点击btn时,没有调用函数btnActionPerformed。。。如何让它工作
private void btButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here
int[] ret = new int[SQL.freetables().size()];
Iterator<Integer> iterator = SQL.freetables().iterator();
for (int i = 0; i < ret.length; i++)
{
ret[i] = iterator.next().intValue();
int num=SQL.freetables().size() + 1;
this.btn = new JButton();
this.btn.setText("" + ret[i]);
this.btn.setSize(60,20);
int x = 100+(80*i);
this.btn.setLocation(x, 140);
this.btn.setVisible(true);
this.add(btn);
// }
}
this.revalidate();
this.repaint();
}
private void btnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.out.print("\b Test: " + btn.getText());
}
# 1 楼答案
你需要注册到
actionPreformed
你的代码应该是:
# 2 楼答案
您必须实现
ActionListener
接口。这两种方法都不符合我看到的所需签名http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html
方法是
actionPerformed
。监听器必须连接到JButton
。我在你的代码中都没有看到你似乎非常需要一个Swing tutorial