展开全部
给你代码。import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Test extends JFrame implements ActionListener {
private JTextField text = null;
private JTextArea area = null;
private SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
private Calendar time = Calendar.getInstance();
private JButton start = null;
private JButton complete = null;
private Timer timer = null;
public Test() {
time.set(Calendar.MINUTE, 25);
time.set(Calendar.SECOND, 0);
JPanel top = new JPanel();
top.setLayout(new BorderLayout());
JLabel label = new JLabel("time:");
text = new JTextField("25:00");
text.setEditable(false);
top.add(label, BorderLayout.WEST);
top.add(text, BorderLayout.CENTER);
add(top, BorderLayout.NORTH);
area = new JTextArea();
area.setEnabled(false);
JScrollPane scrollpane = new JScrollPane(area);
add(scrollpane, BorderLayout.CENTER);
JPanel buttons = new JPanel();
buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
start = new JButton("start");
start.addActionListener(this);
buttons.add(start);
complete = new JButton("Complete");
complete.addActionListener(this);
buttons.add(complete);
add(buttons, BorderLayout.SOUTH);
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Test();
}
@62616964757a686964616fe59b9ee7ad9431333332613732Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
if ("start".equals(e.getActionCommand())) {
area.setEnabled(true);
timer = new Timer();
timer.schedule(new TimeTask(), new Date(), 1000);
start.setEnabled(false);
} else {
start.setEnabled(true);
area.setEnabled(false);
timer.cancel();
}
}
class TimeTask extends TimerTask {
@Override
public void run() {
time.add(Calendar.SECOND, -1);
String strTime = sdf.format(time.getTime());
text.setText(strTime);
if ("00:00".equals(strTime)) {
start.setEnabled(true);
area.setEnabled(false);
this.cancel();
}
}
}
}