gpt4 book ai didi

Java 时钟在 Swing 中不计数

转载 作者:行者123 更新时间:2023-12-01 18:56:05 25 4
gpt4 key购买 nike

我正在尝试使用 swing 制作秒表,但它不起作用。这是我的代码。 Jlabel 时钟始终显示 -1,只有在停止时才会发生这种情况。我是否正确使用了 invokelater?

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



public class sidePanel extends JApplet implements ActionListener{
JPanel pane;
JLabel clock;
JButton toggle;

Timer timer;
StopWatch stopWatch;


public void init()
{
pane = new JPanel();
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));

clock = new JLabel("00:00");

toggle = new JButton("Start/Stop");
toggle.addActionListener(this);

pane.add(clock);
pane.add(toggle);


timer = new Timer(500, this);
timer.setRepeats(true);

stopWatch = new StopWatch();



add(pane);

}

@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == toggle)
{


if(timer.isRunning())
{
stopWatch.endTime = System.currentTimeMillis();
timer.stop();
}
else
{
stopWatch.startTime = System.currentTimeMillis();
timer.start();
}
}

if(e.getSource() == timer)
{
long time = stopWatch.getElapsedTime();
sidePanel.this.clock.setText(String.valueOf(time));
}
}




private class StopWatch{

private long startTime =0;
private long endTime =0;
public boolean isRunning = false;


public void start(){
startTime = System.currentTimeMillis();
isRunning = true;
}

public void end(){
endTime = System.currentTimeMillis();
isRunning = false;
}

public long getElapsedTime()
{
long currentTime = System.currentTimeMillis();
if(isRunning)
return (currentTime - startTime)/1000;
else
return -1;
}

}






}

工作代码

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



public class sidePanel extends JApplet implements ActionListener{
JPanel pane;
JLabel clock;
JButton toggle;

Timer timer;
//StopWatch stopWatch;

boolean pressed = false;

long startTime =0;
long endTime =0;


public void init()
{
pane = new JPanel();
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));

clock = new JLabel("00:00");

toggle = new JButton("Start/Stop");
toggle.addActionListener(this);

pane.add(clock);
pane.add(toggle);


timer = new Timer(500, this);
timer.setRepeats(true);

//stopWatch = new StopWatch();



add(pane);

}

long cur;
long end;
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == toggle)
{

if(!pressed)
{
timer.start();
startTime = System.currentTimeMillis();
pressed = true;
}
else
{
timer.stop();

pressed = false;
}
}



if(timer.isRunning())
{
endTime = System.currentTimeMillis();
clock.setText(String.valueOf((endTime-startTime)/1000));

}

}

}

最佳答案

您的 StopWatch 类运行一次,然后终止...

public void run() {
// Start here
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run() {
long time = getElapsedTime();
sidePanel.this.clock.setText(String.valueOf(time));
}
});
// End here...
}

线程在其 run 方法(在本例中为您的 StopWatchrun 方法)存在时将终止。

您需要做的是维持一个循环,直到 isRunning 变为 false

public void run() {
while (isRunning) {
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run() {
long time = getElapsedTime();
sidePanel.this.clock.setText(String.valueOf(time));
}
});
// Because we really don't want to bombboard the Event dispatching thread
// With lots of updates, which probably won't get rendered any way,
// We put in a small delay...

// This day represents "about" a second accuracy...
try {
Thread.sleep(500);
} catch (Exception exp) {
}
}
}

使用 javax.swing.Timer 会简单得多...

private Timer timer;
public void init()
{
pane = new JPanel();
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));

clock = new JLabel("00:00");

toggle = new JButton("Start/Stop");
toggle.addActionListener(this);

pane.add(clock);
pane.add(toggle);

timer = new Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
long time = getElapsedTime();
sidePanel.this.clock.setText(String.valueOf(time));
}
});
timer.setRepeats(true);
add(pane);

}

@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == toggle)
{
if(timer.isRunning())
{
endTime = System.currentTimeMillis();
timer.stop();
}
else
{
startTime = System.currentTimeMillis();
timer.start();
}
}
}

然后您可以删除 StopWatch 的功能(即 getElapsedTime())

关于Java 时钟在 Swing 中不计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13888026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com