2011-03-24

第六堂課

一開始,先去老師的部落格

程式設計工藝大師(Master of Computer Programming)

尋找程式碼↓



儲存後並執行,會出現一個沒有功能的button↓



加上兩個button↓



要設定按鈕的方向按鈕才不會重疊↓

myfrm.add(btn1,border.EAST);
 myfrm.add(btn2,border.WEST);

按鈕的BorderLayout可以參考

Java 2 Platform SE v1.3.1: Class BorderLayout 



接下來我們要加入一個文字方塊↓

 static TextField tbx1=new TextField("TestField 1");
myfrm.add(tbx1,border.CENTER);

文字方塊的加入方法可參考

TextField (Java 2 Platform SE v1.4.2)


接下來,我們要慢慢的讓按鈕有功能囉!

加入
import java.awt.event.*;套件
因為要實行public void actionPerformed(ActionEvent e) //加在最後↓


然後試著加入
static AwtTest  myfrm=new AwtTest ("Button class");
還有
AwtTest  myfrm=new AwtTest();


最後發現,使用AwtTest myfrm=new AwtTest() 才是可行的

static AwtTest myfrm=new AwtTest ("Button class");則會出現錯誤

成功加入了新的frame

可是我們要讓button可以聽到命令
所以還必須加入btn1.addActionListener(myfrm);



最後,在public void actionPerformed(ActionEvent e) { }裡輸入
int rn;
rn=(int) (Math.random()*49) ; 
System.out.println(rn );


結果跑出來之後,按一下button1(因為我們只讓這個鍵有功能),就可以跑出隨機亂數囉~
如下↓




今天的作業呢!就是要像大樂透一樣,按一下,登登!!
就可以出現7個不同的數字在文字方塊裡

首先呢!Layout的方法不能再使用BorderLayout了,
因為BorderLayout只有五個方位,不管你再怎麼用也只有5個方位,
7個文字方塊怎麼擠的下,所以我們要使用的Layout方法是FlowLayout呦!!

最後,終於給我亂try try 出來了,雖然程式碼「樂樂ㄉㄥˊ」
不過總算是完成作業囉XD

請參考以下程式碼(不一定對,但勉強可參考^^)

// AWT, Button類別
import java.awt.*;
import java.awt.event.*;
public class AwtTest  extends Frame implements ActionListener 
{
// static Frame myfrm=new Frame("Button class");
//static AwtTest  myfrm=new AwtTest ("Button class");
static Button btn1=new Button("Button1"); // 建立按鈕物件1
//static Button btn2=new Button("Button2"); // 建立按鈕物件2

static TextField tbx1=new TextField("1"); // 建立文字方塊1
static TextField tbx2=new TextField(" 2"); // 建立文字方塊2
static TextField tbx3=new TextField("3"); // 建立文字方塊3
static TextField tbx4=new TextField(" 4"); // 建立文字方塊4
static TextField tbx5=new TextField(" 5"); // 建立文字方塊5
static TextField tbx6=new TextField(" 6"); // 建立文字方塊6
static TextField tbx7=new TextField(" 7"); // 建立文字方塊7
public static void main(String args[])
 {
AwtTest  myfrm=new AwtTest();
//BorderLayout border=new BorderLayout();
FlowLayout flow=new FlowLayout();
myfrm.setLayout(flow);
myfrm.setSize(200,150);
btn1.addActionListener(myfrm);

myfrm.add(btn1); // 在視窗內加入按鈕1
//myfrm.add(btn2,border.WEST); // 在視窗內加入按鈕2
myfrm.add(tbx1); // 在視窗內加入文字方塊1
myfrm.add(tbx2); // 在視窗內加入文字方塊2
myfrm.add(tbx3); // 在視窗內加入文字方塊3
myfrm.add(tbx4); // 在視窗內加入文字方塊4
myfrm.add(tbx5); // 在視窗內加入文字方塊5
myfrm.add(tbx6); // 在視窗內加入文字方塊6
myfrm.add(tbx7); // 在視窗內加入文字方塊7
myfrm.setVisible(true);     
}



public void actionPerformed(ActionEvent e) 
int rn1,rn2,rn3,rn4,rn5,rn6,rn7;
rn1=(int) (Math.random()*49) ; 
rn2=(int) (Math.random()*49) ; 
rn3=(int) (Math.random()*49) ; 
rn4=(int) (Math.random()*49) ; 
rn5=(int) (Math.random()*49) ; 
rn6=(int) (Math.random()*49) ; 
rn7=(int) (Math.random()*49) ; 

String stringValue1=Integer.toString(rn1);
String stringValue2=Integer.toString(rn2);
String stringValue3=Integer.toString(rn3);
String stringValue4=Integer.toString(rn4);
String stringValue5=Integer.toString(rn5);
String stringValue6=Integer.toString(rn6);
String stringValue7=Integer.toString(rn7);

System.out.println(rn1);
System.out.println(rn2);
System.out.println(rn3);
System.out.println(rn4);
System.out.println(rn5);
System.out.println(rn6);
System.out.println(rn7);

tbx1.setText(stringValue1 );
tbx2.setText(stringValue2);
tbx3.setText(stringValue3 );
tbx4.setText(stringValue4 );
tbx5.setText(stringValue5 );
tbx6.setText(stringValue6 );
tbx7.setText(stringValue7 );
}

}


喔!忘了說,出來的數字還是可能有重複喔!

沒有留言:

張貼留言