握了制作简单菜单及处理菜单事件的基本方法流程,对创建对话框及定位、显示、激活和关闭对话框的方法也有了初步的了解和掌握。感觉java的图形界面很随意,但不缺乏严谨,很容易出错,不过只需要“按部就班”,循着规律认真的去做,问题也就不会是问题。
实验八、多线程实验
一、实验目的
⒈掌握多线程的实现方法 ⒉学会利用多线程来显示动画
二、预习内容
线程与进程的基础知识
三、实验设备与环境
装有JAVA语言工具软件 (Eclipse )的微机若干
四、实验内容
⒈使用Runnable接口的方法实现多线程
编辑TestRunnable.java,保存在D:\\myjava目录下。
import java.applet.*; import java.awt.*;
public class TestRunnable extends Applet implements Runnable{ Label prompt1=new Label(\); Label prompt2=new Label(\); TextField threadFirst=new TextField(14); TextField threadSecond=new TextField(14); Thread thread1, thread2; int count1=0,count2=0; public void init(){ add(prompt1); add(threadFirst); add(prompt2); add(threadSecond); } public void start(){ thread1=new Thread(this, \); thread2=new Thread(this, \); thread1.start(); thread2.start(); } public void run(){ String currentRunning; while(true){ try{ Thread.sleep((int)(Math.random()*10000)); } catch(Exception e) {} currentRunning=Thread.currentThread().getName(); if(currentRunning.equals(\)){ count1++; threadFirst.setText(\+count1+\); } else if(currentRunning.equals(\)){ count2++; threadSecond.setText(\+count2+\); } } } }
(1) 编译TestRunnable.java。
(2) 编辑TestRunnable.htm,要求与TestRunnable.class在同一目录下。
(3) 运行TestRunnable.htm。
⒉ 实现简单动画
实现一个简单动画,效果为一个球由小到大,从屏幕左侧滚动到右侧。
import java.awt.*;
public class Move extends Frame { public void launchFrame(){ int width = 800; int height = 200; setSize(width,height); setVisible(true); setBackground(Color.blue); setTitle(\会变大的球\); new PaintThread().start(); } int x = 0; int y = 150; int w = 6; int h = 6; public int getX(){ return x; } public void paint(Graphics g){ g.setColor(Color.yellow); g.fillOval(x,y,w,h); if(x < 600){ x = x + 10; y=y-2; w+=2; h+=2; } } public static void main(String args[]){ Move mv = new Move(); mv.launchFrame(); } class PaintThread extends Thread { public void run(){ while(true){ if(getX()<600) repaint(); try{ Thread.sleep(100); } catch(Exception e){ e.printStackTrace(); } } } } }
3. 新建一个主Java文件,命名为SallTicket java,使用Runnable接口定义多线程类Ticket.java,在Ticket中定义成员变量ticket=100,用同步方式创建两个线程售票。
}
public class search_8_SallTicket implements Runnable{ int ticket=100; public void run(){ while(true){ try{ Thread.sleep(300); }catch(Exception e){ System.out.println(e); } showLeftedTicket(); } } public synchronized void showLeftedTicket(){ if(Thread.currentThread().getName().equals(\)&&(ticket>0)){ System.out.println(\号窗口售出一张票,目前还剩余票数:\+--ticket); } if(Thread.currentThread().getName().equals(\)&&(ticket>0)){ System.out.println(\号窗口售出一张票,目前还剩余票数:\+--ticket); } if(ticket<=0){ System.out.println(\票已售罄!\); System.exit(1); } }
public static void main(String [] args){ search_8_SallTicket tt = new search_8_SallTicket(); Thread t1,t2; t1=new Thread(tt); t2=new Thread(tt); t1.setName(\); t2.setName(\); t1.start(); t2.start(); }
运行结果:
五、注意事项
⒈认真填写实验报告
⒉遵守实验室各项制度,服从实验指导教师的安排 ⒊按规定的时间完成实验
六、说明
⒈建议学时数2学时 七、实验总结与体会
通过本实验基本掌握了多线程的实现方法,并学会利用多线程来显示简单动画。知道了在对一个实际的应用程序进行开发的时候一定要考虑他的多方因素,不然在使用中会出现大问题。
实验九、输入输出流实验
一、实验目的
⒈了解文件的概念和文件对象的创建方法
⒉了解FileInputStream和FileOutoutStream的基本概念 ⒊学会创建文件输入输出流
⒋掌握使用文件输入输出流读写文件的方法
二、预习内容
输入输出类的使用方法
三、实验设备与环境
装有JAVA语言工具软件 (Eclipse )的微机若干
四、实验内容
⒈编写程序读取文本文件内容
import java.io.*;
import javax.imageio.IIOException; public class search_9_ReadText { public static void main(String args[]){ try{ File infile=new File(\); FileReader reader=new FileReader(infile); @SuppressWarnings(\) BufferedReader br=new BufferedReader(reader); String s=null; while((s=br.readLine())!=null){ System.out.println(s+\); } }catch(IOException e){ System.out.println(e); } } }
运行结果:
⒉ 读取图像文件
编辑ReadPic.java。设保存在D:myjava目录下。设该目录下已经存在一个run.gif文件(可以在此目录下放置一个本机已有的图像文件名字为run.gif。)
import java.io.*;
public class ReadPic{
}
ReadPic(){ try{ File p1=new File(\); File p2=new File(\); FileInputStream inFile=new FileInputStream(p1); BufferedInputStream inB=new BufferedInputStream(inFile); FileOutputStream outFile=new FileOutputStream(p2); BufferedOutputStream putB=new BufferedOutputStream(outFile); byte b[]=new byte[ (int) p1.length()]; while(inB. read(b)!=-1){ putB.write(b); } putB.flush(); inB.close(); putB.close(); }catch(Exception e){ e.printStackTrace(); } }
public static void main(String args[ ]){ new ReadPic(); }
(1) 编译并运行ReadPic..class。
(2) 打开copyrun.gif与run.gif进行对比。 ⒊将内容写入文件
编辑UseStream1.java。 import java.io.*;
public class search_9_UseStream1 { search_9_UseStream1(String path){ try{ File f=new File(path, \); //向文件test1.txt中写入数据 FileWriter putFile=new FileWriter(f); BufferedWriter OutB=new BufferedWriter(putFile); String s=\你们好,这是一个测试写入数据的文件。\; OutB.write(s); //插入一行 OutB.newLine(); OutB.write(\这是利用FileWrite与 BuffereWrite的例题。\); //需要调用flush()方法 OutB.flush(); //写入完毕要关闭流 OutB.close(); //从text.txt中读取数据 FileReader inFile=new FileReader(f); BufferedReader inB=new BufferedReader(inFile); /*inB中含有能够直接读取一行数据的方法raesLine()供我们使用,当然返回值null时,意味着读取结束*/ String fileContent=\,str=\; while((fileContent=inB.readLine())!=null){ str=str+fileContent+\; } System.out.println(str); inB.close(); }catch(Exception e){ e.printStackTrace(); } } public static void main(String args[ ]){ new search_9_UseStream1(\); } }