import java.util.*;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.*;
import java.lang.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class Rand extends JFrame implements ActionListener{//Rand继承JFrame,实现ActionListener接口。
Container c=getContentPane();//定义一个面板
JTextArea textShow=new JTextArea(10,10);//定义文本区域
JButton button=new JButton("OK");//定义一个为OK的按钮
Rand(){
c.setLayout(new FlowLayout());//new FlowLayout()为布局管理器,已经定义的有几个,同时还可自己根据要求去定义布局。
c.add(textShow);
c.add(button);
button.addActionListener(this);//对button进行监听
}
public static void main(String args[]) {
Rand a=new Rand();
a.setVisible(true);//设置对象a是否可视。
a.setBounds(500, 500, 500, 500);//设置对象a的大小。
}
public void actionPerformed(ActionEvent e) {//触发事件时所执行的程序,e传进来的是事件源的对象。
Random r=new Random();//随机类
int temp=r.nextInt(36)+1;//0到35的数
String n=new Integer(temp)+"";//+""是整型数据转换成字符串的方法,还有其他。。。
textShow.append(n+"\t");//
}
}
通过这个程序,我们可以了解关于窗体程序的一些知识,这个代码主要是通过事件源,转到实现类,触发监听程序,算出随机数,然后在显示到JTextArea中。
阅读(264) | 评论(0) | 转发(0) |