不明白的是这句:final JButton jb=new Button();,为什么要加final,final在这里的作用是什么呢?
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MySwing extends JFrame{
private int count;
private JButton jb;
public MySwing(){
count=0;
final JButton jb=new JButton();
this.setTitle("创建按钮");
jb.setText("按钮按下了0次");
jb.setMnemonic('a');
this.add(jb);
jb.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
jb.setText("按钮按下了"+(++count)+"次");
}
});
this.setBounds(300, 250, 300, 200);
this.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MySwing s=new MySwing();
}
}