我正在创建一个简单的Wordpad编辑器应用程序。我正在使用JTextPane。我已添加代码以使用RTFEditorKit读取“ .rtf”文件。初始化代码:
RTFEditorKit rtfKit = new RTFEditorKit();
JTextPane textPane = new JTextPane();
textPane.setEditorKit(rtfKit);
现在,我需要使用“ RTFEditorKit”将纯文本文件“ .txt”读取到同一JTextPane中,以便可以在同一应用程序中查看纯文本文件和rtf文件。我该如何实现?
我的应用程序最小代码:
import static java.awt.event.InputEvent.CTRL_DOWN_MASK;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.Document;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
import javax.swing.text.rtf.RTFEditorKit;
public class MyNotepad implements ActionListener {
public JFrame frame;
public JPanel panel;
public JTextPane textPane;
public RTFEditorKit rtf;
public StyleContext styleContext;
public Document document;
public JScrollPane scrollPane;
public JMenuBar menuBar;
public JMenu fileMenu;
public JMenuItem newSubMenu;
public JMenuItem openSubMenu;
public JFileChooser fc;
public boolean openFileExtFlag = true;
public boolean saveFileExtFlag = true;
public File openFile;
public File saveFile;
public boolean saveWindowTitle = false;
public boolean openFileFlag;
public boolean saveFileFlag;
public boolean saved = true;
public boolean dontSaveOption;
public BufferedReader br;
public boolean saveForNewOpenExitListener;
public boolean saveAsFlag;
public int returnVal;
public String filePath;
public boolean flagForOpenListener;
public StyledDocument styledDocument;
public Style defaultStyle;
public MutableAttributeSet mas;
public MyNotepad() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
frame = new JFrame("My Notepad");
panel = new JPanel(new BorderLayout());
rtf = new RTFEditorKit();
textPane = new JTextPane();
textPane.setEditorKit(rtf);
textPane.setMargin(new Insets(10,5,5,5));
styleContext = new StyleContext();
mas = textPane.getInputAttributes();
styledDocument = textPane.getStyledDocument();
textPane.setDocument(styledDocument);
scrollPane = new JScrollPane();
scrollPane.getViewport().add(textPane);
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
newSubMenu = new JMenuItem("New");
newSubMenu.setAccelerator(KeyStroke.getKeyStroke('N', CTRL_DOWN_MASK));
openSubMenu = new JMenuItem("Open...");
openSubMenu.setAccelerator(KeyStroke.getKeyStroke('O', CTRL_DOWN_MASK));
defaultStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
fc= new JFileChooser();
openSubMenu.addActionListener(this);
newSubMenu.addActionListener(this);
scrollPane.setPreferredSize(new Dimension(700,500));
fileMenu.add(newSubMenu);
fileMenu.add(openSubMenu);
menuBar.add(fileMenu);
textPane.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(scrollPane, BorderLayout.CENTER);
panel.add(new JLabel(" "), BorderLayout.EAST);
panel.add(new JLabel(" "), BorderLayout.WEST);
panel.add(new JLabel(" "), BorderLayout.SOUTH);
frame.setJMenuBar(menuBar);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(900,200));
frame.pack();
textPane.requestFocus();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
new MyNotepad();
}
public void actionPerformed(ActionEvent ae) {
if ((ae.getSource() == openSubMenu)) {
openActionListener();
}
}
public boolean openActionListener() {
if (openFileExtFlag && saveFileExtFlag) {
fc.setAcceptAllFileFilterUsed(false);
fc.addChoosableFileFilter(new FileNameExtensionFilter("Rich Text File (*.rtf)", "rtf"));
fc.addChoosableFileFilter(new FileNameExtensionFilter("Text File (*.txt)", "txt"));
openFileExtFlag = false;
}
do {
returnVal = fc.showOpenDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
openFile = fc.getSelectedFile();
filePath = openFile.getPath();
if (openFile.exists()) {
break;
}
JOptionPane.showMessageDialog(frame, "File not found, please verify the file name and the path", "Cannot open", JOptionPane.OK_OPTION);
} else {
return false;
}
} while (true);
try {
System.out.println("---opening document...");
textPane.setText("");
InputStream in = new FileInputStream(filePath);
System.out.println("Opening file - " + filePath);
if (filePath.endsWith(".rtf")) {
rtf.read(in, textPane.getDocument(), 0);
in.close();
} else if (filePath.endsWith(".txt")) {
textPane = new JTextPane();
FileReader fileReader = new FileReader(filePath);
textPane.read(fileReader, openFile);
fileReader.close();
}
textPane.requestFocus();
textPane.setCaretPosition(0);
frame.setTitle("My Notepad " + "- " + filePath);
System.out.println("----File opened successfully");
} catch (Exception e) {
JOptionPane.showMessageDialog(frame, "Cannot open, Invalid RTF file", "Cannot open", JOptionPane.OK_OPTION);
}
return true;
}
}
答案 0 :(得分:1)
textPane = new JTextPane();
FileReader fileReader = new FileReader(filePath);
textPane.read(fileReader, openFile);
fileReader.close();
您正在创建一个永远不会添加到UI的新JTextPane,并且将文本添加到此textPane中,而不是您在UI中看到的文本中。
只需删除以下行:textPane = new JTextPane();
对不起,我迟到了。