该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.filechooser.*; public class RarAnnotate extends JFrame implements ActionListener{
private JTextArea annotateArea;
private JTextField rarFileField;
final JButton browseButton = new JButton();
File rarFile;
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
RarAnnotate frame = new RarAnnotate();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public RarAnnotate() {
super();
getContentPane().setLayout(null);
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JLabel label = new JLabel();
label.setText("选择RAR文档:");
label.setBounds(10, 10, 89, 18);
getContentPane().add(label);
final JLabel label_1 = new JLabel();
label_1.setText("注释文档:");
label_1.setBounds(10, 53, 66, 18);
getContentPane().add(label_1);
rarFileField = new JTextField();
rarFileField.setBounds(105, 8, 311, 22);
getContentPane().add(rarFileField);
browseButton.setText("浏览");
browseButton.setBounds(422, 10, 60, 18);
getContentPane().add(browseButton);
browseButton.addActionListener(this);
annotateArea = new JTextArea();
annotateArea.setBounds(10, 77, 472, 209);
getContentPane().add(annotateArea);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==browseButton){
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new FileNameExtensionFilter("RAR 文档","rar"));
chooser.setAcceptAllFileFilterUsed(false);
int option = chooser.showOpenDialog(this);
if(option != JFileChooser.APPROVE_OPTION)
return;
rarFile = chooser.getSelectedFile();
rarFileField.setText(rarFile.toString());
try {
File tempFile = File.createTempFile("rar", ".txt");
Process process = Runtime.getRuntime().exec(
"rar cw\""+rarFile+"\"\""+tempFile+"\"-y");
process.getOutputStream().close();
Scanner sc = new Scanner(process.getInputStream());
while(sc.hasNext()){
sc.nextLine();
}
process.getInputStream().close();
annotateArea.setText("");
Scanner scan = new Scanner(tempFile);
while(scan.hasNext()){
annotateArea.append(scan.next()+"\n");
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}