2011-04-29

第十堂課

一開始,先去  http://ccckmit.wikidot.com/ja:encapsulation  複製封裝的程式碼,藉此了解何謂是封裝


打開文件檔,存成java的形式↓




然後把class Person1剪下,存成另一個檔案↓


修改一下

在Object1加入 
Person1 p3 = new Person1();
p3.checkWeight();

在Person1中加入
    Person1() 
      {      name   = "宜靜";
                  weight = 42;         }

這樣子程式碼找不到條件時就會在
 Person1() 中找


結果↓


接著去老師的Blogger複製程式碼

// with event
//Swing, JButton類別 有ActionListener
//Swing, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingEightPuzzleEvent 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 SwingEightPuzzleEvent()
{}//建構子

public static void main(String args[])
{
SwingEightPuzzleEvent myfrm=new SwingEightPuzzleEvent();
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 numbers[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
//int rndno;
//String stringValue;
//stringValue=tbx1.getText();
//int intValue = Integer.parseInt(stringValue);
//System.out.println(intValue);
//labels[intValue].setText(stringValue);


/*
for (int j = 0; j < 9; j++)
{
//rndno=(int) (Math.random()*9);
//System.out.println(rndno);
//numbers[i]=String.valueOf(rndno); 
//buttons[i].setLabel(numbers[i]); 
}
*/


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



開始幫他瘦身囉~
把一些可以移到建構子的程式移進去建構子

public SwingEightPuzzleEvent()
{}//建構子
如果建構子跟class main 都會用到的程式就移到最上面去
這樣就可以兩個都包含在內了

整理過後的程式↓

// with event
//Swing, JButton類別 有ActionListener
//Swing, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingEightPuzzleEvent 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];
static FlowLayout flow=new FlowLayout();
static GridLayout grid33= new GridLayout(3,3);

static JButton btn1=new JButton("JButton 1"); // 建立按鈕物件 btn1

static  JPanel p1 = new JPanel(grid33); //實作  panel 1
static JPanel p2 = new JPanel(flow); //實作  panel 2
static JPanel p3 = new JPanel(grid33); //實作  panel 3

static  String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};

static GridLayout grid12= new GridLayout(1,2);

public SwingEightPuzzleEvent()
{


for (int i = 0; i < numbers.length; i++)
{
labels[i] = new JLabel(); // create labels
p3.add(labels[i], 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內加入按鈕陣列
}

p2.add(tbx1); // 在 panel 2內加入文字方塊
p2.add(btn1); // 在 panel 2內加入按鈕
}

public static void main(String args[])
{
SwingEightPuzzleEvent myfrm=new SwingEightPuzzleEvent();

myfrm.setLayout(grid12);
myfrm.setSize(450,450);

myfrm.add(p1); // 在視窗myfrm 內加入 panel 1
myfrm.add(p3); // 在視窗myfrm 內加入 panel 3



btn1.addActionListener(myfrm);


myfrm.add(p2); // 在視窗myfrm 內加入 panel 2

myfrm.setVisible(true);
}




今天的作業就是去http://www.rgagnon.com/javadetails/java-0144.html複製程式碼
分別執行套件的運用
就完成囉~






2011-04-24

第九堂課

首先,先去老師的部落格複製程式碼


// 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);*/

加入
String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};//九個抽屜
int rndon=(int)(Math.random()*9);//產生9個亂數
System.out.println( rndon);
numbers[0]=String.valueOf(rndon);

buttons[rndon].setBackground(Color.blue);//輸入哪個數字那個數字就會變藍色
tbx1.setText(numbers[0]);
labels[rndon].setText(numbers[0]);//改變標籤上的字

在public void actionPerformed(ActionEvent e)裡



如果要擷取三個抽屜裡的數字,我們就必須複製三次,以此類推
ex:
String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8"}
String Stringvalue;

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]);

}
}