让我的字处理器GUI打开文件

时间:2016-04-11 02:32:26

标签: java swing word-processor

我正在尝试制作一个具有打开,保存和删除按钮的简单文字处理器。到目前为止,我已经获得了保存按钮!另一方面,我的打开按钮不会打开文件。每当我点击按钮时都没有任何反应。请帮忙!这是我的公开课的代码:

    package Word_Processor.src;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JTextPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.rtf.RTFEditorKit;

public class OpenFile {

public void open(JTextPane text) {
    JFileChooser chooser = new JFileChooser();
    chooser.setMultiSelectionEnabled(false);
    FileNameExtensionFilter filter = new FileNameExtensionFilter("RICH TEXT FORMAT", "rtf", "rtf");
    chooser.setFileFilter(filter);

    int option = chooser.showOpenDialog(null);
    String filePath = chooser.getSelectedFile().getPath();

    if (option == JFileChooser.APPROVE_OPTION) {
        StyledDocument doc = (StyledDocument) text.getDocument();
        RTFEditorKit kit = new RTFEditorKit();
        BufferedInputStream in;

        try {
            in = new BufferedInputStream(new FileInputStream(filePath));
            kit.read(in, doc, 1);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {e.printStackTrace();
        } catch (BadLocationException e) {e.printStackTrace();
        }
    } else {
        System.out.println("Open cancelled!");

    }

}
}

此外,这是我的Display Class调用open类:

package Word_Processor.src;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextPane;

public class Display extends JPanel implements ActionListener {
private JTextPane textArea;
private JButton saveButton;
private JComboBox colorCombo;
private JComboBox fontCombo;
private JLabel processorLabel;
private JSlider fontSize;
private JButton openButton;
private JButton deleteButton;

// Create some method objects
SaveContent saveFile = new SaveContent();
ColorManagement colorClass = new ColorManagement();
FontManagement fontClass = new FontManagement();
Main main = new Main();
OpenFile openFile = new OpenFile();

// Create some arrays
String[] colorItems =     { "Red", "Blue", "Green", "Purple", "Orange", "Black" };
String[] fontItems = { "Monospaced", "Serif", "Sans Serif", "Arial" };

public Display() {
    init();
}

public void init() {
    Font font;
    Color color;

    color = colorClass.getColor();
    font = fontClass.getFont();

    // Construct Components
    textArea = new JTextPane();
    saveButton = new JButton("Save");
    openButton = new JButton("Open");
    deleteButton = new JButton("Delete");
    colorCombo = new JComboBox(colorItems);
    fontCombo = new JComboBox(fontItems);
    processorLabel = new JLabel("Word Processor");
    fontSize = new JSlider(10, 30);

    // Work with slider
    fontSize.setOrientation(JSlider.HORIZONTAL);
    fontSize.setMinorTickSpacing(1);
    fontSize.setMajorTickSpacing(5);
    fontSize.setPaintTicks(true);
    fontSize.setPaintLabels(true);

    // Make the text area look presentable
    textArea.setBackground(Color.LIGHT_GRAY);
    // textArea.setForeground(color);

    // Adjust size and layout
    setPreferredSize(new Dimension(817, 473));
    setLayout(null);

    // Add components
    add(textArea);
    add(saveButton);
    add(colorCombo);
    add(fontCombo);
    add(processorLabel);
    add(fontSize);
    add(deleteButton);
    add(openButton);

    // set boundaries
    textArea.setBounds(10, 30, 650, 450);
    saveButton.setBounds(670, 395, 140, 35);
    openButton.setBounds(670, 350, 140, 35);
    deleteButton.setBounds(670, 305, 140, 35);
    colorCombo.setBounds(670, 205, 140, 35);
    fontCombo.setBounds(670, 150, 140, 35);
    processorLabel.setBounds(670, 20, 140, 35);
    fontSize.setBounds(670, 95, 140, 49);

    // add action listeners
    saveButton.addActionListener(this);
    colorCombo.addActionListener(this);
    fontCombo.addActionListener(this);
    deleteButton.addActionListener(this);
    openButton.addActionListener(this);


    }

    public void actionPerformed(ActionEvent e) {
    if (e.getSource() == saveButton) {
        saveFile.save(textArea);
    }
    if (e.getSource() == openButton) {
        openFile.open(textArea);
    }
    if (e.getSource() == colorCombo) {
        colorClass.selectColor(colorCombo.getSelectedItem().toString());
        textArea.setForeground(colorClass.getColor());
    }
    if (e.getSource() == fontCombo) {
        fontClass.selectFont(fontCombo.getSelectedItem().toString(), fontSize.getValue());
        textArea.setFont(fontClass.getFont());
    }

    }

    public JTextPane getText() {
    return textArea;
}
}   

1 个答案:

答案 0 :(得分:3)

为什么要打开RTF文件并使用HTMLEditorKit来解析它?这是一个失败的设置。

在我看来,你应该使用

  1. 一个RTF编辑器工具包,而不是一个面向HTML文本的工具包。
  2. 你忽略了所有的挡块,你不应该这样做。至少打印异常的堆栈跟踪。