《java程序设计》实验指导书(完整) 联系客服

发布时间 : 星期一 文章《java程序设计》实验指导书(完整)更新完毕开始阅读b4715d38f5335a8102d220b9

XX学院java实验报告

public Frame1() { }

//jbInit()方法完成界面的初始化设置 private void jbInit() throws Exception {

37

setLayout(borderLayout1); jButton1.addActionListener(this); jButton2.addActionListener(this);

add(jTextArea1,borderLayout1.CENTER); add(jPanel1,borderLayout1.SOUTH); jPanel1.setLayout(new FlowLayout()); jPanel1.add(jButton1); jPanel1.add(jButton2); setBounds(200,200,600,600);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){ }

setVisible(false); System.exit(0);

});

XX学院java实验报告

}

//保存、打开两个按钮的事件处理方法 public void actionPerformed(ActionEvent e) { //如果用户点击保存按钮

if(e.getSource()==jButton1){ JFileChooser fc1=new JFileChooser();

int option = fc1.showSaveDialog(null); // 显示文件对话框

if(option == JFileChooser.APPROVE_OPTION) // 如果确认选取文件 {

File file = fc1.getSelectedFile(); // 取得选择的文件 setTitle(file.toString()); // 在标题栏上设定文件名称 try {

file.createNewFile(); // 建立文件 }

catch(IOException e1) {

JOptionPane.showMessageDialog(null, e1.toString(),

\无法建立新文件\ } } }

//如果用户点击打开按钮 if(e.getSource()==jButton2) {

jTextArea1.setText(\

JFileChooser fc=new JFileChooser();

38

XX学院java实验报告

int option1 = fc.showDialog(null, null);

39

// 使用者按下确认键

if(option1 == JFileChooser.APPROVE_OPTION) { try {

// 开启选取的文件 BufferedReader buf = new BufferedReader( new FileReader( fc.getSelectedFile()));

setTitle(fc.getSelectedFile().toString());// 设定文件标题 jTextArea1.setText(\清除前一次文件

String lineSeparator = System.getProperty(\ // 取得系统相依的换行字符 String text;// 读取文件并附加至文字编辑区 while((text = buf.readLine()) != null) { }

jTextArea1.append(text); jTextArea1.append(lineSeparator);

buf.close(); }

catch(IOException ee) {

JOptionPane.showMessageDialog(null, ee.toString(), \开启文件失败\ }

XX学院java实验报告

catch(Exception ex){

System.out.print(ex.toString());

} } } }

2.主程序:

}

import java.awt.*; import javax.swing*;

public class Application1 {

public Application1( ) {

Frame1 frame = new Frame1(); // 使窗口居中

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = frame.getSize(); if (frameSize.height > screenSize.height)

{ frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width)

{ frameSize.width = screenSize.width; }

frame.setLocation( (screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);

frame.setVisible(true); }

//主程序的main()方法

public static void main(String[] args) { new Application1( ); } }

三、试验要求

1、预习试验内容并写出上机报告。 2、实验中出现的问题及实验体会。

实验十五 网络通信

一、实验目的

1.理解网络通信的原理和常用通信技术的概念; 2.掌握基于TCP协议的套接字的网络编程方法 3.掌握网络通信在实际应用开发程序中的应用。 二、实验内容

案例 聊天程序解析

通过建立一个Socket客户端和一个ServerSocket服务端进行实时数据交换。运行时先打开服务器端,然后

40