捕捉mouseClicked()事件的图像闪烁和计时器-Java Swing
我现在正在编写一个游戏,其中一个图像在捕捉mouseClicked()事件的图像闪烁和计时器-Java Swing,java,image,swing,timer,jlabel,Java,Image,Swing,Timer,Jlabel,我现在正在编写一个游戏,其中一个图像在JFrame上闪烁,每次出现时,用户都必须单击一个按钮来记录他的反应时间 必须存储每个反应时间 面临的问题: 无法使用Swing Timer类闪烁图像 如何在每次图像闪烁时捕获mouseclicked()事件?如何以数组或集合的形式记录单击时间 这是我的代码: import javax.swing.*; import java.awt.*; import java.awt.event.*; class Test { JFrame fr
JFrame
上闪烁,每次出现时,用户都必须单击一个按钮来记录他的反应时间
必须存储每个反应时间
面临的问题:
- 无法使用Swing Timer类闪烁图像
- 如何在每次图像闪烁时捕获mouseclicked()事件?如何以数组或集合的形式记录单击时间
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Test
{
JFrame fr, fr1;
JLabel startLabel, clickArea, timerLocation;
ImageIcon ic;
JButton click, restart, quit;
JPanel jp1, jp2, jp3;
boolean showpic = false;
Timer timer;
Test()
{
fr= new JFrame("Advanced Mode: Reflex Master");
fr.getContentPane().setBackground(Color.BLUE);
fr.setSize(800,800);
fr.setVisible(true);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jp1= new JPanel();
startLabel = new JLabel("Click on the image, whenever it appears! And be QUICK!!!", JLabel.CENTER);
startLabel.setOpaque(true);
startLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
startLabel.setFont(new Font("Times New Roman", Font.BOLD, 26));
startLabel.setBackground(Color.RED);
startLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
jp1.add(startLabel);
jp1.setBackground(Color.YELLOW);
/*timerLocation = new JLabel(new ImageIcon("time.png"));
timerLocation.setOpaque(true);
timerLocation.setAlignmentX(Component.RIGHT_ALIGNMENT);
jp1.add(timerLocation);*/
fr.add(jp1, BorderLayout.NORTH);
jp2 = new JPanel();
GridLayout gL = new GridLayout(0,1);
jp2.setLayout(gL);
ImageIcon ic = new ImageIcon("time.png");
clickArea = new JLabel(ic);
fr.add(clickArea, BorderLayout.CENTER);
click = new JButton("Click Here!");
jp2.add(click);
ActionListener aL = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if(clickArea != null)
{
if(!showpic)
{
clickArea.setVisible(false);
}
clickArea.setVisible(true);
showpic = !showpic;
}
else
{
clickArea.setText("Picture not available");
}
}
};
timer = new Timer(600, aL);
timer.setRepeats(true);
timer.start();
restart = new JButton("Restart");
jp2.add(restart);
quit = new JButton("Quit");
jp2.add(quit);
click.setFocusable(false);
restart.setFocusable(false);
quit.setFocusable(false);
fr.add(jp2, BorderLayout.EAST);
jp3 = new JPanel();
jp3.add(new JLabel("Time: "));
timerLocation = new JLabel("", SwingConstants.CENTER);
jp3.add(timerLocation);
fr.add(jp3, BorderLayout.SOUTH);
new CountDownTimer(this).start();
fr.pack();
fr.setResizable(false);
}
public void settimerLocationText(String text)
{
timerLocation.setText(text);
}
public void endTimerGUI()
{
timer.stop();
fr1= new JFrame("Result");
fr1.getContentPane().setBackground(Color.BLACK);
fr1.setVisible(true);
fr1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String s[])
{
new Test();
}
}
class CountDownTimer
{
private static final int TIMER_PERIOD = 1000;
protected static final int MAX_COUNT = 60;
private Test test; // holds a reference to the Test class
private int count;
public CountDownTimer(Test test)
{
this.test = test; // initializes the reference to the Test class.
String text = "(" + (MAX_COUNT - count) + ") seconds left";
test.settimerLocationText(text);
}
public void start()
{
new Timer(TIMER_PERIOD, new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
if (count < MAX_COUNT)
{
count++;
String text = "(" + (MAX_COUNT - count) + ") seconds left";
test.settimerLocationText(text); // uses the reference to
}
else
{
((Timer) e.getSource()).stop();
test.endTimerGUI();
}
}
}).start();
}
}
enter code here
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
课堂测试
{
JFrame-fr,fr1;
JLabel标签、单击区域、时间位置;
图像图标ic;
JButton单击,重新启动,退出;
JPanel jp1、jp2、jp3;
布尔showpic=false;
定时器;
测试()
{
fr=新JFrame(“高级模式:反射主控”);
fr.getContentPane().setBackground(颜色为.BLUE);
fr.setSize(800800);
fr.setVisible(真);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jp1=新的JPanel();
startAbel=new JLabel(“在图像出现时单击它!并快速!!!”,JLabel.CENTER);
astartabel.setOpaque(真);
STARTABEL.setAlignmentX(组件中心对齐);
setFont(新字体(“Times new Roman”,Font.BOLD,26));
受挫地(颜色:红色);
setboorder(BorderFactory.createLineBorder(Color.BLACK));
jp1.增补(abel);
jp1.挫折背景(颜色:黄色);
/*timerLocation=newjlabel(newimageicon(“time.png”);
timerLocation.setOpaque(true);
timerLocation.setAlignmentX(组件右对齐);
jp1.添加(时间位置)*/
fr.add(jp1,BorderLayout.NORTH);
jp2=新的JPanel();
GridLayout gL=新的GridLayout(0,1);
jp2.设置布局(gL);
ImageIcon ic=新的ImageIcon(“time.png”);
clickArea=新的JLabel(ic);
fr.add(单击区域、边框布局、中心);
单击=新建JButton(“单击此处!”);
jp2.添加(单击);
ActionListener aL=新的ActionListener()
{
已执行的公共无效行动(行动事件ae)
{
如果(单击区域!=null)
{
如果(!showpic)
{
clickArea.setVisible(假);
}
单击Area.setVisible(真);
showpic=!showpic;
}
其他的
{
单击Area.setText(“图片不可用”);
}
}
};
定时器=新定时器(600,aL);
timer.setRepeats(真);
timer.start();
重启=新的JButton(“重启”);
jp2.add(重启);
quit=新的JButton(“quit”);
jp2.加入(退出);
单击.setFocusable(false);
restart.setFocusable(false);
quit.setFocusable(false);
fr.add(jp2,BorderLayout.EAST);
jp3=新的JPanel();
jp3.添加(新的JLabel(“时间:”);
timerLocation=newjlabel(“,SwingConstants.CENTER);
jp3.添加(时间位置);
fr.add(jp3,南部边界布局);
新的倒计时(this.start();
fr.pack();
fr.setresizeable(假);
}
public void settimerLocationText(字符串文本)
{
timerLocation.setText(文本);
}
公共void endTimerGUI()
{
timer.stop();
fr1=新的JFrame(“结果”);
fr1.getContentPane().setBackground(颜色:黑色);
fr1.设置可见(真);
fr1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
公共静态void main(字符串s[]
{
新测试();
}
}
类倒计时
{
专用静态最终整数计时器\u周期=1000;
受保护静态最终整数最大计数=60;
私有测试;//保存对测试类的引用
私人整数计数;
公共倒计时(测试)
{
this.test=test;//初始化对测试类的引用。
字符串text=“(“+(最大计数-计数)+”)剩余秒数”;
test.settimerLocationText(文本);
}
公开作废开始()
{
新计时器(计时器周期,新ActionListener()
{
@凌驾
已执行的公共无效操作(操作事件e)
{
如果(计数<最大计数)
{
计数++;
字符串text=“(“+(最大计数-计数)+”)剩余秒数”;
test.settimerLocationText(text);//使用对
}
其他的
{
((计时器)e.getSource()).stop();
test.endTimerGUI();
}
}
}).start();
}
}
在这里输入代码
1)为什么要闪烁图像?只需显示图像。2) 获取显示图像的时间。在按钮的动作侦听器中获取时间。减去时间得到反应时间。如果是否定的,则忽略。如果是肯定的,将反应时间以毫秒为单位存储在列表中Sir,这就是我面临的问题。我不知道如何将计时器与按钮的动作侦听器绑定。请提供部分代码供我理解,然后我将制作自己的代码。谢谢你说你不行
,这意味着你已经尝试过了,但失败了。因此,向我们展示失败的codeImageIcon ic=newImageIcon(“time.png”);clickArea=新的JLabel(ic);fr.add(单击区域、边框布局、中心);单击=新建JButton(“单击此处!”);jp2.添加(单击);ActionListener aL=new ActionListener(){public void actionPerformed(ActionEvent ae){if(clickArea!=null){if(!showpic){clickArea.setVisible(false);}clickArea.setVisible(true);showpic=!showpic;}否则{clickArea.setText(“图片不可用”);};定时器=新定时器(600,aL);timer.setRepeats(真);timer.start();不幸的是,当我输入答案时,这个问题被搁置了。你可以找到我输入的代码