Tfd7.setBackground(Color.RED);
// app17_8, Button類別
import java.awt.*;
import java.awt.event.*;
public class app17_8 extends Frame implements ActionListener
{
//static Frame frm=new Frame("Button class");
static Button btn1=new Button("Button1"); // 建立按鈕物件
static Button btn2=new Button("Button2");
static Button btn3=new Button("Button3");
static Button btn4=new Button("Button4");
static Button btn5=new Button("Button5");
static BorderLayout border=new BorderLayout();//環繞排列
static TextField Tfd=new TextField();
public static void main(String args[])
{
app17_8 frm=new app17_8();
frm.setSize(200,200);
btn1.addActionListener(frm);
frm.add(btn1,border.WEST); // 在視窗內加入按鈕
frm.add(btn2,border.EAST);
frm.add(btn3,border.NORTH);
frm.add(btn4,border.SOUTH);
//frm.add(btn5,border.CENTER);
frm.add(Tfd,border.CENTER);
//frm.setLayout(new FlowLayout());
frm.setVisible(true);
frm.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
int rn;
rn=(int) (Math.random()*49) ;
System.out.println(rn );
}
}
執行結果 |
// app17_8, Button類別
import java.awt.*;
import java.awt.event.*;
public class app17_8 extends Frame implements ActionListener
{
//static Frame frm=new Frame("Button class");
static Button btn1=new Button("樂透產生器"); // 建立按鈕物件
static Button btn2=new Button("Button2");
static Button btn3=new Button("Button3");
static Button btn4=new Button("Button4");
static Button btn5=new Button("Button5");
//static BorderLayout border=new BorderLayout();//環繞排列
static TextField Tfd1=new TextField();
static TextField Tfd2=new TextField();
static TextField Tfd3=new TextField();
static TextField Tfd4=new TextField();
static TextField Tfd5=new TextField();
static TextField Tfd6=new TextField();
static TextField Tfd7=new TextField();
public static void main(String args[])
{
app17_8 frm=new app17_8();
frm.setSize(200,200);
btn1.addActionListener(frm);
frm.add(btn1);
frm.add(Tfd1);
frm.add(Tfd2);
frm.add(Tfd3);
frm.add(Tfd4);
frm.add(Tfd5);
frm.add(Tfd6);
frm.add(Tfd7);
//frm.add(btn1,border.WEST); // 在視窗內加入按鈕
//frm.add(btn2,border.EAST);
//frm.add(btn3,border.NORTH);
//frm.add(btn4,border.SOUTH);
//frm.add(btn5,border.CENTER);
//frm.add(Tfd,border.CENTER);
frm.setLayout(new FlowLayout());
frm.setVisible(true);
frm.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
int t1,t2,t3,t4,t5,t6,t7;
t1=(int)(Math.random()*49+1);
t2=(int)(Math.random()*49+1);
t3=(int)(Math.random()*49+1);
t4=(int)(Math.random()*49+1);
t5=(int)(Math.random()*49+1);
t6=(int)(Math.random()*49+1);
t7=(int)(Math.random()*49+1);
String string1= Integer.toString(t1);
String string2 = Integer.toString(t2);
String string3 = Integer.toString(t3);
String string4 = Integer.toString(t4);
String string5 = Integer.toString(t5);
String string6 = Integer.toString(t6);
String string7 = Integer.toString(t7);
Tfd1.setText(string1);
Tfd2.setText(string2);
Tfd3.setText(string3);
Tfd4.setText(string4);
Tfd5.setText(string5);
Tfd6.setText(string6);
Tfd7.setText(string7);
}
}