下面是Java实现秒表功能的完整攻略:
确定界面布局
首先,要确定秒表界面的布局,可以使用Swing包提供的JFrame
、JLabel
等组件。
一个简单的秒表GUI界面可以包含如下控件:
- 用于显示秒数的
JLabel
。 - 用于开始、停止、清零等操作的
JButton
。 - (可选)用于记录计时时间的列表或文本框。
编写计时器逻辑
初始秒表面板布局完成后,需要编写计时器逻辑。在Java中,可以使用javax.swing.Timer
类来实现定时器的逻辑。
比如,可以在每次定时器执行时,更新当前的时间,并将其显示在界面上,例如:
// 创建定时器,每1000ms执行一次
Timer timer = new Timer(1000, e -> {
currentTime += 1; // 当前时间加1秒
timeLabel.setText(String.valueOf(currentTime)); // 更新时间文本框显示
});
添加按钮事件监听器
下一步是编写按钮事件监听器,负责处理按钮点击事件。例如,当用户点击Start
按钮时,启动定时器。
// Start按钮事件监听器
startButton.addActionListener(e -> {
// 定时器已经启动,不需要重新启动
if (timer != null && timer.isRunning()) {
return;
}
// 创建新的定时器
timer = new Timer(1000, event -> {
currentTime += 1;
timeLabel.setText(String.valueOf(currentTime));
});
timer.start(); // 启动定时器
});
当用户点击Reset
按钮时,重置计时器并清空文本框。
// Reset按钮事件监听器
resetButton.addActionListener(e -> {
currentTime = 0;
timeLabel.setText("0");
if (timer != null) {
timer.stop();
}
});
示例1:简单的秒表实现
下面是一个简单的秒表实现示例:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.Dimension;
public class SimpleStopwatch {
private final JFrame frame;
private final JLabel timeLabel;
private final JButton startButton;
private final JButton resetButton;
private Timer timer;
private int currentTime;
public SimpleStopwatch() {
this.frame = new JFrame("Simple Stopwatch");
this.timeLabel = new JLabel("0", JLabel.CENTER);
this.startButton = new JButton("Start");
this.resetButton = new JButton("Reset");
this.currentTime = 0;
initUI();
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void initUI() {
JPanel panel = new JPanel(new BorderLayout());
panel.setPreferredSize(new Dimension(200, 100));
panel.add(timeLabel, BorderLayout.CENTER);
JPanel btnPanel = new JPanel();
btnPanel.add(startButton);
btnPanel.add(resetButton);
panel.add(btnPanel, BorderLayout.SOUTH);
frame.getContentPane().add(panel);
startButton.addActionListener(e -> {
if (timer != null && timer.isRunning()) {
return;
}
timer = new Timer(1000, event -> {
currentTime += 1;
timeLabel.setText(String.valueOf(currentTime));
});
timer.start();
});
resetButton.addActionListener(e -> {
currentTime = 0;
timeLabel.setText("0");
if (timer != null) {
timer.stop();
}
});
}
public static void main(String[] args) {
new SimpleStopwatch();
}
}
示例2:添加记录功能的秒表
即使只是一个简单的秒表,也可以提供更多的功能。例如,可以添加记录时间的功能。
下面是一个添加记录时间功能的秒表的示例:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Timer;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.ArrayList;
public class StopwatchWithRecord {
private final JFrame frame;
private final JLabel timeLabel;
private final JButton startButton;
private final JButton recordButton;
private final JButton resetButton;
private final JList<String> recordList;
private final JScrollPane scrollPane;
private Timer timer;
private int currentTime;
private final ArrayList<String> records;
public StopwatchWithRecord() {
this.frame = new JFrame("Stopwatch with Record");
this.timeLabel = new JLabel("0", JLabel.CENTER);
this.startButton = new JButton("Start");
this.recordButton = new JButton("Record");
this.resetButton = new JButton("Reset");
this.recordList = new JList<>();
this.recordList.setVisibleRowCount(7);
this.scrollPane = new JScrollPane(this.recordList);
this.currentTime = 0;
this.records = new ArrayList<>();
initUI();
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void initUI() {
JPanel panel = new JPanel(new BorderLayout());
panel.setPreferredSize(new Dimension(250, 200));
panel.add(timeLabel, BorderLayout.CENTER);
JPanel btnPanel = new JPanel();
btnPanel.add(startButton);
btnPanel.add(recordButton);
btnPanel.add(resetButton);
panel.add(btnPanel, BorderLayout.SOUTH);
JPanel recordPanel = new JPanel(new BorderLayout());
recordPanel.setPreferredSize(new Dimension(200, 150));
recordPanel.add(scrollPane, BorderLayout.CENTER);
panel.add(recordPanel, BorderLayout.EAST);
frame.getContentPane().add(panel);
startButton.addActionListener(e -> {
if (timer != null && timer.isRunning()) {
return;
}
timer = new Timer(1000, event -> {
currentTime += 1;
timeLabel.setText(String.valueOf(currentTime));
});
timer.start();
});
resetButton.addActionListener(e -> {
currentTime = 0;
timeLabel.setText("0");
if (timer != null) {
timer.stop();
}
records.clear();
recordList.setListData(new String[]{});
});
recordButton.addActionListener(e -> {
records.add(timeLabel.getText());
recordList.setListData(records.toArray(new String[0]));
});
}
public static void main(String[] args) {
new StopwatchWithRecord();
}
}
以上就是Java实现秒表功能的完整攻略。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:java实现秒表功能 - Python技术站