一下是一段实现秒表功能的代码,有两个地方有些迷惑,请前辈指点一下!import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.util.Date;
import java.text.SimpleDateFormat;
public class StopWatch extends JFrame{
JButton btnStart,btnStop;
JLabel label;
Timer timer;
public StopWatch(){
label = new JLabel("00:00:00.000");
btnStart = new JButton("start");
btnStop = new JButton("stop");
final int delay = 100;
final Date startTime = new Date();
final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.S");
final Action taskPerformer = new AbstractAction(){
public void actionPerformed(ActionEvent evt){
//显示时间
Date d=new Date(System.currentTimeMillis()-startTime.getTime()-28800000);
label.setText(sdf.format(d));
label.repaint();
}
};
btnStart.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
startTime.setTime(new Date().getTime());
timer=new Timer(delay, taskPerformer);
timer.start();
}
});
btnStop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
if(timer!=null&&timer.isRunning())
timer.stop();
}
});
Container c=getContentPane();
c.add(label,BorderLayout.NORTH);
c.add(btnStart,BorderLayout.CENTER);
c.add(btnStop,BorderLayout.SOUTH);
}
public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI(){
StopWatch window=new StopWatch();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.pack();
window.setVisible(true);
}
}
问题一:在 StopWatch构造方法中定义了final等常量,为什么在构造方法外给按钮注册监听时也可以访问呢?没有超出作用域吗?问题二: final Action taskPerformer 构造时用到了匿名的内部类,但是也可以在构造方法外访问,这种用法很常见吗?
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.util.Date;
import java.text.SimpleDateFormat;
public class StopWatch extends JFrame{
JButton btnStart,btnStop;
JLabel label;
Timer timer;
public StopWatch(){
label = new JLabel("00:00:00.000");
btnStart = new JButton("start");
btnStop = new JButton("stop");
final int delay = 100;
final Date startTime = new Date();
final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.S");
final Action taskPerformer = new AbstractAction(){
public void actionPerformed(ActionEvent evt){
//显示时间
Date d=new Date(System.currentTimeMillis()-startTime.getTime()-28800000);
label.setText(sdf.format(d));
label.repaint();
}
};
btnStart.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
startTime.setTime(new Date().getTime());
timer=new Timer(delay, taskPerformer);
timer.start();
}
});
btnStop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
if(timer!=null&&timer.isRunning())
timer.stop();
}
});
Container c=getContentPane();
c.add(label,BorderLayout.NORTH);
c.add(btnStart,BorderLayout.CENTER);
c.add(btnStop,BorderLayout.SOUTH);
}
public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI(){
StopWatch window=new StopWatch();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.pack();
window.setVisible(true);
}
}
问题一:在 StopWatch构造方法中定义了final等常量,为什么在构造方法外给按钮注册监听时也可以访问呢?没有超出作用域吗?问题二: final Action taskPerformer 构造时用到了匿名的内部类,但是也可以在构造方法外访问,这种用法很常见吗?
这只是访问作用,你声明了这个对象,那么肯定在作用域内能够访问啊?这种应用还是很多的。