import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Vector; import javax.swing.DefaultListModel; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.JList; import javax.swing.JButton; import javax.swing.ListModel; import java.awt.Font; import javax.swing.SwingConstants; import javax.swing.JScrollBar; public class testIO extends JFrame implements ActionListener { private JPanel contentPane; private JTextField number; private JButton input; private int num; private DefaultListModel listModel,list; private Vector<Double> v; private JList fudian1; private JList fudian2; private JButton anwser; private JButton btnSave; private double a[]; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { testIO frame = new testIO(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public testIO() { v=new Vector<Double>(); setForeground(Color.GREEN); setTitle("\u6D6E\u70B9\u6570\u6392\u5E8F"); 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); JLabel lblIdhu = new JLabel("\u6D6E\u70B9\u6570\u4E2A\u6570"); lblIdhu.setBounds(57, 65, 75, 22); contentPane.add(lblIdhu); number = new JTextField(); number.setBounds(57, 125, 66, 21); contentPane.add(number); number.setColumns(10); input = new JButton("\u8F93\u5165"); input.setBounds(57, 201, 75, 23); input.addActionListener(this); contentPane.add(input); listModel = new DefaultListModel(); list = new DefaultListModel(); fudian1 = new JList(listModel); fudian1.setValueIsAdjusting(true); JScrollPane j = new JScrollPane(fudian1); j.setBounds(155, 91, 66, 133); contentPane.add(j); fudian2 = new JList(list); JScrollPane j2 = new JScrollPane(fudian2); j2.setBounds(284, 91, 66, 133); contentPane.add(j2); anwser = new JButton("\u6253\u5F00..."); anwser.setBounds(95, 10, 75, 23); anwser.addActionListener(this); contentPane.add(anwser); btnSave = new JButton("\u4FDD\u5B58.."); btnSave.setBounds(257, 10, 93, 23); btnSave.addActionListener(this); contentPane.add(btnSave); JLabel label = new JLabel("\u6392\u5E8F\u4E4B\u524D"); label.setBounds(156, 69, 54, 15); contentPane.add(label); JLabel label_1 = new JLabel("\u6392\u5E8F\u4E4B\u540E"); label_1.setBounds(284, 69, 54, 15); contentPane.add(label_1); JLabel lblTip = new JLabel("tip\uFF1A\u8BF7\u5148\u8F93\u5165\u6D6E\u70B9\u6570\u4E2A\u6570\uFF0C\u6216\u8005\u6253\u5F00\u6587\u4EF6"); lblTip.setVerticalAlignment(SwingConstants.TOP); lblTip.setFont(new Font("微软雅黑", Font.PLAIN, 12)); lblTip.setBounds(95, 234, 300, 18); contentPane.add(lblTip); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==input){ listModel.removeAllElements(); String s = number.getText(); num = Integer.parseInt(s); for(int i=1;i<=num;i++){ String name="请输入第"+i+"个数"; String ss= JOptionPane.showInputDialog(name); listModel.addElement(Double.parseDouble(ss)); v.add(Double.parseDouble(ss)); } } if(e.getSource()==btnSave){ save(); } if(e.getSource()==anwser){ v.removeAllElements(); String name="请输入文档路径 'c:\txt' "; String ss= JOptionPane.showInputDialog(name); File file = new File(ss); try { FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); try { String s, s2 = new String(); while ((s = br.readLine()) != null) { s2 += s + "\n "; v.add(Double.parseDouble(s)); } br.close(); System.out.println(s2); System.out.println("OK?"); for(int i=0;i<v.size();i++){ System.out.println(v.elementAt(i)); } } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } int i,j; double temp; num=v.size(); String g = ""+num; number.setText(g); a=new double[num]; listModel.removeAllElements(); for(i=0;i<num;i++){ a[i]=v.elementAt(i); listModel.addElement(a[i]); } for(i=0;i<num-1;i++) for(j=i+1;j<num;j++) if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } v.removeAllElements(); for(i=num-1;i>=0;i--){ System.out.println(a[i]); list.addElement(a[i]); v.addElement(a[i]); } try { FileWriter writer = new FileWriter(ss, true); for(i=0;i<v.size();i++){ double k=v.elementAt(i); String sss = k+"\r\n"; writer.write(sss); System.out.println(k); } writer.close(); } catch (IOException e1) { e1.printStackTrace(); } /* PrintWriter pw = null; try { pw = new PrintWriter(new FileWriter(ss)); for(i=0;i<v.size();i++){ double k=v.elementAt(i); pw.print(k+"\r\n"); System.out.println(k); } } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); }finally { if(pw != null) { pw.close(); pw = null; } } */ } } public void save(){ String name="请输入保存文档路径 'c:\txt' "; String ss= JOptionPane.showInputDialog(name); PrintWriter pw = null; try { pw = new PrintWriter(new FileWriter(ss)); for(int i=0;i<v.size();i++){ double k=v.elementAt(i); pw.print(k+"\r\n"); System.out.println(k); } } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); }finally { if(pw != null) { pw.close(); pw = null; } } } }