`
mmdev
  • 浏览: 12919789 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

Java程序设计 实验4

 
阅读更多
//1、编写Applet小程序,通过在HTML文档中接收参数,
//用不同颜色、字体显示当前的系统时间,
//源程序保存为Ex4_1.java,HTML文件保存为Ex4_1.html
import java.awt.*;
import java.applet.Applet;
import java.util.Calendar;

public class Ex4_1 extends Applet
{
	Calendar now;
	private String s1;
	private int size,color;
	
	public void init()
	{
		now=Calendar.getInstance();      //获得系统当前时间
		s1=now.get(now.HOUR)+"时"
		+now.get(now.MINUTE)+"分"
		+now.get(now.SECOND)+"秒";
		size=Integer.parseInt(getParameter("size"));//获得字体大小
		color=Integer.parseInt(getParameter("color"),16);	//获得颜色值
	}
    
	public void paint(Graphics g)
	{
		Color c=new Color(color);
		g.setColor(c);
		Font f=new Font("",1,size);
		g.setFont(f);
		g.drawString(s1,10,50);			//显示指定大小颜色的字符串
	}
}

<html>
<Applet code="Ex4_1.class" width=300 height=100>
<param name=s1 value=s1>
<param name=size value=30>
<param name=color value=007000>
</Applet>
</html>


//2、编写Applet小程序,
//实现将输入的字符串翻转输出
//实例输入:Hello world
//实例输出:dlrow olleH

import java.applet.*; 
import java.awt.*; 
import java.awt.event.*; 
public class Ex4_2 extends Applet implements ActionListener 
{ 
	TextField t1,t2;Label label1,label2; 
	Button b1,b2; 
	public void init() 
	{ 
	t1=new TextField(20); 
	t2=new TextField(20); 
	b1=new Button("确定"); 
	b2=new Button("清除"); 
	label1=new Label("请输入一字符串:"); 
	label2=new Label("反转后的字符串");
	t2.setEditable(false); 
	b1.addActionListener(this); 
	b2.addActionListener(this); 
	add(label1);add(t1);
	add(b1); add(label2);
	add(t2); add(b2);
} 
public void actionPerformed(ActionEvent e) 
{ 
	if(e.getSource()==b1) 
	{ 
	StringBuffer str=new StringBuffer(t1.getText()); 
	str=str.reverse(); 
	t2.setText(null); 
	t2.setText(str.toString()); 
} 
	if(e.getSource()==b2) 
	{ 
	t1.setText(null); 
	t2.setText(null); 
	} 
} 
} 

<html>
<Applet code="Ex4_2.class" width=400 height=200>
</Applet>
</html>

//编写应用程序:利用标准输入方法从键盘输入字符,
//并将输入的字符写到文本文件“Ex4_3.txt”中。
//然后再打开文本文件“Ex4_3.txt”,把其中的内容输出到屏幕上,源程序保存为Ex4_3.java。
//注意:建立文件输入流进行读取前,先调用文件输出流的close()方法关闭输出流
//或使用文件输出流的flush()方法把缓冲区的信息先写入文件。

import java.io.*;

public class Ex4_3
{
	public static void main(String[] args) throws java.io.IOException
	{
		char ch1;
		File file1=new File("D:/Ex4_3.txt");		
		FileOutputStream fout=new FileOutputStream(file1);
		ch1=(char)System.in.read();
		while(ch1!='?')
		{
			fout.write(ch1);
			ch1=(char)System.in.read();
		}
		fout.close();

		int ch2;
		File file2=new File("D:/Ex4_3.txt");
		FileInputStream fin=new FileInputStream(file2);
		ch2=fin.read();
		while(ch2!=-1)
		{
			System.out.print((char)ch2);
			ch2=fin.read();
		}
		fin.close();
	}
		
}


//4、编写Applet程序,随机生成10个小于500的整数,
//统计其中的奇数个数、偶数个数,如下图所示分别输出这些整数和统计结果,
//源程序保存为Ex4_4.java,HTML文件保存为Ex4_4.html
import java.lang.*;
import java.awt.*;
import java.applet.Applet;
import java.util.*;
public class Ex4_4 extends Applet
{
	int r[]=new int[10];
	String array="";
	String evenInt="";
	String oddInt="";
	int ecount=0;
	int ocount=0;
	Random ra=new Random(123457890L);
	public void init()
	{
		
		for(int i=0;i<10;i++)
		{
			r[i]=ra.nextInt(500);
			array+=(String.valueOf(r[i])+" ");
			if(r[i]%2!=0)
			{
				evenInt+=(String.valueOf(r[i])+",");
				++ecount;
			}
			else
			{
				oddInt+=(String.valueOf(r[i])+",");
				++ocount;
			}
		}
	}
	public void paint(Graphics g)
	{
		g.drawString("生成随机数:"+array,20,20);
		g.drawString("奇数:"+evenInt+"共"+ecount+"个",20,40);
		g.drawString("偶数:"+oddInt+"共"+ocount+"个",20,60);
	}
}
	


<html>
<applet code="Ex4_4.class" width=400 height=200>
</applet>
</html>



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics