代码如下,做的粗糙了些,不好意思啊,没做过游戏。
我想问的是,我生成了一个可执行的.jar文件,里面也包含了需要加载的图片,可是如果文件夹里面如果没有图片那么在运行过程中图片就不显示,为什么呢?
我把文件打包大家自己运行以下就知道了,想要运行成功的话图片(liandluo.gif)就必须和程序放在一个目录下,而放在jar包里就不行,想不通。。。。。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Fanying extends JFrame implements MouseListener,ActionListener
{
public static void main(String[] args)
{
Fanying f = new Fanying();
}
Timer timer;
int score;
JLabel img[] = new JLabel[4];
JLabel lb1;
public Fanying()
{
super("看看你的敏捷度!");
Container c = getContentPane();
c.setLayout(null);
lb1 = new JLabel("点击下面的图片!!");
lb1.setFont(new Font("仿宋",Font.BOLD,14));
lb1.setForeground(Color.red);
lb1.setSize(190,40);
c.add(lb1);
ImageIcon icon1 = new ImageIcon("liandluo.gif");
for(int i=0; i<4; i++)
{
img[i] = new JLabel(icon1);
img[i].setSize(80,80);
img[i].setLocation((50 + i*106),150);
img[i].setVisible(true);
img[i].addMouseListener(this);
c.add(img[i]);
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,400);
setVisible(true);
score = 0;
timer = new Timer(200,this);
timer.start();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == timer)
{
int i = 0;
i = (int)(Math.random()*4);
img[i].setVisible(true);
//img[i].setVisible(false);
}
if(e.getSource() == timer)
{
int j = 0;
j = (int)(Math.random()*4);
img[j].setVisible(false);
}
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e)
{
for(int i=0; i<4; i++)
{
if(e.getSource() == img[i])
{
score = score + 1;
lb1.setText("你点到了 " + score + " 下!!");
img[i].setVisible(false);
//int k;
//k = (int)(Math.random()*4);
//img[k].setVisible(true);
}
}
}
}