世博会记忆消费

时间:2017-01-23 23:14:34

标签: java swing memory jeditorpane

我做了一个阅读html文件的应用程序。我使用JEditorPane

我的问题是消耗内存,当我加载不同的文件时,内存只会增加而且永不减少:

Consumption

我不能按原样使用我的“应用程序”,很快就消耗了1g。

我从2004年开始使用setPage方法读取内容,但它已经过时了。

你有什么想法吗?

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Collections;
import java.util.Vector;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;

import org.apache.commons.lang3.StringUtils;

public class MyRP extends JFrame {

private static final long serialVersionUID = 8649207663033200521L;
GridBagConstraints c = new GridBagConstraints();
JEditorPane editorPane = new JEditorPane();

public MyRP() {

    setTitle("My RP");
    // Modifier la taille
    setSize(1500, 800);
    // Taille non modifiable par l'utilisateur
    setResizable(true);
    // Un clic sur croix ferme la fenetre
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // Centrer la fenetre par rapport à l'écran
    setLocationRelativeTo(null);

    getContentPane().setLayout(new GridBagLayout());

    c.weightx = 1.0;
    c.gridx = 1;
    c.gridy = 0;
    c.ipady = 1500;
    c.ipadx = 1200;
    c.fill = GridBagConstraints.VERTICAL;
    c.insets = new Insets(2, 0, 2, 2);

    getContentPane().add(new JScrollPane(editorPane), c);

    // Build the panel of controls
    JPanel upperPanel = new JPanel();
    upperPanel.setLayout(new BorderLayout());

    // Ajout de l'arbre hierarchique
    JTree tree = new JTree(addNodes(null, new File(".")));
    JScrollPane scrollPane = new JScrollPane(tree);
    upperPanel.add(scrollPane, "Center"); // Add the tree

    Box controlPanel = new Box(BoxLayout.Y_AXIS);

    // Add a button to install the text
    JButton bouton = new JButton("Ouvrir un fichier");
    controlPanel.add(bouton);

    upperPanel.add(controlPanel, "South");
    c.gridx = 0;
    c.gridy = 0;
    c.fill = GridBagConstraints.BOTH;
    c.ipadx = 50;
    c.weighty = 1;
    c.insets = new Insets(2, 10, 2, 2);
    getContentPane().add(upperPanel, c);

    // Add a listener to the button
    bouton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser dialogue = new JFileChooser(new File("."));
            File fichier = new File("");
            if (dialogue.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                fichier = dialogue.getSelectedFile();
            }

            if (fichier.toString().contains("MyRP")) {
                readSelectedFile(fichier);
            } else if(StringUtils.isNotBlank(fichier.toString())){
                JOptionPane.showMessageDialog(new JFrame(), "Le fichier n'est pas au bon format : MyRP - Mois - Année.html");
            }
        }
    });

    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();

            String chemin = "";
            DefaultMutableTreeNode node2 = node;
            while (node2.getParent() != null) {
                if (node2.getParent() != null) {
                    chemin = node2.getUserObject() + "\\" + chemin;
                }
                node2 = (DefaultMutableTreeNode) node2.getParent();
            }

            chemin = chemin.substring(0, chemin.length() - 1);
            if (chemin.toString().contains("MyRP")) {
                readSelectedFile(new File(System.getProperty("user.dir") + "\\" + chemin));
            } else {
                JOptionPane.showMessageDialog(new JFrame(), "Le fichier  n'est pas au bon format : MyRP - Mois - Année.html");
            }
        }
    });

}

private void readSelectedFile(File fichier) {
    try {
        //File file = new File(fichier.toString());
        //editorPane.getEditorKit().createDefaultDocument();
        //editorPane.setPage(file.toURI().toURL());
        editorPane.setPage("file:\\" + fichier.toString());
        editorPane.setContentType("text/html;charset=UTF-8");
        editorPane.setEditable(false);
    } catch (Exception exception) {
        exception.printStackTrace();
    }
}

@SuppressWarnings({ "rawtypes", "unchecked" })
DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) {
    String curPath = dir.getPath();
    DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath);
    if (curTop != null) {
        curTop.add(curDir);
    }
    Vector ol = new Vector();
    String[] tmp = dir.list();
    for (int i = 0; i < tmp.length; i++)
        ol.addElement(tmp[i]);
    Collections.sort(ol, String.CASE_INSENSITIVE_ORDER);
    File f;
    Vector files = new Vector();
    for (int i = 0; i < ol.size(); i++) {
        String thisObject = (String) ol.elementAt(i);
        String newPath;
        if (curPath.equals("."))
            newPath = thisObject;
        else
            newPath = curPath + File.separator + thisObject;
        if ((f = new File(newPath)).isDirectory())
            addNodes(curDir, f);
        else
            files.addElement(thisObject);
    }
    for (int fnum = 0; fnum < files.size(); fnum++)
        curDir.add(new DefaultMutableTreeNode(files.elementAt(fnum)));
    return curDir;
}

}

0 个答案:

没有答案