这只是一个力学练习。我试图创建三个自定义面板来控制自己的进度条。这是我为自己写的一个时间管理程序的一部分,以了解更多关于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);
}
}
我真正唯一担心的是我的逻辑有瑕疵,关于酒吧和标签的进展。我希望找到如何使两者达到零。 (两天的研究和工作只是酒吧)
再次感谢您的时间。
你说“一切都会崩溃”,究竟发生了什么,你能更具体吗?他们中的一个能够正常完成,但其余的人立即完成?或者他们都立即完成?你的'System.out.println()'方法调用的输出是什么? –
这个程序发布后,所有三行快速完成,第一个快5秒,第二个快12秒,最后快23秒。 System.out的所有输出都是您所期望的。数学排队,但酒吧仍然提前结束。在我稍微模拟的较大程序中。一个(最长的一个)完成正确,其余的一切都很快。 – Powe8525
你说,''请原谅我的代码缺乏形式,我更关心逻辑而不是形式。“当你为自己编写代码时,这很好,但是当你编写代码并要求其他人尝试评估和理解它时,特别是志愿者,那么这是错误的。在此处张贴代码时使用适当的表单表示尊重我们的时间和努力,并表明您愿意付出努力,以便为您的利益做出努力***更轻松。 –