JAVA编程题全集(100题及答案) 下载本文

import java.io.*; public class FISDemo {

public static void main(String args[]) { byte[] buf=new byte[2056]; try{

FileInputStream fileIn=new FileInputStream(\ int bytes=fileIn.read(buf,0,2056); String str=new String(buf,0,bytes); System.out.println(str); }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};

int t; try

{ DataOutputStream out = new DataOutputStream (new FileOutputStream(“dest.txt”)); for(int i=0;i

DataInputStream in = new DataInputStream (new FileInputStream(“dest.txt”)); for(int i= data.length-1;i>= 0;i--) { t=in.readInt(data[i]);

System.out.print(“ ”+t); }

System.out.println( ); in.close();

}catch(IOException e)

{ System.out.println(e.getMessage());} } }

8. 利用Applet类和Runnable接口实现滚动字幕,其中字幕文字(“学好Java有工作”)和

时间间隔(“200”)需要由页面文件中标记的子标记传递。

import java.awt.Color; import java.awt.Graphics; import javax.swing.JApplet;

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 {

Thread.sleep(time);

thread.start();

setBackground(Color.PINK); str = getParameter(\String timeArg = getParameter(\time = Integer.parseInt(timeArg);; thread = new Thread(this);

} catch (Exception e) { }

g.clearRect(0, 0, getWidth(), getHeight()); g.drawString(str, x, 30); x += 2;

if (x >= getWidth())

x = 0;

e.printStackTrace();

}

}

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++){

System.out.print(“ ”+this.getName()); Try { Sleep(300);

}catch(InterruptedException e){ System.out.println(e.getMessage()); Return; } }

System.out.println(“ /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.*; import java.awt.event.*;

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); //设置关闭时隐含操作