我正在创建一个简单的java计时器,但是当点击“开始”按钮时,负责显示时间的JPanel不会更新。我用摇摆计时器来更新JPanel,但无济于事。我在错误的组件上使用它吗? 这是我的代码......
JFrame(主计时器组件)
if [[ ${USER} != "user1" && "user2" && "user3" && "user4" ]]; then
这是JPanel类(显示时间的内容)
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class theTimer extends JFrame{
//Initialize fields
private TimerPanel tp = new TimerPanel();
private JButton start,stop,reset;
private Dimension buttonSize = new Dimension(80,30);
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Main Method
public static void main(String[] args){
theTimer tT = new theTimer();
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Constructor
public theTimer(){
setLayout(new FlowLayout());
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
add(tp);
addButtonAction();
setButtonSize();
add(start); add(stop); add(reset);
setTitle("Java Study Timer");
setVisible(true);
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Set the size of the timer buttons
private void setButtonSize(){
start.setPreferredSize(buttonSize);
stop.setPreferredSize(buttonSize);
reset.setPreferredSize(buttonSize);
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Gives the buttons functionality
private void addButtonAction(){
start = new JButton("Start");
stop = new JButton("Stop");
reset = new JButton("Reset");
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
tp.startTimer();
}
});
stop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
tp.stopTimer();
}
});
reset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
tp.resetTimer();
}
});
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
}
答案 0 :(得分:1)
请在TimerPanel.java
中插入以下声明:
theTime = min + ":" + sec;
在repaint();
每次出现之前看到结果。
如果进行了上述更改,TimerPanel.java
如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.*;
public class TimerPanel extends JPanel{
private int min,sec;
private String theTime = min + ":" + sec;
private int width=350, height=300;
private boolean timerStarted=false;
private Timer swingTimer = new Timer(900, new ActionListener(){
public void actionPerformed(ActionEvent event){
if(sec<60){
sec++;
theTime = min + ":" + sec;
repaint();
}else{
min++;
sec=0;
theTime = min + ":" + sec;
repaint();
}
}
});
//Constructor
public TimerPanel(){
setPreferredSize(new Dimension(350,300));
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//start the timer
public void startTimer(){
swingTimer.start();
}
//Stop the timer
public void stopTimer(){
swingTimer.stop();
}
//reset the timer
public void resetTimer(){
sec=0; min=0;
theTime = min + ":" + sec;
repaint();
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Paint Method
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.WHITE);
g.setFont(new Font("Arial", Font.PLAIN, 40));
g.drawString(theTime, width/2-45, height/2);
setBackground(Color.BLACK);
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
}
此外,请从tp.updateTime();
删除所有theTimer.java
,因为不需要void PhysicsThread::run(){
qDebug() << "SUCCESSFULLY STARTED UP PHYSICS-SIMULATION";
forever{
mutex.lock();
qDebug() << "RUNNING PHYSICS-SIMULATION";
runSimulation();
if(stop){
mutex.unlock();
break;
}
if(bPause){
pauseManager.wait(&mutex);
}
mutex.unlock();
}
}
void PhysicsThread::runSimulation(){
auto startTime = std::chrono::high_resolution_clock::now();
// Collision Border
for(int i = 0 ; i < pobjectsSphere.size() ; i++) {
PhysicsSphere* op = pobjectsSphere.at(i);
if(op->getIsMovable()){
if(op->getX()-(op->getSize()) < minx ){
if(op->getVelocityX() < 0){
op->setVelocityX(-op->getVelocityX());
}else{
op->setVelocityX(op->getVelocityX());
}
}else if(op->getX()+(op->getSize()) > maxx) {
if(op->getVelocityX() > 0){
op->setVelocityX(-op->getVelocityX());
}else{
op->setVelocityX(op->getVelocityX());
}
}
if(op->getY()-(op->getSize()) < miny){
if(op->getVelocityY() < 0){
op->setVelocityY(-op->getVelocityY() * op->getRemainingEnergy());
}else{
op->setVelocityY(op->getVelocityY());
}
}else{
if(op->getY()+(op->getSize()) > maxy){
if(op->getVelocityY() > 0){
op->setVelocityY(-op->getVelocityY());
}else{
op->setVelocityY(op->getVelocityY());
}
}
// Gravity
op->setVelocityY(op->getVelocityY() + g*deltaTimeMS*op->getMass());
}
if(op->getZ()-(op->getSize()) < minz){
if(op->getVelocityZ() < 0){
op->setVelocityZ(-op->getVelocityZ());
}else{
op->setVelocityZ(op->getVelocityZ());
}
}else if(op->getZ()+(op->getSize()) > maxz){
if(op->getVelocityZ() > 0){
op->setVelocityZ(-op->getVelocityZ());
}else{
op->setVelocityZ(op->getVelocityZ());
}
}
}
}
// Collision Sphere on Sphere
for(int i = 0 ; i < pobjectsSphere.size() ; i++) {
PhysicsSphere* op1 = pobjectsSphere.at(i);
for(int j = i ; j < pobjectsSphere.size() ; j++) {
PhysicsSphere* op2 = pobjectsSphere.at(j);
// Sphere on Sphere
if(i != j && Collision::SphereVersusSphere(op1->getX() ,op1->getY() ,op1->getZ() ,op1->getSize() ,op2->getX() ,op2->getY() ,op2->getZ() ,op2->getSize())){
double tempX (op1->getX() - op2->getX());
double tempY (op1->getY() - op2->getY());
double tempZ (op1->getZ() - op2->getZ());
double norm = sqrt(tempX*tempX + tempY*tempY + tempZ*tempZ);
tempX = tempX / norm;
tempY = tempY / norm;
tempZ = tempZ / norm;
double a1 = (op1->getVelocityX() * tempX) + (op1->getVelocityY() * tempY) + (op1->getVelocityZ() * tempZ);
double a2 = (op2->getVelocityX() * tempX) + (op2->getVelocityY() * tempY) + (op2->getVelocityZ() * tempZ);
double optimizedP = (2.0 * (a1 - a2)) / (op1->getMass() + op2->getMass());
// fix
optimizedP = std::abs(optimizedP);
// 0.9 Verlusst
if(op1->getIsMovable()){
op1->setVelocityX( op1->getVelocityX() + (optimizedP * op2->getMass() * tempX) * (op1->getRemainingEnergy()*op2->getRemainingEnergy()));
op1->setVelocityY( op1->getVelocityY() + (optimizedP * op2->getMass() * tempY) * (op1->getRemainingEnergy()*op2->getRemainingEnergy()));
op1->setVelocityZ( op1->getVelocityZ() + (optimizedP * op2->getMass() * tempZ) * (op1->getRemainingEnergy()*op2->getRemainingEnergy()));
}
if(op2->getIsMovable()){
op2->setVelocityX( op2->getVelocityX() - (optimizedP * op1->getMass() * tempX) * (op1->getRemainingEnergy()*op2->getRemainingEnergy()));
op2->setVelocityY( op2->getVelocityY() - (optimizedP * op1->getMass() * tempY) * (op1->getRemainingEnergy()*op2->getRemainingEnergy()));
op2->setVelocityZ( op2->getVelocityZ() - (optimizedP * op1->getMass() * tempZ) * (op1->getRemainingEnergy()*op2->getRemainingEnergy()));
}
if(!op1->getIsMovable() && op2->getIsMovable()){
op2->setX(op2->getX() - op1->getVelocityX() * deltaTimeMS);
op2->setY(op2->getY() - op1->getVelocityY() * deltaTimeMS);
op2->setZ(op2->getZ() - op1->getVelocityZ() * deltaTimeMS);
op1->setVelocityX(0.0);
op1->setVelocityY(0.0);
op1->setVelocityZ(0.0);
}else if(op1->getIsMovable() && !op2->getIsMovable()){
op1->setX(op1->getX() - op2->getVelocityX() * deltaTimeMS);
op1->setY(op1->getY() - op2->getVelocityY() * deltaTimeMS);
op1->setZ(op1->getZ() - op2->getVelocityZ() * deltaTimeMS);
op2->setVelocityX(0.0);
op2->setVelocityY(0.0);
op2->setVelocityZ(0.0);
}
op1->setX(op1->getX() + op1->getVelocityX() * deltaTimeMS);
op1->setY(op1->getY() + op1->getVelocityY() * deltaTimeMS);
op1->setZ(op1->getZ() + op1->getVelocityZ() * deltaTimeMS);
op2->setX(op2->getX() + op2->getVelocityX() * deltaTimeMS);
op2->setY(op2->getY() + op2->getVelocityY() * deltaTimeMS);
op2->setZ(op2->getZ() + op2->getVelocityZ() * deltaTimeMS);
}
}
}
for(int i = 0 ; i < pobjectsSphere.size() ; i++) {
PhysicsSphere* op1 = pobjectsSphere.at(i);
for(int j = 0 ; j < pobjectsBox.size() ; j++) {
PhysicsBox* op2 = pobjectsBox.at(j);
if(Collision::SphereVersusBox( op1->getX() ,op1->getY() ,op1->getZ() ,op1->getSize() ,op2->getMinX()+op2->getX() ,op2->getMinY()+op2->getY() ,op2->getMinZ()+op2->getZ() ,op2->getMaxX()+op2->getX() ,op2->getMaxY()+op2->getY() ,op2->getMaxZ()+op2->getZ())){
if((op1->getX()+op1->getSize()) > op2->getMinX() && op1->getX() < op2->getMinX()+op2->getX()){
if(op1->getVelocityX() > 0){
op1->setVelocityX(-op1->getVelocityX());
}
}
if((op1->getX()-op1->getSize()) < op2->getMaxX() && op1->getX() > op2->getMaxX()+op2->getX()){
if(op1->getVelocityX() < 0){
op1->setVelocityX(-op1->getVelocityX());
}
}
if((op1->getY()+op1->getSize()) > op2->getMinY() && op1->getY() < op2->getMinY()+op2->getY()){
if(op1->getVelocityY() > 0){
op1->setVelocityY(-op1->getVelocityY());
}
}
if((op1->getY()-op1->getSize()) < op2->getMaxY() && op1->getY() > op2->getMaxY()+op2->getY()){
if(op1->getVelocityY() < 0){
op1->setVelocityY(-op1->getVelocityY());
}
}
if((op1->getZ()+op1->getSize()) > op2->getMinZ() && op1->getZ() < op2->getMinZ()+op2->getZ()){
if(op1->getVelocityZ() > 0){
op1->setVelocityZ(-op1->getVelocityZ());
}
}
if((op1->getZ()-op1->getSize()) < op2->getMaxZ() && op1->getZ() > op2->getMaxZ()+op2->getZ()){
if(op1->getVelocityZ() < 0){
op1->setVelocityZ(-op1->getVelocityZ());
}
}
}
}
}
// Move
for(int i = 0 ; i < pobjectsSphere.size() ; i++) {
PhysicsSphere* op = pobjectsSphere.at(i);
if(op->getIsMovable()){
op->setX(op->getX() + op->getVelocityX()*deltaTimeMS);
op->setY(op->getY() + op->getVelocityY()*deltaTimeMS);
op->setZ(op->getZ() + op->getVelocityZ()*deltaTimeMS);
}else{
op->setVelocityX(0.0);
op->setVelocityY(0.0);
op->setVelocityZ(0.0);
}
}
if(pauseTickTime > 0.0){
this->msleep(pauseTickTime);
}
auto endTime = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> time = endTime - startTime;
deltaTimeNS = std::chrono::duration_cast<std::chrono::nanoseconds>(time).count();
deltaTimeMS = deltaTimeNS / 1000000.0;
//qDebug() << "DeltaT NS: " << deltaTimeNS << " DeltaT MS: " << deltaTimeMS;
}
。希望这可以帮助。