首页 > 解决方案 > 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");
        }
   }
}

标签: javaswinguser-interfacetimerstopwatch

解决方案


public class TimerPanel extends JFrame implements ActionListener  {

好吧,你调用你的类, TimerPanel但你扩展了一个 JFrame。

如果该类是一个“面板”,那么它应该扩展JPanel.

  TimerPanel panel = new TimerPanel();
  frame.getContentPane().add(panel);

您收到错误是因为您无法将 JFrame 添加到 JFrame。

运行我的程序的任何帮助,或任何其他改进

为什么要在进行测试之前编写整个类?测试应逐步进行。

因此,您首先创建一个框架并对其进行测试。

然后在框架中添加一些组件并测试布局是否正确。

然后您将 ActionListeners 添加到您的按钮(一次一个)并测试它们。

这样,当你遇到问题时,你就会知道你刚刚改变了什么。


推荐阅读