2011年4月22日 星期五

04/22 BINGO!

首先到 http://tccnchsu.blogspot.com/ 複製第8週程式碼

// with event
//Swing, JButton類別 有ActionListener
//Swing, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
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[10];
static JLabel  labels[]=new JLabel [10];
public static void main(String args[])
{
SwingTestEvent myfrm=new SwingTestEvent();

String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
FlowLayout flow=new FlowLayout();
GridLayout grid12= new GridLayout(1,2);
GridLayout grid33= new GridLayout(3,3);
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 stringValue;
stringValue=tbx1.getText();
int intValue = Integer.parseInt(stringValue);
System.out.println(intValue);
buttons[intValue].setBackground(Color.blue);
labels[intValue].setText(stringValue);


 改成

如圖中紅色框框部分
int rndon=(int)(Math.random()*9); //產生亂數
利用for迴圈省去一個個改數字的麻煩

利用tmp來產生亂數(將tmp視為一個暫時的位子)

String tmp;//宣告tmp
tmp=numbers[8];//第8個數字先坐在tmp→第8個位子是空的
numbers[8]=numbers[rndon];//產生的亂數坐到第8個位子→第8個位子被填滿,亂數的位子是空的
numbers[rndon]=tmp;//tmp位子數字(剛剛的第8個數字)坐到亂數的位子
利用不斷產生亂數,使得每一次每個位子的數字都會不一樣


因為我們的目的是產生5X5個亂數
所以記得將這些地方修改掉

最後程式碼變為



// 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(900,600);
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 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;
String tmp;
//stringValue=tbx1.getText();
/*
for (int i = 0; i < 9; i++)
{
int rndno = (int)(Math.random()*9);
System.out.println(rndno);
numbers[i]=String.valueOf(rndno);
buttons[i].setLabel(numbers[i]);
}
*/
for (int i = 0; i <25; i++)
{
int y=24-i;
int rndno = (int)(Math.random()*(y+1));
System.out.println(rndno);
tmp=numbers[y];
numbers[y]=numbers[rndno];
numbers[rndno]=tmp;
}

for (int i = 0; i < 25; i++)
{
System.out.println(numbers[i]);
buttons[i].setLabel(numbers[i]);
}

//tbx1.setText(numbers[0]);
//buttons[rndno].setBackground(Color.pink);
//labels[rndno].setText(numbers[0]);
}
}


注意


for (int i = 0; i < 25; i++)
{
System.out.println( numbers[i]);
buttons[i].setLabel(numbers[i]);
}

是為了能夠看到產生的亂數



在60個數字裡產生25個亂數

只要改掉一些地方即可 25→60

// 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[]=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(900,600);
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 numbers[]=new String[61];
for (int i = 0; i <60; i++)
{
numbers[i]=String.valueOf(i);
}

String stringValue;
String tmp;
//stringValue=tbx1.getText();

System.out.println(tbx1.getText());

for (int i = 0; i <61; i++)
{
int y=60-i;
int rndno = (int)(Math.random()*(y+1));
System.out.println(rndno);
tmp=numbers[y];
numbers[y]=numbers[rndno];
numbers[rndno]=tmp;
}


for (int i = 0; i < 25; i++)
{
System.out.println(numbers[i]);
buttons[i].setLabel(numbers[i]);
}


//tbx1.setText(numbers[0]);
//buttons[rndno].setBackground(Color.pink);
//labels[rndno].setText(numbers[0]);

}
}


沒有留言:

張貼留言