2011年3月25日 星期五

java - 隨機選號2




1.建立按鈕

// AWT, Button類別

import java.awt.*;
public class AwtTest
{
static Frame frm=new Frame("Button class");
static Button btn=new Button("Push Me!!"); // 建立按鈕物件
public static void main(String args[])
{
frm.setSize(200,150);
frm.add(btn); // 在視窗內加入按鈕
frm.setVisible(true);
}
}





2.再來是建立多個按鈕


// AWT, Button類別

import java.awt.*;
public class AwtTest
{
static Frame myfrm=new Frame("Button class");
static Button btn1=new Button("Button 1"); // 建立按鈕物件
static Button btn2=new Button("Button 2");
static Button btn3=new Button("Button 3");
static Button btn4=new Button("Button 4");
public static void main(String args[])
{
BorderLayout border=new BorderLayout();
myfrm.setLayout(border);

myfrm.setSize(200,150);
myfrm.add(btn1); // 在視窗內加入按鈕
myfrm.add(btn2);
myfrm.add(btn3);
myfrm.add(btn4);

myfrm.setVisible(true);
}
}






3.分開配置按鈕


// AWT, Button類別

import java.awt.*;
public class AwtTest
{
static Frame myfrm=new Frame("Button class");
static Button btn1=new Button("Button 1"); // 建立按鈕物件
static Button btn2=new Button("Button 2");
static Button btn3=new Button("Button 3");
static Button btn4=new Button("Button 4");
public static void main(String args[])
{
BorderLayout border=new BorderLayout();
myfrm.setLayout(border);

myfrm.setSize(200,150);
myfrm.add(btn1, border.WEST); // 在視窗內加入按鈕
myfrm.add(btn2, border.EAST);
myfrm.add(btn3, border.NORTH);
myfrm.add(btn4, border.SOUTH);

myfrm.setVisible(true);
}
}



4.建立一個文件


// AWT, Button類別

import java.awt.*;
public class AwtTest
{
static Frame myfrm=new Frame("Button class");
static Button btn1=new Button("Button 1"); // 建立按鈕1物件
static Button btn2=new Button("Button 2"); // 建立按鈕2物件
static Button btn3=new Button("Button 3"); // 建立按鈕3物件
static Button btn4=new Button("Button 4");// 建立按鈕4物件
static TextField txf1=new TextField("JAVA 學習!!");// 建立文字1物件
public static void main(String args[])
{
BorderLayout border=new BorderLayout();
myfrm.setLayout(border);

myfrm.setSize(300,150);
myfrm.add(btn1, border.WEST); // 在視窗內加入按鈕1
myfrm.add(btn2, border.EAST); // 在視窗內加入按鈕2
myfrm.add(btn3, border.NORTH); // 在視窗內加入按鈕3
myfrm.add(btn4, border.SOUTH); // 在視窗內加入按鈕4
myfrm.add(txf1, border.CENTER); // 在視窗內加入文件1


myfrm.setVisible(true);
}
}




5.使按鍵可以印出數字


// AWT, Button類別

import java.awt.event.*;
import java.awt.*;
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("Button 1"); // 建立按鈕1物件
static Button btn2=new Button("Button 2"); // 建立按鈕2物件
static Button btn3=new Button("Button 3"); // 建立按鈕3物件
static Button btn4=new Button("Button 4");// 建立按鈕4物件
static TextField txf1=new TextField("JAVA 學習!!");// 建立文字1物件
public static void main(String args[])
{
AwtTest myfrm=new AwtTest();
BorderLayout border=new BorderLayout();
myfrm.setLayout(border);
btn1.addActionListener(myfrm);
myfrm.setSize(300,150);
myfrm.add(btn1, border.WEST); // 在視窗內加入按鈕1
myfrm.add(btn2, border.EAST); // 在視窗內加入按鈕2
myfrm.add(btn3, border.NORTH); // 在視窗內加入按鈕3
myfrm.add(btn4, border.SOUTH); // 在視窗內加入按鈕4
myfrm.add(txf1, border.CENTER); // 在視窗內加入文件1
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int rn;
rn=(int) (Math.random()*49) ;
System.out.println(rn );
}
}



6.將印出的數字也印在文件方格上


// AWT, Button類別

import java.awt.event.*;
import java.awt.*;
public class AwtTest extends Frame implements ActionListener
{
//static Frame myfrm=new Frame("Button class");
static AwtTest myfrm=new AwtTest();

static Button btn1=new Button("Button 1"); // 建立按鈕1物件
static Button btn2=new Button("Button 2"); // 建立按鈕2物件
static Button btn3=new Button("Button 3"); // 建立按鈕3物件
static Button btn4=new Button("Button 4");// 建立按鈕4物件
static TextField txf1=new TextField("JAVA 學習!!");// 建立文字1物件
public static void main(String args[])
{
//AwtTest myfrm=new AwtTest();
BorderLayout border=new BorderLayout();
myfrm.setLayout(border);
btn1.addActionListener(myfrm);
myfrm.setSize(300,150);
myfrm.add(btn1, border.WEST); // 在視窗內加入按鈕1
myfrm.add(btn2, border.EAST); // 在視窗內加入按鈕2
myfrm.add(btn3, border.NORTH); // 在視窗內加入按鈕3
myfrm.add(btn4, border.SOUTH); // 在視窗內加入按鈕4
myfrm.add(txf1, border.CENTER); // 在視窗內加入文件1
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int rn;
rn=(int) (Math.random()*49) ;
String stringValue = Integer.toString(rn);
System.out.println(rn );
txf1.setText(stringValue);

}
}







作業 : 在文字方格上一次顯示7個數字


// AWT, Button類別

import java.awt.event.*;
import java.awt.*;
public class AwtTest extends Frame implements ActionListener
{
//static Frame myfrm=new Frame("Button class");
static AwtTest myfrm=new AwtTest();

static Button btn1=new Button("隨機選號"); // 建立按鈕1物件
static TextField txf1=new TextField("Sample Ramdon1");// 建立文字1物件
static TextField txf2=new TextField("Sample Ramdon2");
static TextField txf3=new TextField("Sample Ramdon3");
static TextField txf4=new TextField("Sample Ramdon4");
static TextField txf5=new TextField("Sample Ramdon5");
static TextField txf6=new TextField("Sample Ramdon6");
static TextField txf7=new TextField("Sample Ramdon7");
public static void main(String args[])
{
//AwtTest myfrm=new AwtTest();
FlowLayout flow=new FlowLayout();
myfrm.setLayout(new FlowLayout());
btn1.addActionListener(myfrm);
myfrm.setSize(300,150);
myfrm.add(btn1); // 在視窗內加入按鈕1
myfrm.add(txf1); // 在視窗內加入文件1
myfrm.add(txf2);
myfrm.add(txf3);
myfrm.add(txf4);
myfrm.add(txf5);
myfrm.add(txf6);
myfrm.add(txf7);
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int rn,rn1,rn2,rn3,rn4,rn5,rn6,rn7;
rn1=(int) (Math.random()*49+1) ;
rn2=(int) (Math.random()*49+1) ;
rn3=(int) (Math.random()*49+1) ;
rn4=(int) (Math.random()*49+1) ;
rn5=(int) (Math.random()*49+1) ;
rn6=(int) (Math.random()*49+1) ;
rn7=(int) (Math.random()*49+1) ;

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

txf1.setText(stringValue1);
txf2.setText(stringValue2);
txf3.setText(stringValue3);
txf4.setText(stringValue4);
txf5.setText(stringValue5);
txf6.setText(stringValue6);
txf7.setText(stringValue7);

}
}



加分題 : 

// AWT, Button類別

import java.awt.event.*;
import java.awt.*;
public class AwtTest extends Frame implements ActionListener 
{
//static Frame myfrm=new Frame("Button class");
static AwtTest myfrm=new AwtTest();

static Button btn1=new Button("隨機選號"); // 建立按鈕1物件
static TextField txf1=new TextField("Sample Ramdon1");// 建立文字1物件
static TextField txf2=new TextField("Sample Ramdon2");
static TextField txf3=new TextField("Sample Ramdon3");
static TextField txf4=new TextField("Sample Ramdon4");
static TextField txf5=new TextField("Sample Ramdon5");
static TextField txf6=new TextField("Sample Ramdon6");
static TextField txf7=new TextField("Sample Ramdon7");
public static void main(String args[])
{
//AwtTest myfrm=new AwtTest();
FlowLayout flow=new FlowLayout();
myfrm.setLayout(new FlowLayout());
btn1.addActionListener(myfrm);
myfrm.setSize(300,150);
myfrm.add(btn1); // 在視窗內加入按鈕1
myfrm.add(txf1); // 在視窗內加入文件1
myfrm.add(txf2); 
myfrm.add(txf3); 
myfrm.add(txf4); 
myfrm.add(txf5);
myfrm.add(txf6); 
myfrm.add(txf7); 
myfrm.setVisible(true); 
}
public void actionPerformed(ActionEvent e) 
int rn,rn1,rn2,rn3,rn4,rn5,rn6,rn7;
rn1=(int) (Math.random()*49+1) ; 
rn2=(int) (Math.random()*49+1) ; 
rn3=(int) (Math.random()*49+1) ; 
rn4=(int) (Math.random()*49+1) ; 
rn5=(int) (Math.random()*49+1) ; 
rn6=(int) (Math.random()*49+1) ; 
rn7=(int) (Math.random()*49+1) ; 

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

txf1.setText(stringValue1);
txf2.setText(stringValue2);
txf3.setText(stringValue3);
txf4.setText(stringValue4);
txf5.setText(stringValue5);
txf6.setText(stringValue6);
txf7.setText(stringValue7);
txf7.setBackground(Color.RED);

}
}










2011年3月18日 星期五

java - 隨機選號1

今天先複習了上一堂課的按鈕


之後又試驗了一個三個按鈕的程式


回到原來的Button繼續編輯

有閃一下就不見的


還有增加一個按鈕


接著就是排列按鈕
setLayout(new FlowLayout());


最後可以讓他跑出一些數字
但只有一邊按鈕是有用的= =



最最後是作業
要一次跑出七個數字


這次一步一步有跟上
還算蠻輕鬆的

2011年3月11日 星期五

java - Button&CheckBox

這次的有點難度
一開始還不覺得






就這麼簡單

但最後好幾的步驟有點複雜
很混亂,所以下一節要再聽懂一點

作業是

2011年3月4日 星期五

java - 加減乘除

今天教了一個有趣的方法
可以加減乘除

一開始還是從選擇路徑
然後再開始寫程式
一開始大致和上一次相同

教了一個指令ISystem.out.println(args[0]);
就可以輸入字元



再來又加上另一指令Integer.parseInt
將字元轉換成整數
既可進行計算



今天教的可以用於1*2及2*1矩陣計算