简体   繁体   中英

Getting my Word Processor GUI to Open a File

I am trying to make a simple word processor that has an open, save, and delete button. So far I have gotten the save button to work! My open button, on the other hand, will not open a file. Whenever I click the button nothing happens. Please help! Here is the code for my open class:

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

    }

}
}

Also, here is my Display Class that calls the open class:

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

Why are you opening an RTF file and using a HTMLEditorKit to parse it? This is a set-up for failure.

It would seem to me that you should use

  1. an RTF editor kit, not one geared towards HTML text, for your purposes.
  2. You're ignoring all catch blocks, which you shouldn't. At least print the exception's stacktrace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM