java - Java 秒表 GUI 程序符合但不运行
问题描述
我有一个任务,我必须创建一个 GUI 秒表程序,该程序具有计时器的开始、停止和重置按钮。
到目前为止,我有一个程序可以正确编译,但是当我运行该程序时会产生下面的错误,所以我不确定如何纠正这个错误。
at java.awt.Container.checkNotAWindow(Container.java:492)
at java.awt.Container.addImpl(Container.java:1093)
at java.awt.Container.add(Container.java:419)
at TimerFrame.main(TimerFrame.java:30)
Process completed.
运行我的程序的任何帮助,或任何其他可以使程序运行更顺畅的改进,将不胜感激。我是学习java的新手,所以任何帮助表示赞赏。
我也在下面包含了我的代码。
提前致谢!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TimerFrame {
public static void main(String[] args){
new TimerPanel();
JFrame frame = new JFrame("Stopwatch GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TimerPanel panel = new TimerPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class TimerPanel extends JFrame implements ActionListener {
private int count;
private JButton start;
private JButton stop;
private JButton reset;
private JLabel label;
private Timer timer;
private double time;
private double extra = 0;
public TimerPanel(){
setLayout(new GridLayout(2, 3));
label = new JLabel("00.00", JLabel.CENTER);
add(label);
// Creating a panel (JPanel) for hte buttons to reside in
JPanel buttons = new JPanel();
// initilizing buttons
start = new JButton("Start");
stop = new JButton("Stop");
reset = new JButton("Reset");
// adds the buttons start, stop, and reset to the panel
buttons.add(start);
buttons.add(stop);
buttons.add(reset);
// adds the panel to the frame
add(buttons);
// adding action listeners to the buttons
start.addActionListener(this);
stop.addActionListener(this);
reset.addActionListener(this);
// initilize timer
timer = new Timer(0, this);
timer.setDelay(1000);
setBackground(Color.pink);
setPreferredSize(new Dimension(500, 300));
}
public void actionPerformed(ActionEvent event){
//find the source of the action
if(event.getSource().equals(timer)){
// records the current time event from timer
double currentTime = System.currentTimeMillis();
// finds the elapsed time from time and currentTime
double elapsed = (double) ( currentTime - time) / 1000;
//adds the extra to the elapsed time
elapsed = elapsed + extra;
//displays time in JLabel label
label.setText(String.format("%.1f", elapsed));
} else if (event.getSource().equals(start)){
// the start button has been clicked
if(!timer.isRunning()){
time = System.currentTimeMillis();
timer.start();
}
} else if (event.getSource().equals(stop)) {
// the stop button has been clicked
if(timer.isRunning()){
//record current time in currentTime
double currentTime = System.currentTimeMillis();
// finds the elapsed time from time and currentTime
double elapsed = (double) ( currentTime - time ) / 1000;
// (double) -> casts whatever is produced to be a double
//adds the extra to the elapsed time
elapsed = elapsed + extra;
//stop the timer
timer.stop();
}
} else{
// the reset button has been clicked
// stops the timer before resetting it
if(timer.isRunning()){
timer.stop();
}
// resets the timer
time = System.currentTimeMillis();
extra = 0;
label.setText("00.00");
}
}
}
解决方案
public class TimerPanel extends JFrame implements ActionListener {
好吧,你调用你的类, TimerPanel
但你扩展了一个 JFrame。
如果该类是一个“面板”,那么它应该扩展JPanel
.
TimerPanel panel = new TimerPanel();
frame.getContentPane().add(panel);
您收到错误是因为您无法将 JFrame 添加到 JFrame。
运行我的程序的任何帮助,或任何其他改进
为什么要在进行测试之前编写整个类?测试应逐步进行。
因此,您首先创建一个框架并对其进行测试。
然后在框架中添加一些组件并测试布局是否正确。
然后您将 ActionListeners 添加到您的按钮(一次一个)并测试它们。
这样,当你遇到问题时,你就会知道你刚刚改变了什么。
推荐阅读
- activemq - MassTransit - 启动总线时 AmazonMQ 的套接字异常
- r - 使用不断变化的 shapefile(只有 4 个不同的 shapefile)制作动画地图挂起
- javascript - 通过单击关闭 JS 关闭弹出窗口
- python - Discord.py 向特定频道发送消息
- python - 狮身人面像:modules.rst:警告:文档不包含在任何目录树中
- node.js - 内置中间件功能
- r - 并行运行 R 函数 / 使用 furr / 语法
- yaml - jekyll 博客新行(最小错误)
- javascript - 点击图片更改网址
- scheme - 获得平均值的计划程序不识别最后一个数字