产生不重复随机数,写得不好,希望大家多多批评…

2008-02-23 09:25:01来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

小弟初学Java,最近在写一个拼图游戏的程序,在写得过程中考虑要随机排列图片,经过一番研究终于学出来了,我写得这个不是很好,在高手看来可能算不了什么,但是对于我这样初学的人我想还会有一些帮助的,所以就把它写出来了!!!

代码如下:

package PT;
import java.awt.event.*;
import javax.Swing.*;
import javax.swing.Icon;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
class myPanel extends JPanel implements ActionListener
{
JButton bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9;
ImageIcon ico1=new ImageIcon("1.jpg");
ImageIcon ico2=new ImageIcon("2.jpg");
ImageIcon ico3=new ImageIcon("3.jpg");
ImageIcon ico4=new ImageIcon("4.jpg");
ImageIcon ico5=new ImageIcon("5.jpg");
ImageIcon ico6=new ImageIcon("6.jpg");
ImageIcon ico7=new ImageIcon("7.jpg");
ImageIcon ico8=new ImageIcon("8.jpg");
ImageIcon[] ico=new ImageIcon[8];
public myPanel()
{
int[] a=new int[8];
for(int i=0;i<a.length;i )
{
a[i]=(int)(Math.random()*8 1);
for(int j=0;j<i;j )
{
if(a[j]==a[i])
{
i--;
break;
}
}
}

for(int i=0;i<a.length;i )
{
ico[i]=new ImageIcon(a[i] ".jpg");
// System.out.println (a[i]);

}
this.setLayout(new GridLayout(3,3));

bt1=new JButton(ico[0]);
bt2=new JButton(ico[1]);
bt3=new JButton(ico[2]);
bt4=new JButton(ico[3]);
bt5=new JButton(ico[4]);
bt6=new JButton(ico[5]);
bt7=new JButton(ico[6]);
bt8=new JButton(ico[7]);
bt9=new JButton();

bt1.setSize(200,200);
bt2.setSize(200,200);
bt3.setSize(200,200);
bt4.setSize(200,200);
bt5.setSize(200,200);
bt6.setSize(200,200);
bt7.setSize(200,200);
bt8.setSize(200,200);
bt9.setSize(200,200);
this.add(bt1);
this.add(bt2);
this.add(bt3);
this.add(bt4);
this.add(bt5);
this.add(bt6);
this.add(bt7);
this.add(bt8);
this.add(bt9);
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt4.addActionListener(this);
bt5.addActionListener(this);
bt6.addActionListener(this);
bt7.addActionListener(this);
bt8.addActionListener(this);
bt9.addActionListener(this);

}

public void actionPerformed(ActionEvent e)
{
if (e.getSource()==bt1)
{
if(bt2.getIcon()==null)
{
bt2.setIcon(bt1.getIcon());
bt1.setIcon(null);
}
if(bt4.getIcon()==null)
{
bt4.setIcon(bt1.getIcon());
bt1.setIcon(null);
}
}
if(e.getSource()==bt2)
{
if(bt1.getIcon()==null)
{
bt1.setIcon(bt2.getIcon());
bt2.setIcon(null);
}
if(bt3.getIcon()==null)
{
bt3.setIcon(bt2.getIcon());
bt2.setIcon(null);
}
if(bt5.getIcon()==null)
{
bt5.setIcon(bt2.getIcon());
bt2.setIcon(null);
}
}
if(e.getSource()==bt3)
{
if(bt2.getIcon()==null)
{
bt2.setIcon(bt3.getIcon());
bt3.setIcon(null);
}
if(bt6.getIcon()==null)
{
bt6.setIcon(bt3.getIcon());
bt3.setIcon(null);
}
}
if(e.getSource()==bt4)
{
if(bt1.getIcon()==null)
{
bt1.setIcon(bt4.getIcon());
bt4.setIcon(null);
}
if(bt5.getIcon()==null)
{
bt5.setIcon(bt4.getIcon());
bt4.setIcon(null);
}
if(bt7.getIcon()==null)
{
bt7.setIcon(bt4.getIcon());
bt4.setIcon(null);
}
}

if(e.getSource()==bt5)
{
if(bt2.getIcon()==null)
{
bt2.setIcon(bt5.getIcon());
bt5.setIcon(null);
}
if(bt4.getIcon()==null)
{
bt4.setIcon(bt5.getIcon());
bt5.setIcon(null);
}
if(bt6.getIcon()==null)
{
bt6.setIcon(bt5.getIcon());
bt5.setIcon(null);
}
if(bt8.getIcon()==null)
{
bt8.setIcon(bt5.getIcon());
bt5.setIcon(null);
}
}
if(e.getSource()==bt6)
{
if(bt3.getIcon()==null)
{
bt3.setIcon(bt6.getIcon());
bt6.setIcon(null);
}
if(bt5.getIcon()==null)
{
bt5.setIcon(bt6.getIcon());
bt6.setIcon(null);
}
if(bt9.getIcon()==null)
{
bt9.setIcon(bt6.getIcon());
bt6.setIcon(null);
}
}
if(e.getSource()==bt7)
{
if(bt4.getIcon()==null)
{
bt4.setIcon(bt7.getIcon());
bt7.setIcon(null);
}
if(bt8.getIcon()==null)
{
bt8.setIcon(bt7.getIcon());

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:使用links方式安装Eclipse插件

下一篇:用java做的滚动的正弦曲线