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

标签 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!");

    }

}
}

此外,这是调用公开类的我的显示类:

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;
}
}   

最佳答案

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

在我看来你应该使用

  1. 一个 RTF 编辑器工具包,而不是一个针对 HTML 文本的工具包,供您使用。
  2. 您忽略了所有不应该的 catch block 。至少打印异常的堆栈跟踪。

关于java - 让我的文字处理器 GUI 打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36538915/

相关文章:

java - 自定义 JLabel 图标

algorithm - 如何检测文件是否已更改?

java - 如何在没有 JFileChooser 的情况下在 Java Swing 中打开文件

java - 在小程序中选择自定义颜色

macros - LibreOffice 4.1 编写器 : macro to adjust column widths in tables

java - 移动系统覆盖元素

java - 删除后元素的 ID 会怎样?

java - Intellij 对声明不兼容 'Object' 方法的接口(interface)的检查

java - 在数组中添加连续的整数对的分而治之算法有问题