2011年3月25日 星期五

JAVA-15

這是今天作業的程式碼
一開始遇到很多問題
後來經過問同學後才知道
因為使用了flowlayout之後
之前的borderlayout就沒有再用了
要用掉
用掉之後因為沒有borderlayout
所以方位也不用了

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test3 extends Frame implements ActionListener
{

static Frame frame=new Frame("Button class");
static Button button1=new Button("Button 1");
//static Button button2=new Button("Button 2");
static TextField tbx1=new TextField("TextField1");
static TextField tbx2=new TextField("TextField2");
static TextField tbx3=new TextField("TextField3");
static TextField tbx4=new TextField("TextField4");
static TextField tbx5=new TextField("TextField5");
static TextField tbx6=new TextField("TextField6");
static TextField tbx7=new TextField("TextField7");
static TextField tbx8=new TextField("TextField8");



public static void main(String args[])
{
Test3 frame=new Test3();
FlowLayout border=new FlowLayout();
frame.setLayout(border);
frame.setSize(400,400);
button1.addActionListener(frame);
frame.add(button1);
//frame.add(button2, border);
frame.add(tbx1, border);
frame.add(tbx2);
frame.add(tbx3);
frame.add(tbx4);
frame.add(tbx5);
frame.add(tbx6);
frame.add(tbx7);
frame.add(tbx8);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int a;
int b;
int c;
int d;
int f;
int g;
int h;
a=(int) ((Math.random()*49)+1);
String stringValue1=Integer.toString(a);
b=(int) ((Math.random()*49)+1);
String stringValue2=Integer.toString(b);
c=(int) ((Math.random()*49)+1);
String stringValue3=Integer.toString(c);
d=(int) ((Math.random()*49)+1);
String stringValue4=Integer.toString(d);
f=(int) ((Math.random()*49)+1);
String stringValue5=Integer.toString(f);
g=(int) ((Math.random()*49)+1);
String stringValue6=Integer.toString(g);
h=(int) ((Math.random()*49)+1);
String stringValue7=Integer.toString(h);
System.out.println(a);
tbx2.setText(stringValue1);
System.out.println(b);
tbx3.setText(stringValue2);
System.out.println(c);
tbx4.setText(stringValue3);
System.out.println(d);
tbx5.setText(stringValue4);
System.out.println(f);
tbx6.setText(stringValue5);
System.out.println(g);
tbx7.setText(stringValue6);
System.out.println(h);
tbx8.setText(stringValue7);
}
}

沒有留言:

張貼留言