📄 jmessagesave.java
字号:
import java.io.*;import javax.swing.*;import javax.swing.filechooser.FileFilter;public class JMessageSave{ private JFileChooser fileChooser; private JPrivateMessage frame; private JEditorPane txtDisplay; private File file = null; private int result; // Constructor public JMessageSave(JPrivateMessage frame, JEditorPane txtDisplay) { this.frame = frame; this.txtDisplay = txtDisplay; fileChooser = new JFileChooser(); fileChooser.setDialogTitle("Save Conversation"); fileChooser.setApproveButtonText("Save"); fileChooser.addChoosableFileFilter(new txtFileFilter("html")); // class txtFileFilter will be defined below this.saveDialog(); // Function to active SAVE } public void saveDialog() { FileOutputStream fileout = null; result = fileChooser.showSaveDialog(frame); if (result == JFileChooser.APPROVE_OPTION) { file = fileChooser.getSelectedFile(); } if (file != null) { try { fileout = new FileOutputStream(file); // establish output stream } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(null, "File not existed", "File Error", JOptionPane.ERROR_MESSAGE); return; } // if output stream is ok, then grap the text from the txtDisplay String txtContent = txtDisplay.getText(); try { fileout.write(txtContent.getBytes()); // specify the size writing into the stream } catch (IOException ex) { JOptionPane.showMessageDialog(null, "Error in writing file", "File Error", JOptionPane.ERROR_MESSAGE); } // The following code must be executed finally { try { if (fileout != null) fileout.close(); } catch (IOException ex) { JOptionPane.showMessageDialog(null, "Error!", "File Error", JOptionPane.ERROR_MESSAGE); } } } }}class txtFileFilter extends FileFilter{ String extend; // Constructor public txtFileFilter(String extend) // input "txt" here { this.extend = extend; // the pre-set extension for the files ( _____.txt ) } public boolean accept(File file) { if (file.isDirectory()) return true; String fileName = file.getName(); int index = fileName.lastIndexOf('.'); if ((index>0) && (index<fileName.length()-1)) { String in_extend = fileName.substring(index+1).toLowerCase(); // extract the extension of the input file if (in_extend.equals(extend)) return true; } return false; } public String getDescription() { return "HTML (*.html)"; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -