Java 秒表 GUI 程序符合但不符合 运行
Java stopwatch GUI program complies but does not run
我有一个任务,我必须创建一个 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 {
好吧,你调用你的 class TimerPanel
但你扩展了一个 JFrame。
如果 class 是 "panel" 那么它应该扩展 JPanel
.
TimerPanel panel = new TimerPanel();
frame.getContentPane().add(panel);
您收到错误是因为您无法将 JFrame 添加到 JFrame。
Any help with running my program, or any other improvements
为什么要在测试之前写完整个 class?测试应该一步一步来。
所以您首先创建一个框架并对其进行测试。
然后将一些组件添加到框架中并测试布局是否正确。
然后将 ActionListeners 添加到按钮(一次一个)并测试它们。
这样当您遇到问题时,您就知道您刚刚更改了什么。
我有一个任务,我必须创建一个 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 {
好吧,你调用你的 class TimerPanel
但你扩展了一个 JFrame。
如果 class 是 "panel" 那么它应该扩展 JPanel
.
TimerPanel panel = new TimerPanel();
frame.getContentPane().add(panel);
您收到错误是因为您无法将 JFrame 添加到 JFrame。
Any help with running my program, or any other improvements
为什么要在测试之前写完整个 class?测试应该一步一步来。
所以您首先创建一个框架并对其进行测试。
然后将一些组件添加到框架中并测试布局是否正确。
然后将 ActionListeners 添加到按钮(一次一个)并测试它们。
这样当您遇到问题时,您就知道您刚刚更改了什么。