java实验报告(图形界面编程及文件读写)

《Java程序设计》实 验 报 告(三) 所使用的工具软件及环境:
环境:Windows 8.1; jdk 1.8;
工具:eclipse
实验目的:
1、掌握图形界面编程及输入输出流类。
2、了解Java 系统包的结构。
3、掌握不同类型的输入输出流类,标准数据流、文件流、数据输入输出流、对象流等。
实验内容:
编写图形界面程序,接受用户输入的5个浮点数据和一个文件目录名,将这5个数据保存在该文件中,再从文件中读取出来并且进行从大到小排序,然后再一次追加保存在该文件中。
源程序:
import java.awt.BorderLayout;
import java.util.Arrays;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.awt.event.ActionEvent;
public class simple extends JFrame {
private JPanel contentPane;
private JTextField textField;
private final JTextField textField_1 = new JTextField();
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;

/** * Launch the application. */public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { simple frame = new simple(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */public static boolean isNumber(String str){ String reg = "^[0-9]+(.[0-9]+)?$"; return str.matches(reg); }public static boolean isNumeric(String str){ for (int i = str.length(); --i>=0; ){ if (!Character.isDigit(str.charAt(i))){ return false; } } return true; } public int count=0; private JButton btnNewButton_2; public simple() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); textField = new JTextField(); textField.setBounds(22, 24, 66, 21); contentPane.add(textField); textField.setColumns(10); textField_1.setBounds(97, 24, 66, 21); contentPane.add(textField_1); textField_1.setColumns(10); textField_2 = new JTextField(); textField_2.setBounds(173, 24, 66, 21); contentPane.add(textField_2); textField_2.setColumns(10); textField_3 = new JTextField(); textField_3.setBounds(257, 24, 66, 21); contentPane.add(textField_3); textField_3.setColumns(10); textField_4 = new JTextField(); textField_4.setBounds(345, 24, 66, 21); contentPane.add(textField_4); textField_4.setColumns(10); final double[] arr1=new double[5]; JButton btnNewButton = new JButton("\u8F93\u5165"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String s1=textField.getText(); String s3=textField_1.getText(); String s4=textField_2.getText(); String s5=textField_3.getText(); String s6=textField_4.getText(); if(isNumber(s1)==true) { arr1[0]=Double.parseDouble(s1); textField.setText(""); } else textField.setText(""); if(isNumber(s3)==true) { arr1[1]=Double.parseDouble(s3); textField_1.setText(""); } else textField_1.setText(""); if(isNumber(s4)==true) { arr1[2]=Double.parseDouble(s4); textField_2.setText(""); } else textField_2.setText(""); if(isNumber(s5)==true) { arr1[3]=Double.parseDouble(s5); textField_3.setText(""); } else textField_3.setText(""); if(isNumber(s6)==true) { arr1[4]=Double.parseDouble(s6); textField_4.setText(""); } else textField_4.setText(""); //五次之后按钮失效} }); btnNewButton.setBounds(142, 55, 97, 23); contentPane.add(btnNewButton); textField_5 = new JTextField(); textField_5.setBounds(22, 101, 107, 21); contentPane.add(textField_5); textField_5.setColumns(10); JButton btnNewButton_1 = new JButton("\u4FDD\u5B58\u6587\u4EF6"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { String s2=textField_5.getText(); //label_2.setText(s2); File outFile=new File(s2); FileWriter fos=new FileWriter(outFile); for(int c=0; c=0; d--) { fos.write(String.valueOf(arr2[d])+"\r\n"); } fos.close(); br.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (NumberFormatException e1) {e1.printStackTrace(); } catch (IOException e1) {e1.printStackTrace(); }} }); btnNewButton_2.setBounds(122, 168, 97, 23); contentPane.add(btnNewButton_2); }

【java实验报告(图形界面编程及文件读写)】}

    推荐阅读