2011年3月25日 星期五

JAVA第六堂課





                                          一開始先參考別人的程式,一步一步改成自己想要的東西,
                                          不過不能照抄。
                                 

                                         JAVA 版面 → 可以參考版面的用法


 
           上面所圈的是建立多個按鈕的程式和文字方塊,
還有將他把放在適當的位置避免重疊

這次要直接在文字方塊上印出七個號碼,
程式如下。
    程式 :
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,p,q,r;
a=(int)(Math.random()*49)+1;
b=(int)(Math.random()*49)+1;
c=(int)(Math.random()*49)+1;
d=(int)(Math.random()*49)+1;
p=(int)(Math.random()*49)+1;
q=(int)(Math.random()*49)+1;
r=(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(p);
String six= Integer.toString(q);
String seven= Integer.toString(r);
tf1.setText(one);
tf2.setText(two);
tf3.setText(three);
tf4.setText(four);
tf5.setText(five);
tf6.setText(six);
tf7.setText(seven);
System.out.println();
}
}

沒有留言:

張貼留言