已阅读5页,还剩17页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验五:系统I/O程序设计实验目的:理解数据流的概念、Java流的层次结构及文件的概念;熟悉图形用户界面基本组件的使用方法,熟悉如何使用布局管理器对组件进行管理及如何使用Java的事件处理机制。实验要求:1、掌握字节流和字符流的基本使用方法。2、能够创建、读写、更新文件。3、掌握在Applet容器中添加组件的方法,掌握使用布局管理器对组件进行管理的方法。4、理解 Java的事件处理机制,掌握为不同组件编写事件处理程序的方法。5、掌握编写独立运行的窗口界面的方法。6、了解对话框及Java Swing组件的使用方法。实验内容:1、创建标准数据流的应用程序标准数据流指在字符方式下(如DOS提示符)程序与系统进行输入输出的方式,键盘和显示器屏幕是标准输入输出设备,数据输入的起点为键盘,数据输出的终点是屏幕,输出的数据可以在屏幕上显示出来。程序功能:将键盘上输入的字符在屏幕上显示出来,源代码如下public class IOTestpublic static void main(String args) byte buffer=new byte255;System.out.println(请在下面输入一行字符:n);try System.in.read(buffer,0,255); catch(Exception e) System.out.println(读取输入字符出错,错误信息为:+e.toString()+n); System.out.println(您刚才输入的一行字符为:n);String inputStr=new String(buffer,0);System.out.println(inputStr); 编译运行程序。实验结果:2、使用文件输入输出流的应用程序程序功能:读入一个文件的内容拷贝到另一个文件中去,源代码如下import java.io.*;class FileStreamsTestpublic static void main(String args)tryFileInputStream fis=new FileInputStream(einput.txt);FileOutputStream fos=new FileOutputStream(eoutput.txt); int c; while(c=fis.read()!=-1) fos.write(c); fis.close(); fos.close(); catch(FileNotFoundException e)System.err.println(FileStreamsTest: +e);catch(IOException e) System.err.println(FileStreamsTest: +e); 编译、运行程序。3、创建图形用户界面图形用户界面(Graphic User Interface,简称GUI)是为方便用户使用设计的窗口界面,在图形用户界面中用户可以看到什么就操作什么,取代了在字符方式下知道是什么后才能操作什么的方式。组件(Component)是构成GUI的基本要素,通过对不同事件的响应来完成和用户的交互或组件之间的交互。组件一般作为一个对象放置在容器(Container)内,容器是能容纳和排列组件的对象,如Applet、Panel(面板)、Frame(窗口)等。通过容器的add方法把组件加入到容器中。在Applet中添加标签、按钮并使用网格布局程序功能:在Applet容器中添加组件标签、按钮,并使用网格布局管理器排列组件在容器中的位置。源代码如下:import java.awt.*; import java.applet.Applet;public class Test_Button extends AppletLabel l1;Button b1, b2, b3, b4, b5, b6;public void init() setLayout(new GridLayout(3,3);/ 设置网格布局(3行3列共9个网格)l1=new Label(标签1);b1 = new Button(按钮1);b2 = new Button(按钮2);b3 = new Button(按钮3);b4 = new Button(按钮4);add(l1);add(b1);add(b2);add(b3);add(new Label();add(b4);add(new Button(按钮5); add( new Button(按钮6); add(new Label(标签2);编译程序,编写显示Applet的html页面文件。在面板中添加组件程序功能:在Applet中添加面板容器,并分别在Applet、面板容器中添加组件并使用不同的布局管理方式。源代码如下:import java.awt.*;import java.awt.Color;import java.applet.Applet;public class Test_Component extends Applet public void init() /设置最底层的 Applet容器为顺序布局setFont(new Font(Arial,Font.PLAIN,20);Label l=new Label(这是最底层的 Applet容器中的标签,Label.CENTER);add(l);Panel panel1=new Panel();add( panel1);panel1.setBackground(Color.blue);panel1.setForeground(Color.red);panel1.setLayout(new BorderLayout();/设置边界布局panel1.add(North, new Button(北);panel1.add(South, new Button(南);panel1.add(East, new Button(东);panel1.add(West, new Button(西);panel1.add(Center, new Label(这是在 Panel1面板中部添加的标签); Panel panel2=new Panel();add( panel2);panel2.setLayout(new GridLayout(3,1); /设置网格布局Choice c=new Choice ();/创建下拉式列表c.addItem(北京);c.addItem(上海);c.addItem(天津);Label l1=new Label(这是在 Panel2面板中的标签);Button b1=new Button(Panel2中的按钮);panel2.setBackground(Color.green);panel2.add(l1);panel2.add(b1);panel2.add(c);编译程序,编写显示Applet的html页面文件。4、从标准设备中输入若干行英文句子,直到输入bye结束,将这些字符串写入文件。源程序:import java.io.*;public class bye public static void main(String args) throws IOException System.out.println(Please input some sentences end with bye:); BufferedReader keyin=new BufferedReader(new InputStreamReader(System.in);String s; RandomAccessFile f=new RandomAccessFile(e:bye.txt,rw);boolean ss;while(s=keyin.readLine()!=null)ss=s.endsWith(bye);if(ss)System.exit(0);elseSystem.out.println(s);f.seek(f.length();f.writeUTF(s); 实验结果:5、从键盘输入一个整型数,一个双精度型和一个字符串,用DataOutputStream将这些数据输出到文件中,然后用DataInputStream从文件中读出这些数据并打印到标准输出设备。源程序:import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.InputStreamReader;import java.io.IOException;public class TestDataOutputStream public static void main(String args) throws IOException BufferedReader bf = new BufferedReader(new InputStreamReader(System.in); File newFile = new File( e:TestDataOutputStream.txt); try FileOutputStream fOut = new FileOutputStream(newFile); DataOutputStream dOut = new DataOutputStream(fOut); System.out.println(Please input Integer:); int i = Integer.parseInt(bf.readLine(); System.out.println(Please input Float:); float f = Float.parseFloat(bf.readLine(); System.out.println(Please input Double:); double d = Double.parseDouble(bf.readLine(); System.out.println(Please input Boolean:); boolean b = new Boolean(bf.readLine().booleanValue(); dOut.writeInt(i); dOut.writeFloat(f); dOut.writeDouble(d); dOut.writeBoolean(b); dOut.close(); catch (FileNotFoundException e) System.out.println(e); catch (IOException e) System.out.println(e); 结果:是乱码6、一个窗口中,在四个位置循环显示四种不同颜色的正方形,当鼠标点击时,停止循环显示,再次点击,恢复显示。import java.awt.*;import java.awt.event.*;import javax.swing.*;public class CycleRecTest extends JFrame /implements ActionListener /static int times = 1; / final CycleRec pane; JButton playButton; JButton stopButton; public CycleRecTest() setTitle(图形循环显示); setSize(470, 470); final CycleRec pane = new CycleRec(); /JButton playButton = new JButton(Play); /playButton.addActionListener(this); /JButton stopButton = new JButton(Stop); /stopButton.addActionListener(this); Container content = getContentPane(); content.setLayout(new BorderLayout(); /JPanel pane1 = new JPanel(); /pane1.add(playButton); /pane1.add(stopButton); /content.add(pane1, South); content.add(pane, Center); addMouseListener(new MouseAdapter() public void mouseClicked(MouseEvent e) if (pane.isPlay()pane.stop();elsepane.play(); ); public static void main(String args) JFrame app = new CycleRecTest(); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.setVisible(true); class CycleRec extends JPanel implements Runnable private Thread show; private Color blackCor = Color.black; private Color blueCor = Color.blue; private Color cyanCor = Color.cyan; private Color greenCor = Color.green; int imgID = 0; boolean bPlay = true; public CycleRec() public void paintComponent(Graphics g) if (imgID = 0) fillR(g, 100, 100, 100, blackCor);fillR(g, 200, 100, 100, blueCor);fillR(g, 100, 200, 100, cyanCor);fillR(g, 200, 200, 100, greenCor); else if (imgID = 1)fillR(g, 100, 100, 100, cyanCor);fillR(g, 200, 100, 100, blackCor);fillR(g, 100, 200, 100, greenCor);fillR(g, 200, 200, 100, blueCor);else if (imgID = 2)fillR(g, 100, 100, 100, greenCor);fillR(g, 200, 100, 100, cyanCor);fillR(g, 100, 200, 100, blueCor);fillR(g, 200, 200, 100, blackCor);else if (imgID = 3)fillR(g, 100, 100, 100, blueCor);fillR(g, 200, 100, 100, greenCor);fillR(g, 100, 200, 100, blackCor);fillR(g, 200, 200, 100, cyanCor); public static void fillR(Graphics g, int x, int y, int length, Color color) g.setColor(color);g.fillRect(x, y, length, length); public void run() int showpic = 0;while (bPlay = true)imgID = showpic;repaint();pause(1000);showpic+;if (showpic 3)showpic = 0; public void pause(int time) try Thread.sleep(time); catch (InterruptedException e) e.printStackTrace(); public void play() if (show = null)show = new Thread(this);bPlay = true;show.start();/show = new Thread(this);/ bPlay = true;/show.start(); public void stop() /if (show != null)/ show.destroy();if (show != null)show = null;bPlay = false; public boolean isPlay() if (show != null)return true;elsereturn false; 实验结果:7、在窗口的一个区域进行鼠标操作:mouseEnter,mouseExit、mousePress,mouseDrage、mouseClick。在窗口的另一个区域以文字显示鼠标所进行的相应操作。另外当鼠标进行mousePress、mouseDrage、mouseClick操作时,将显示一个图片。当鼠标拖拉时,图片随鼠标移动。import java.awt.*;import java.awt.event.*;import javax.swing.*;public class myclass /* param args*/private JFrame fm=new JFrame(鼠标移动示例); private JLabel lb=new JLabel(拖动测试对象);private Thread th=new Thread(new dothread();private boolean canmove=false;private Component com=null;private Point pt=new Point();myclass()fm.setLayout(null);fm.setDefaultCloseOperation(3);fm.setBounds(300, 200, 450, 300);fm.addMouseListener(new mouselisten();lb.setBounds(10, 10, 100, 25);/lb.hide();th.start();fm.getContentPane().add(lb);fm.show();class dothread implements Runnablepublic void run() / TODO 自动生成方法存根while (true)if (canmove)if (fm.getMousePosition()!=null)pt.x=fm.getMousePosition().x;pt.y=fm.getMousePosition().y;lb.setLocation(pt);class mouselisten implements MouseListenerpublic void mouseClicked(MouseEvent arg0) / TODO 自动生成方法存根if (arg0.getButton()=1)canmove=true;else canmove=false;public void mouseEntered(MouseEvent arg0) / TODO 自动生成方法存根public void mouseExited(MouseEvent arg0) / TODO 自动生成方法存根public void mousePressed(MouseEvent arg0) / TODO 自动生成方法存根canmove=false;public void mouseReleased(MouseEvent arg0) / TODO 自动生成方法存根canmove=false;public static void main(String args) / TODO 自动生成方法存根new myclass();8、实现一个计算器,界面类似WINDOWS中的界面,包括09十个数字、加、减、乘、除四种运算符号和一个用于输入及显示结果的文本框,单击每个按钮都能完成相应的功能。import java.awt.*; import java.awt.event.*; public class Calculator extends Frame implements ActionListener private double a=0, b=0, sum=0; int flag=0, count=0,p=1; char mode=?; /本次运算的符号 private TextField display = new TextField(); private Button one = new Button(1); private Button two = new Button(2); private Button three = new Button(3); private Button four = new Button(4); private Button five = new Button(5); private Button six = new Button(6); private Button seven = new Button(7); private Button eight = new Button(8); private Button nine = new Button(9); private Button zero = new Button(0); private Button point = new Button(.); private Button add = new Button(+); private Button sub = new Button(-); private Button mul = new Button(); private Button div = new Button(); private Button result = new Button(=); private Button clr = new Button(CLR); private Button quit = new Button(quit); private Label label = new Label(calculator); private class WindowCloser extends WindowAdapter public void windowClosing(WindowEvent we) System.exit(0); /布局 private void setup() Panel top = new Panel(); top.setLayout(new GridLayout(2,1); top.add(label); top.add(display); Panel number = new Panel(); number.setLayout(new GridLayout(6,3); number.add(one); number.add(four); number.add(seven); number.add(two); number.add(five); number.add(eight); number.add(three); number.add(six); number.add(nine); number.add(zero); number.add(add); number.add(sub); number.add(point); number.add(mul); number.add(div); number.add(result); number.add(clr); number.add(quit); setLayout(new BorderLayout(); add(North,top); add(South,number); /构造方法 public Calculator() super(Calculator); setup(); one.addActionListener(this); two.addActionListener(this); three.addActionListener(this); four.addActionListener(this); five.addActionListener(this); six.addActionListener(this); seven.addActionListener(this); eight.addActionListener(this); nine.addActionListener(this); zero.addActionListener(this); point.addActionListener(this); add.addActionListener(this); sub.addActionListener(this); mul.addActionListener(this); div.addActionListener(this); clr.addActionListener(this); quit.addActionListener(this); result.addActionListener(this); addWindowListener(new WindowCloser(); pack(); setSize(200,222); setVisible(true); void reset0() a=0; b=0; count=0; sum=0; p=1; mode=?; void setAndDis(char c) count=0; flag=0; a=Double.parseDouble(display.getText(); mode=c; /说明单击按钮以后要采取的行动 public void actionPerformed(ActionEvent ae) if(ae.getSource() = quit) System.exit(0); if(ae.getSource() = clr) reset0(); flag=0; display.setText(0); else if(ae.getSource() = zero) if(count != 0) reset0(); if(flag = 0) display.setText(0); flag=1; else display.setText(display.getText()+0); /输入09 if(ae.getSource() = one) if(count != 0) reset0(); if(flag = 0|display.getText().equals(0) display.setText(1); flag=1; else display.setText(display.getText()+1); if(ae.getSource() = two) if(count != 0) reset0(); if(flag = 0|display.getText().equals(0) display.setText(2); flag=1; else display.setText(display.getText()+2); if(ae.getSource() = three) if(count != 0) reset0(); if(flag = 0|display.getText().equals(0) display.setText(3); flag=1; else display.setText(display.getText()+3); if(ae.getSource() = four) if(count != 0) reset0(); if(flag = 0|display.getText().equals(0) display.setText(4); flag=1; else display.setText(display.getText()+4); if(ae.getSource() = five) if(count != 0) reset0(); if(flag = 0|display.getText().equals(0) display.setText(5); flag=1; else display.setText(display.getText()+5); if(ae.getSource() = six) if(count != 0) reset0(); if(flag = 0|display.getText().equals(0) display.setText(6); flag=1; else display.setText(display.getText()+6); if(ae.getSource() = seven) if(count != 0) reset0(); if(flag = 0|display.getText().equals(0) display.setText(7); flag=1; else display.setText(display.getText()+7); if(ae.getSource() = eight) if(count != 0) reset0(); if(flag = 0|display.getText().equals(0) display.setText(8); flag=1; else display.setText(display.getText()+8); if(ae.getSource() = nine) if(count != 0) reset0(); if(flag = 0|display.getText().equals(0) display.setText(9); flag=1; else display.setText(display.getText()+9); if(ae.getSource() = point) if(flag = 1 & p != 0) display.setText(display.getText()+.); p=0; /输入+,-, if(ae.getSource() = add) setAndDis(+); if(ae.getSource() = sub) setAndDis(-); if(ae.getSource() = mul) setAndDis(); if(ae.getSource() = div) setAndDis(); if(ae.getSource() = result)&mode != ?) flag=0; if(count = 0) b=Double.parseDouble(display.getText(); count=1; sum=run(a,b,mode); a=sum; display.setText(String.valueOf(sum); public double run(double a, double b, char mode) double sum=0; if(mode = +) sum=a+b; if(mode = -) sum=a-b; if(mode = ) sum=a*b; if(mode = ) sum=a/b; return(sum); public static void main(String args) Calculator cal = new Calculator(); 实验结果:实验分析:java的I/O系统提供了InputSream和Out
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 化工过滤工岗位环保竞赛考核试卷含答案
- 植保机械操作工岗前诚信考核试卷含答案
- 冲压模具工岗前工艺控制考核试卷含答案
- 催化裂化工岗位师带徒考核试卷含答案
- 碳汇计量评估师岗中责任制考核试卷含答案
- 空气相关的课件
- 口腔消毒技术课件
- 空气栓塞课件
- 化疗相关性腹泻的护理
- 足底骨折术后护理常规
- 2026-2027学年人教版八年级物理上册期末模拟测试卷(含答案)
- 动物医学专业教学标准(高等职业教育专科)2025修订
- 日本所有番号分类
- 利尿药用药护理
- 《防护措施下的光刻技术安全》课件
- 药品批发企业药品专业知识与技能培训
- 史记中的英雄豪杰课件
- 护理人员院感培训
- FZ∕T 93047-2011 针刺机行业标准
- 《三相交流系统短路电流计算 第1部分:电流计算》
- 《照明设计师教程》课件
评论
0/150
提交评论