2011年3月25日 星期五

2011/03/25上課內容

http://tccnchsu.blogspot.com/  程式設計工藝大師

本次上課介紹使用AWT按鈕的範例


建立五個按鈕物件,並在視窗內加入按鈕
執行結果是五個按鈕疊在一起
因為還沒有排列按鈕的位置

首先,利用AWT資料夾裡面的BorderLayout的排序法
此為編排的結果

接下來建立文字方塊物件

建立兩個按鈕,並在中間建立文字方塊物件
替Button 1加入監聽事件
按Button 1會顯示49號碼取一的隨機變數

接下來要讓顯示再命令提示字元的數字改成顯示在文字方塊物件上
但是顯示的方式只能用字串才能執行
所以要將整數轉為字串

在此程式只有按Button 1有動作
顯示結果如下

作業:
建立一個按鈕以及七個文字方塊物件,再按下按鈕後,七個文字方塊能顯示出七個不同的
1~49隨機數字(大樂透)

程式碼:
import java.awt.*;
import java.awt.event.*;
public class AwtTest extends Frame implements ActionListener
{
static Frame myfrm=new Frame("Button class");
static Button btn=new Button("Button");
static TextField tf1=new TextField("TextField");
static TextField tf2=new TextField("TextField");
static TextField tf3=new TextField("TextField");
static TextField tf4=new TextField("TextField");
static TextField tf5=new TextField("TextField");
static TextField tf6=new TextField("TextField");
static TextField tf7=new TextField("TextField");
public static void main(String args[])
{
AwtTest myfrm=new AwtTest();
FlowLayout flow=new FlowLayout();
myfrm.setLayout(flow);
myfrm.setSize(200,150);
myfrm.add(btn,flow);
myfrm.add(tf1,flow);
myfrm.add(tf2,flow);
myfrm.add(tf3,flow);
myfrm.add(tf4,flow);
myfrm.add(tf5,flow);
myfrm.add(tf6,flow);
myfrm.add(tf7,flow);
btn.addActionListener(myfrm);
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int a,b,c,d,f,g,h;
a=(int)(Math.random()*49)+1;
b=(int)(Math.random()*49)+1;
c=(int)(Math.random()*49)+1;
d=(int)(Math.random()*49)+1;
f=(int)(Math.random()*49)+1;
g=(int)(Math.random()*49)+1;
h=(int)(Math.random()*49)+1;
String one= Integer.toString(a);
String two= Integer.toString(b);
String three= Integer.toString(c);
String four= Integer.toString(d);
String five= Integer.toString(f);
String six= Integer.toString(g);
String seven= Integer.toString(h);
tf1.setText(one);
tf2.setText(two);
tf3.setText(three);
tf4.setText(four);
tf5.setText(five);
tf6.setText(six);
tf7.setText(seven);
System.out.println();
}
}

執行結果:

2011年3月18日 星期五

2011/03/18 上課內容

先開啟一範例程式並執行
之後按按鈕能夠顯示出wait
增加兩個按鈕,執行結果閃一下
下載此檔
將檔案執行
先讓套件改成SWING的
執行要有兩最後那行才不會出錯
新增兩個按鈕
執行結果
新增按鈕動作
執行結果
作業:按按鈕顯示出亂數的七個號碼

2011年3月11日 星期五

2011/03/11上課內容

今天的課程主要是教我們新增Button的幾種方法
首先可以用VB來新增Button
開新專案
點選Windows Form應用程式,然後按確定
接下來就可以開始新增Button
如果沒有VB的話,可以利用Office的任一軟體來用,在此舉EXCEL為例
點選檢視,巨集→錄製巨集
按確定, 之後隨意在工作表上打一個數字
然後按巨集→停止錄製
之後按巨集→檢視巨集→編輯
輸入以下字串然後按執行(F5)
出現以下結果
以下是在JAVA建立Button的方法
以及相關部落格
作業:完成一個CheckBox
要將以下的檔案執行出來

程式碼:
執行結果:

2011年3月4日 星期五

2011/03/04課程內容

JAVA範例程式碼:
開啟WordPad→另存新檔→存檔類型:文字文件
前往存檔路徑→將副檔名改成.java
開啟命令提示字元→將檔案編譯(JAVAC)→發現錯誤
以下左圖為解決方法,右圖為執行結果:


下圖代表兩個數相加不是用這種程式碼:

遇到以下問題:

上網找尋解決辦法:

寫出相加的程式碼→執行結果:1+2=3


作業:寫出兩矩陣(1*2)(2*1)相乘的矩陣程式