2011-03-25

2011/03/25

BorderLayout
fm.add(bt1, bd.EAST);     <==位置需用大寫



還蠻有美感的所以貼一下


 // AWT, Button類別

import java.awt.*;
import java.awt.event.*;


public class awttest extends Frame implements ActionListener
{
//static Frame fm=new Frame("Button class");
static Button bt1=new Button("1");   // 建立按鈕物件
static Button bt2=new Button("2");
static Button bt3=new Button("3");
static Button bt4=new Button("4");
static TextField tf1=new TextField("5");

public static void main(String args[])
{
awttest fm=new awttest();
BorderLayout bd=new BorderLayout();
fm.setLayout(bd);
fm.setSize(200,150);
bt1.addActionListener(fm);
fm.add(bt1, bd.EAST);   // 在視窗內加入按鈕
fm.add(bt2, bd.WEST);
fm.add(bt3, bd.SOUTH);
fm.add(bt4, bd.NORTH);
fm.add(tf1, bd.CENTER);
fm.setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
int r1;
r1=(int) (Math.random()*49)+1;
System.out.println(r1);
}

}

=======================

// AWT, Button類別

import java.awt.*;
import java.awt.event.*;


public class awttest extends Frame implements ActionListener
{
//static Frame fm=new Frame("Button class");
/*
static Button bt1=new Button("1");   // 建立按鈕物件
static Button bt2=new Button("2");
static Button bt3=new Button("3");
static Button bt4=new Button("4");
static TextField tf1=new TextField("5");
*/
static Button bt1=new Button("RUN");   // 建立按鈕物件
static TextField tf1=new TextField("1");
static TextField tf2=new TextField("2");
static TextField tf3=new TextField("3");
static TextField tf4=new TextField("4");
static TextField tf5=new TextField("5");
static TextField tf6=new TextField("6");
static TextField tf7=new TextField("7");
public static void main(String args[])
{
awttest fm=new awttest();
//BorderLayout bd=new BorderLayout();
//fm.setLayout(bd);
fm.setLayout(new FlowLayout());
fm.setSize(200,150);
bt1.addActionListener(fm);

/*
fm.add(bt1, bd.EAST);   // 在視窗內加入按鈕
fm.add(bt2, bd.WEST);
fm.add(bt3, bd.SOUTH);
fm.add(bt4, bd.NORTH);
fm.add(tf1, bd.CENTER);
*/
fm.add(bt1);   // 在視窗內加入按鈕
fm.add(tf1);
fm.add(tf2);
fm.add(tf3);
fm.add(tf4);
fm.add(tf5);
fm.add(tf6);
fm.add(tf7);
fm.setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
int r1,r2,r3,r4,r5,r6,r7;
r1=(int) (Math.random()*49)+1;
r2=(int) (Math.random()*49)+1;
r3=(int) (Math.random()*49)+1;
r4=(int) (Math.random()*49)+1;
r5=(int) (Math.random()*49)+1;
r6=(int) (Math.random()*49)+1;
r7=(int) (Math.random()*49)+1;
String sv1 = Integer.toString(r1);
String sv2 = Integer.toString(r2);
String sv3 = Integer.toString(r3);
String sv4 = Integer.toString(r4);
String sv5 = Integer.toString(r5);
String sv6 = Integer.toString(r6);
String sv7 = Integer.toString(r7);
tf1.setText(sv1);
tf2.setText(sv2);
tf3.setText(sv3);
tf4.setText(sv4);
tf5.setText(sv5);
tf6.setText(sv6);
tf7.setText(sv7);
}

}


 執行圖

沒有留言:

張貼留言