gpt4 book ai didi

java - 尝试创建进度条

转载 作者:行者123 更新时间:2023-11-29 03:51:43 25 4
gpt4 key购买 nike

这只是一个力学练习。我正在尝试创建三个自定义面板来控制自己的进度条。这是我为自己编写的时间管理程序的一部分,以了解更多有关 Java 的信息。较大的程序使用用户输入的日期来创建进度条的最小/最大框架。这个程序和更大的程序都表现出相同的行为,多个条形图争分夺秒。

我遇到的问题是,如果我只有一个小节,一切似乎都正常,但是当我有多个小节时,一切似乎都会崩溃。所以我写了这个小程序来测试一些东西。它非常简单,需要三个自定义面板,给它们一个标签并使用一个计时器事件来更改标签和进度条的位置。我的问题是,如果数学结果一致(系统输出显示计算结果)并且我每秒(1000 毫秒)都在计算事件,为什么一切都在倒计时。

请原谅我的代码缺乏形式。我更关心逻辑而不是形式。

(下面的大部分内容都是从我的大型程序中截取的,所以如果你看到无关的部分,它们确实有一个家)提前谢谢你。

import javax.swing.*;
import javax.swing.Timer;
import java.awt.*;
import java.awt.Color;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;

public class plaything extends JFrame implements ActionListener
{
myPanel[] mp = new myPanel[3];
JLabel[] jl = new JLabel[3];
short[] tim = new short[3];
short x = 0;
short t = 0; //used to stagger the clocks
short dateSaver; //holds temp dates

public plaything()
{
setSize(400, 350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(3, 1) );

for(short x = 0; x < 3; x++)
{
mp[x] = new myPanel();

//sets all three bars to different 'times'
dateSaver = (short)(10 + t) ;
tim[x] = dateSaver;
mp[x].setMax( dateSaver );

jl[x] = new JLabel("Expires: " + dateSaver);
this.add(mp[x]);
mp[x].add( jl[x] );
t += 15; // 15 seconds
}

Timer time = new Timer(1000, this);
time.start();
}
public void actionPerformed(ActionEvent e)
{
if ( x < 60 )
{
x++;
}
else
{
x = 1;
}
for(myPanel m : mp)
{
m.tock();
}
for(short x = 0; x < 3; x++ )
{
mp[x].tock();
jl[x].setText( "" + --tim[x] );
}
}


private class myPanel extends JPanel
{
//Fields
private boolean finished = false;
//(x,y) Coords
private short x = 15;
private short y = 50;
//Size and shape
private short width = 200;
private short height = 10;
private short arcSize = 10;
//Bar essentials
private double max; //highest range of bar
private double fill = width; //sets filled in portion
private double tick; //calculates movement per event
private Color urgent = Color.BLUE; // Changes the color depending on the Urgency

//Constructors
public myPanel()
{
this.setBackground( Color.WHITE );
repaint();
}

//Mutators ( tick manipulation )
public void setMax( double maxIn )
{
this.max = maxIn;
System.out.println("Max: " + this.max );
this.tick = (double)width / this.max;
System.out.println("tick: " + this.tick );
}

//Methods
//Tick Manipulation
public void tock()
{
//Ends when zero
if( fill < 1 )
{
fill = width;
finished = true;
tick = 0;
urgent = Color.BLUE;
repaint();
}
else
{
fill -= tick ;
System.out.println("fill is " + fill );
repaint();
}

}
//Paint method
public void paint( Graphics g)
{
super.paint(g);
Graphics2D g2 = (Graphics2D)g;
g2.setColor( urgent );
g2.draw(new RoundRectangle2D.Double(x,y + 40, width, height, arcSize, arcSize) );
g2.fill(new RoundRectangle2D.Double(x,y + 40, fill , height, arcSize, arcSize) );
}
}
public static void main(String[] args)
{
plaything pt = new plaything();
pt.setVisible(true);
}
}

我真正唯一关心的是关于条形图和标签的进展我的逻辑哪里有缺陷。我希望找到使两者一起达到零的方法。 (仅在条形图上进行了两天的研究和工作)

再次感谢您的宝贵时间。

最佳答案

您在 Timer 的每次迭代中调用 tock() 两次:

    for(myPanel m : mp)
{
m.tock(); // ONCE
}
for(short x = 0; x < 3; x++ )
{
mp[x].tock(); // TWICE
jl[x].setText( "" + --tim[x] );
}

您应该删除一个电话或另一个电话。

关于java - 尝试创建进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8468692/

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