*********************关于随机函数,附代码,up有份!************************

sxhv998 2003-08-30 08:43:00
package sxh;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SlidingPuzzle {
public static void main(String[] args) {
PuzzleFrame frame = new PuzzleFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}

class PuzzleFrame extends JFrame {
public PuzzleFrame() {
setTitle("Puzzle");
setSize(WIDTH, HEIGHT);

Container contentPane = getContentPane();
PuzzlePanel panel = new PuzzlePanel();
contentPane.add(panel);
}
public static final int WIDTH = 220;
public static final int HEIGHT = 220;
}

class NewButton extends JButton {
public int num;

public NewButton(String s,int n) {
super(s);
num=n;
}
}

class PuzzlePanel extends JPanel {
JButton[] button = { new NewButton("1",1),new NewButton("2",2),new NewButton("3",3),
new NewButton("4",4),new NewButton("5",5),new NewButton("6",6),
new NewButton("7",7),new NewButton("8",8),new NewButton("9",9),
new NewButton("10",10),new NewButton("11",11),new NewButton("12",12),
new NewButton("13",13),new NewButton("14",14),new NewButton("15",15),
new NewButton("",16)};
private int i;
private JPanel panel;

public PuzzlePanel() {
setLayout(new BorderLayout());

panel = new JPanel();
panel.setLayout(new GridLayout(4,4,1,1));

//String move=new String();
for(int i=0;i<button.length;i++){
panel.add(button[i]);
button[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
NewButton b=(NewButton)e.getSource();
if ((b.num-1-4)>=0) {
if (button[b.num-1-4].getText().equals("")) {
button[b.num-1-4].setText(button[b.num-1].getText());
button[b.num-1].setText("");
}
}; //move=up;
if ((b.num-1-1)>=0) {
if (button[b.num-1-1].getText().equals("")) {
button[b.num-1-1].setText(button[b.num-1].getText());
button[b.num-1].setText("");
}
}; //move=left;
if ((b.num-1+1)<=button.length-1) {
if (button[b.num-1+1].getText().equals("")) {
button[b.num-1+1].setText(button[b.num-1].getText());
button[b.num-1].setText("");
}
}; //move=right;
if ((b.num-1+4)<=button.length-1) {
if (button[b.num-1+4].getText().equals("")) {
button[b.num-1+4].setText(button[b.num-1].getText());
button[b.num-1].setText("");
}
}; //move=down;
}
});
}
add(panel);
}
}


怎样才能随机排列按钮上的数字!
谢谢!
...全文
50 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
rombus 2003-08-31
  • 打赏
  • 举报
回复
up
neuyqy 2003-08-31
  • 打赏
  • 举报
回复
有分,up
stonewang 2003-08-31
  • 打赏
  • 举报
回复
有分,up
wangyanqiu 2003-08-31
  • 打赏
  • 举报
回复


帮你up
sxhv998 2003-08-31
  • 打赏
  • 举报
回复
up
noratong 2003-08-31
  • 打赏
  • 举报
回复
忘记说了,传入后还要调用里面的randomstor()方法,返回的就是这些数中的一个数.不过我这随机数的产生,是指从0到某个数的随机顺序,不能特指一些数的随机顺序.不过那也好做.只要改一下我的原代码就可以了(我原来想改的,可一直忘了).如果看不懂原代码,最好找一本讲了collecion(集合对象)的书来看一下,就很好懂了.
noratong 2003-08-31
  • 打赏
  • 举报
回复
哈哈,你算问到点子上了,我原来也为这个问题苦脑了很久,一开始用的方法很麻烦,后来我自己想出了一个简单的方法.代码如下(你只要调用我这个包里的类,实例化时传入你要随机排序的数的个数就可以了:
package MyRandomStor;

import java.util.*;

public class RandomStor
{
ArrayList RandomNumber = new ArrayList(10);

public RandomStor (int num)
{
for (int i=1; i<=num; i++ )
{
RandomNumber.add(new Integer(i));
}
}

public int get(int j)
{
Integer i = (Integer)RandomNumber.get(j);
return i.intValue();
}

public void randomstor()
{
Collections.shuffle(RandomNumber);
}
/* public static void main(String[] args)
{
RandomStor r = new RandomStor(14);
r.randomstor();
for (int k=0; k<14; k++)
{
System.out.println(r.get(k));
}
} */
}
如有问题的话,跟我联系:nora_tong@sina.com.cn.
OK???
kongxiangfa 2003-08-31
  • 打赏
  • 举报
回复
(int)(Math.random()*15+1),这个表达式可以帮你取到你要的随机数,试试吧
xinxinduyu 2003-08-31
  • 打赏
  • 举报
回复
代码比较规范,只是地方太小了,自动换行了,看了一下,研究研究
liwei55555 2003-08-31
  • 打赏
  • 举报
回复
你的代码可否规范一点
Abib 2003-08-31
  • 打赏
  • 举报
回复
什么嘛
Abib 2003-08-30
  • 打赏
  • 举报
回复
生成随机数组,然后在构造函数里头依序取数初始化
JavaBoyCaoJi 2003-08-30
  • 打赏
  • 举报
回复
up
JAMES001 2003-08-30
  • 打赏
  • 举报
回复
up
cool_li 2003-08-30
  • 打赏
  • 举报
回复
up
sxhv998 2003-08-30
  • 打赏
  • 举报
回复
没人回答吗?
100分呐!

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧