在java中如何获取按钮被按下时的时间,在把它显示出来

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
 
class DisplayTime{
    JButton b1;
    JLabel l1;
    DisplayTime(){
        JFrame mainFrame = new JFrame("Display click button time");
        mainFrame.setLayout(new GridLayout(2,1));
        b1 = new JButton("Get Time");
        l1 = new JLabel();
         
        b1.addActionListener(new Monitor(this));
         
        mainFrame.add(b1);
        mainFrame.add(l1);
         
        mainFrame.pack();
        mainFrame.setDefaultCloseOperation(mainFrame.EXIT_ON_CLOSE);
        mainFrame.setVisible(true);
         
         
         
    }
     
    class Monitor implements ActionListener{
        DisplayTime a;
        Monitor(DisplayTime a){
            this.a = a;
        }
         
        public void actionPerformed(ActionEvent e){
            this.a.l1.setText(new Date().toString());
        }
    }
     
    public static void main(String[] args){
        new DisplayTime();
    }
}

你可能感兴趣的:(java)