内容发布更新时间 : 2024/11/16 22:53:55星期一 下面是文章的全部内容请认真阅读。
6. 应用FileInputStream类,编写应用程序,从磁盘上读取一个Java程序,并将源程序代码显示在屏幕上。(被读取的文件路径为:E:/myjava/Hello.java) // Programme Name FISDemo.java import java.io.*; public class FISDemo {
public static void main(String args[]) { byte[] buf=new byte[2056]; try{
FileInputStream
FileInputStream(\ int bytes=fileIn.read(buf,0,2056); String str=new String(buf,0,bytes); }catch(Exception e){ e.printStackTrace( );
} }
7、编写一个Java程序将当100,101,102,103,104,105个数以数组的形式写入到Dest.txt文件中,并以相反的顺序读出显示在屏幕上。(p190,例7-2) //programme name IODemo.java import java.io.*; public class IODemo {
public static void main( String args[] ) { int data[] = {100,101,102,103,104,105};
fileIn=new
int t; try
{ DataOutputStream out = new DataOutputStream (new
FileOutputStream(“dest.txt”));
for(int i=0;i in = new DataInputStream (new FileInputStream(“dest.txt”)); for(int i= data.length-1;i>= 0;i--) { t=in.readInt(data[i]); “ ”+t); } in.close(); }catch(IOException e) {} } } 8. 利用Applet类和Runnable接口实现滚动字幕,其中字幕文字(“学好Java有工作”)和时间间隔(“200”)需要由页面文件中 public class MoveMessage extends JApplet implements Runnable { String str; } int time; private Thread thread; public void init() { } public void start() { } public void run() { } int x = 0; Graphics g = getGraphics(); while (true) { } try { } g.clearRect(0, 0, getWidth(), getHeight()); g.drawString(str, x, 30); x += 2; if (x >= getWidth()) x = 0; Thread.sleep(time); e.printStackTrace(); } catch (Exception e) { thread.start(); setBackground(Color.PINK); str = getParameter(\ String timeArg = getParameter(\time = Integer.parseInt(timeArg);; thread = new Thread(this);
9、编写一个Java程序实现多线程,在线程中输出线程的名字,隔300毫秒输出一次,共输出20次。(p202,例8-1) // programme name TestThread; // 声明一个子线程类Threaddemo; class ThreadDemo extends Thread { public ThreadDemo(String str) {
super(str); }
public void run() { for(int i=0;i<20;i++){
“ ”+this.getName()); Try {
Sleep(300);
}catch(InterruptedException e){ Return; } }
“ /end”); } }
public class TestThread {
public static void main( String args[] ) { ThreadDemo thread1=new ThreadDemo(“T1”); ThreadDemo thread2=new ThreadDemo(“T2”); ThreadDemo thread3=new ThreadDemo(“T3”); thread1.start(); thread2.start(); thread3.start(); } }
10. 编写程序,在屏幕上显示带标题的窗口,并添加一个按钮。当用户单击按钮
时,结束程序。
// Programme Name ButtonEventDemo.java import javax.swing.*;
public class ButtonEventDemo extends JPanel implements ActionListener{
protected JButton b1;
//声明一个按钮对象
public ButtonEventDemo() { //构造方法
ImageIcon ButtonIcon = new ImageIcon(\创
建按钮的图标对象
b1 = new JButton(\退出按钮\ //生成按钮对象
b1.setMnemonic(KeyEvent.VK_E); //设置b1的助记符是Alt+E
b1.setToolTipText(\这是退出按钮。\设置按钮提示条 this.add(b1); //往面板对象中加载按钮
b1.addActionListener(this); //本类对象注册为按钮的事件监听器 }
public void actionPerformed(ActionEvent e){ //按钮事件响应方法
System.exit(0); //按b1则退出主程序 }
private static void createGUI() { //创建窗体
JFrame.setDefaultLookAndFeelDecorated(true); //设置java隐含观感
JFrame frame = new JFrame(\按钮测试\生成应用程序主窗体 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置关