我正在制作一个简单的Java游戏来学习它。我想要一个方形女巫的程序,你可以点击它,它改变颜色,比我不想能够点击它8秒。我知道我不会让计时器变得更高,我这样做是为了检查是否可以在使用睡眠之前先退出计时器。
虽然是这样做我的程序卡住了我无法关闭它,所以我尝试线程,但它仍然无法正常工作。有人能告诉我我做错了吗?
import javax.swing.*;
import java.awt.*;
class ViewControll extends JFrame
{
JPanel playerPanel;
Dimension playerPlotDimension;
Plot plot00;
Thread t;
public ViewControll()
{
playerPlotDimension=new Dimension(200,200);
plot00 = new Plot(playerPlotDimension);
t= new Thread(plot00);
createAndShowGUI();
}
private void createAndShowGUI()
{
setTitle("ALPHA 0.1");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setSize(400,400);
setVisible(true);
t.start();
add(plot00);
}
public static void main(String args[])
{
new ViewControll();
}
}
这里是第二类
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
public class Plot extends JPanel implements Runnable
{
int plotstate;
int timer;
public Plot(Dimension playerPlotDimension)
{
plotstate =0;
timer =0;
setBackground(Color.GRAY);
setPreferredSize(playerPlotDimension);
setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY,5,true));
setToolTipText("Werkt Dit?");
}
@Override
public void run()
{
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
switch (plotstate)
{
case 0: setBackground(Color.GREEN);
plotstate = 1;
break;
case 1: setBackground(Color.DARK_GRAY);
while (timer != 8)
{
plotstate =0;
}
break;
}
}
});
}
}
答案 0 :(得分:0)
我这样修好了,这是一个很好的解决方案吗?
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
public class Plot extends JPanel implements Runnable
{
int plotstate;
int growtimer;
public Plot(Dimension playerPlotDimension)
{
plotstate =0;
growtimer =0;
setBackground(Color.GRAY);
setPreferredSize(playerPlotDimension);
setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY,5,true));
setToolTipText("Werkt Dit?");
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
switch (plotstate)
{
case 0: setBackground(Color.GREEN);
plotstate = 1;
break;
case 1: setBackground(Color.DARK_GRAY);
run();
break;
}
}
});
}
@Override
public void run()
{
while (growtimer != 8)
{
plotstate =0;
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
growtimer++;
}
}
答案 1 :(得分:0)
如果您希望您的情节被禁用" 8秒,为什么不存储点击时间,然后忽略后续点击,而8秒没有过去? 类似的东西:
public class Plot extends JPanel implements Runnable
{
int plotstate;
long clicktime;
...
public Plot(Dimension playerPlotDimension)
{
plotstate =0;
growtimer =0;
setBackground(Color.GRAY);
setPreferredSize(playerPlotDimension);
setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY,5,true));
setToolTipText("Werkt Dit?");
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
switch (plotstate)
{
case 0: setBackground(Color.GREEN);
plotstate = 1;
clicktime = System.currentMillis();
break;
case 1: setBackground(Color.DARK_GRAY);
if (System.currentMillis() - clicktime > 8000)
{
plotstate = 0;
}
break;
}
}
});
}