public static void main(String[] args) { Prime p1=new Prime(2,1000); Prime p2=new Prime(5000,10000); Thread th1=new Thread(p1);Thread th2=new Thread(p2); th1.start(); try
{th1.join();}
catch(Exception e)
{System.out.println(e.getMessage());} th2.start(); } }
第七八次上机
import java.awt.*; import java.applet.*; import java.awt.event.*;
public class Draw extends Applet implements ActionListener { String choice1; TextField text1;
public void init() //改写父类的init()方法 { Label lab1=new Label(\输入欲绘制的图形\ text1=new TextField(10); //初始化文本框 add(lab1); //添加组件到浏览器窗口 add(text1);
text1.addActionListener(this); //对text1注册监视器 }
public void actionPerformed(ActionEvent e) //实现接口中的抽象方法 { choice1=text1.getText(); //获取文本框中的数据 repaint(); //重新调用paint方法 } public void paint(Graphics g) //改写父类的paint方法 { { if(choice1.equals(\线\ { for(int i=1;i<=5;i++) g.drawLine(10,50,250,50+i*10); } else
if (choice1.equals(\圆\ {for(int i=1;i<=5;i++)
g.drawOval(10+i*10,10+i*10,50+i*10,50+i*10); } else
if(choice1.equals(\矩形\ {for(int i=1;i<=5;i++) g.drawRect(10+i*10,10+i*10,50+i*10,50+i*10); } else
g.drawString(\输入有误!\ } } }
/在第一个和第二个文本框中输入两个数据,在第三和第四个文本框中显示两数之和及之差.要求:在第一和第二个文本框中按回车键或者按计算按钮均能完成计算。 import java.awt.*;
import java.awt.event.*; //需要该包中的ActionEvent 类和接口ActionListener class Myframe extends Frame implements ActionListener { TextField text1, text2, text3,text4; //定义文本框
Label lab1=new Label(\第一个数据为\定义标签 Label lab2=new Label(\第二个数据为\ Label lab3=new Label(\两数之和为\ Label lab4=new Label(\两数之差为\ Button but1=new Button(\计算\ Myframe(String s)//构造函数 { super(s); setSize(400,400); setVisible(true);//设置窗口大小和可见性 text1=new TextField(10); text2=new TextField(10); text3=new TextField(10); text4=new TextField(10); setLayout(new FlowLayout()); //设置布局 add(lab1);add(text1); add(lab2);add(text2); add(lab3);add(text3); add(lab4);add(text4); add(but1);//将控件添加到窗口
text1.addActionListener(this); //注册监视器 text2.addActionListener(this); but1.addActionListener(this);
validate();//保证窗口所有元素可见 }
public void actionPerformed (ActionEvent e) //实现接口中的方法,以处理事件 { long n3,n4; if (e.getSource()==text1 || e.getSource()==text2 ||e.getSource()==but1) { long n1,n2; n1=Long.parseLong(text1.getText()); n2=Long.parseLong(text2.getText()); n3=n1+n2; n4=n1-n2; text3.setText(String.valueOf(n3)); text4.setText(String.valueOf(n4)); }//获取text1和text2中的文本并将其转化为long 类型数据,并计算和和差 } }
public class App7_2{ public static void main(String args[]) { Myframe mr=new Myframe(\求两数之和及两数之差的结果\ } }