开发者社区> 问答> 正文

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");
        }
   }
}

问题来源:Stack Overflow

展开
收起
montos 2020-03-25 21:39:19 413 0
1 条回答
写回答
取消 提交回答
  • public class TimerPanel extends JFrame implements ActionListener  {
    

    好吧,您可以调用类, TimerPanel但可以扩展JFrame。

    如果该类是“面板”,则应该扩展JPanel。

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

    因为无法将JFrame添加到JFrame中,所以出现错误。

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

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

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

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

    然后,将ActionListeners一次添加到您的按钮中,并对其进行测试。

    这样,当您遇到问题时,您就知道自己刚刚做了什么更改。

    回答来源:Stack Overflow

    2020-03-25 21:40:13
    赞同 展开评论 打赏
问答分类:
问答地址:
相关产品:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载