我通过实现ActionListener看到了ActionListener在代码中使用的一个例子。但在这里我想通过使用Ref.Var来使用ActionListener的功能。 of ActionListener。
JButton createButton(){
ActionListener al;
JButton button = new JButton();
button.setBounds(130, 100, 100, 40);
button.setText("aaa");
button.setSize(100, 40);
button.setLayout(null);
frame.add(button);
return button;
}
在这里查看ActionListener引用。如何在按钮上使用此ref.var来监听按钮上的事件
JButton createButton(){
ActionListener al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
// run code;
}
};
JButton button = new JButton();
button.setBounds(130, 100, 100, 40);
button.setText("aaa");
button.setSize(100, 40);
button.addActionListener(al);
frame.add(button);
return button;
}
要么
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//run code;
}
} );
它与包含类实现ActionListener的基本完全相同:您提供了一个实现,并将按钮配置为侦听它。