int rndon
rndon=(int)(Math.random()*9);
System.out.println( rndon);
numbers[0]=String.valueOf(rndon);
buttons[0].setLabel(numbers[0]);
rndon=(int)(Math.random()*9);
System.out.println( rndon);
numbers[1]=String.valueOf(rndon);
buttons[1].setLabel(numbers[1]);
rndon=(int)(Math.random()*9);
System.out.println( rndon);
numbers[2]=String.valueOf(rndon);
buttons[2].setLabel(numbers[2]);
如果用for迴圈就可以不用如此麻煩了
for (int i = 0; i < 9; i++)
{
rndon=(int)(Math.random()*9);
System.out.println( rndon);
numbers[i]=String.valueOf(rndon);
buttons[i].setLabel(numbers[i]);
}
for{要執行的程式}
今天我們要做的是產生5*5亂數
(類似玩bingo遊戲用的)
所以我們要幫數字換位子
ex:
0 1 2 3 4 5 6 7 8
產生亂數2
擷取第2個抽屜裡的數字[2]換到最後一個
變成
0 1 8 3 4 5 6 7 2
再產生亂數2
擷取第二個抽屜裡面的數字[8]
變成
0 1 7 3 4 5 6 8 2
換過的數字就不動了
所以我們必須製造第三個位子給數字暫時坐著
tmp就是暫時的位子
加入
String tmp;
tmp=numbers[8];
//tmp給第八個抽屜裡的數字坐
numbers[8]=numbers[rndon];
//random產生的第rndon個抽屜裡的數字坐到第八個抽屜
numbers[rndon]=tmp;
//tmp裡的抽屜再坐到rndon裡
最後的程式碼↓
// with event
//Swing, JButton類別 有ActionListener
//Swing, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
public class SwingTestEvent extends JFrame implements ActionListener
{
//static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
//static AwtTestEvent myfrm=new AwtTestEvent("JFrame 1 "); // Java Class JFrame
static JTextField tbx1=new JTextField(2); // 建立1文字方塊物件
static JButton buttons[]=new JButton[25];
static JLabel labels[]=new JLabel [25];
public static void main(String args[])
{
SwingTestEvent myfrm=new SwingTestEvent();
String numbers[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24"};
FlowLayout flow=new FlowLayout();
GridLayout grid12= new GridLayout(1,2);
GridLayout grid33= new GridLayout(5,5);
myfrm.setLayout(grid12);
myfrm.setSize(450,450);
JPanel p1 = new JPanel(grid33); //實作 panel 1
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
p1.add(buttons[i], grid33); // 在 panel 1內加入按鈕陣列
}
myfrm.add(p1); // 在視窗myfrm 內加入 panel 1
JPanel p3 = new JPanel(grid33); //實作 panel 3
for (int i = 0; i < numbers.length; i++)
{
labels[i] = new JLabel(); // create labels
p3.add(labels[i], grid33); // 在 panel 1內加入按鈕陣列
}
myfrm.add(p3); // 在視窗myfrm 內加入 panel 3
JPanel p2 = new JPanel(flow); //實作 panel 2
JButton btn1=new JButton("JButton 1"); // 建立按鈕物件 btn1
btn1.addActionListener(myfrm);
p2.add(tbx1); // 在 panel 2內加入文字方塊
p2.add(btn1); // 在 panel 2內加入按鈕
myfrm.add(p2); // 在視窗myfrm 內加入 panel 2
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
//String stringValue;
//stringValue=tbx1.getText();
//int intValue = Integer.parseInt(stringValue);
//System.out.println(intValue);
//buttons[intValue].setBackground(Color.blue);
//labels[intValue].setText(stringValue);
String numbers[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24"};
String Stringvalue;
int rndon;
String tmp;
for (int i = 0; i < 25; i++)
{
int j=24-i;
rndon=(int)(Math.random()*(j+1));
System.out.println( rndon);
tmp=numbers[j];
numbers[j]=numbers[rndon];
numbers[rndon]=tmp;
}
for (int i = 0; i < 25; i++)
{
System.out.println( numbers[i]);
buttons[i].setLabel(numbers[i]);
}
//buttons[rndon].setBackground(Color.blue);
//tbx1.setText(numbers[0]);
//labels[rndon].setText(numbers[0]);
}
}
※ 記得要改
static JButton buttons[]=new JButton[25];
static JLabel labels[]=new JLabel [25];
這樣才有25個空格可以裝得下
※ String numbers[] = {"0", "1", "2", "3", "4", "5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24"};
25個抽屜
※ GridLayout grid33= new GridLayout(5,5);用5*5的
GridLayout
※ 加入import java.lang.*setLabel才有辦法執行
※ j=24-i的原因是產生的亂數不能一直介在1~25之間,要逐一減少
※ (Math.random()*(j+1))的原因是i<25
※ 寫出for (int i = 0; i < 25; i++)
{
System.out.println( numbers[i]);
buttons[i].setLabel(numbers[i]);
}
的原因是要知道它變化的情況,
不然它會直接在系統裡面變化,我們看不到
結果圖↓
進階!!
就是要在60之內去25個數字放進去空格裡
為了省去要打到60個麻煩
加入
String numbers[]=new String[61];
for (int i = 0; i <60; i++)
{
numbers[i]=String.valueOf(i);
}
Button數跟Label以及GridLayout都維持不變
加入
String tmp;
for (int i = 0; i < 61 ; i++)
{
int j= 60 -i;
rndon=(int)(Math.random()*(j+1));
System.out.println( rndon);
tmp=numbers[j];
numbers[j]=numbers[rndon];
numbers[rndon]=tmp;
}
for (int i = 0; i <25; i++)
{
System.out.println( numbers[i]);
buttons[i].setLabel(numbers[i]);
}
完成!!
程式碼如下
// with event
//Swing, JButton類別 有ActionListener
//Swing, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
public class SwingTestEvent extends JFrame implements ActionListener
{
//static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
//static AwtTestEvent myfrm=new AwtTestEvent("JFrame 1 "); // Java Class JFrame
static JTextField tbx1=new JTextField(2); // 建立1文字方塊物件
static JButton buttons[]=new JButton[25];
static JLabel labels[]=new JLabel [25];
public static void main(String args[])
{
SwingTestEvent myfrm=new SwingTestEvent();
//String numbers[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24"};
String numbers[]=new String[61];
for (int i = 0; i <60; i++)
{
numbers[i]=String.valueOf(i);
}
FlowLayout flow=new FlowLayout();
GridLayout grid12= new GridLayout(1,2);
GridLayout grid33= new GridLayout(5,5);
myfrm.setLayout(grid12);
myfrm.setSize(450,450);
JPanel p1 = new JPanel(grid33); //實作 panel 1
for (int i = 0; i < 25; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
p1.add(buttons[i], grid33); // 在 panel 1內加入按鈕陣列
}
myfrm.add(p1); // 在視窗myfrm 內加入 panel 1
JPanel p3 = new JPanel(grid33); //實作 panel 3
for (int i = 0; i <25; i++)
{
labels[i] = new JLabel(); // create labels
p3.add(labels[i], grid33); // 在 panel 1內加入按鈕陣列
}
myfrm.add(p3); // 在視窗myfrm 內加入 panel 3
JPanel p2 = new JPanel(flow); //實作 panel 2
JButton btn1=new JButton("JButton 1"); // 建立按鈕物件 btn1
btn1.addActionListener(myfrm);
p2.add(tbx1); // 在 panel 2內加入文字方塊
p2.add(btn1); // 在 panel 2內加入按鈕
myfrm.add(p2); // 在視窗myfrm 內加入 panel 2
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
//String stringValue;
//stringValue=tbx1.getText();
//int intValue = Integer.parseInt(stringValue);
//System.out.println(intValue);
//buttons[intValue].setBackground(Color.blue);
//labels[intValue].setText(stringValue);
//String numbers[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24"};
String numbers[]=new String[61];
for (int i = 0; i <60; i++)
{
numbers[i]=String.valueOf(i);
}
String Stringvalue;
int rndon;
/*for (int i = 0; i < 9; i++)
{
rndon=(int)(Math.random()*9);
System.out.println( rndon);
numbers[i]=String.valueOf(rndon);
buttons[i].setLabel(numbers[i]);
}
*/
String tmp;
for (int i = 0; i < 61 ; i++)
{
int j= 60 -i;
rndon=(int)(Math.random()*(j+1));
System.out.println( rndon);
tmp=numbers[j];
numbers[j]=numbers[rndon];
numbers[rndon]=tmp;
}
for (int i = 0; i <25; i++)
{
System.out.println( numbers[i]);
buttons[i].setLabel(numbers[i]);
}
//buttons[rndon].setBackground(Color.blue);
//tbx1.setText(numbers[0]);
//labels[rndon].setText(numbers[0]);
}
}