HTML-JScrollPane作为日志(带时间戳的滚动文本)

时间:2015-01-23 20:42:15

标签: java html swing logging jeditorpane

我希望你能帮助我。我尝试制作一个“日志”,告知用户程序进度。 没有HTML就没问题了:

package view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class LogComponent extends JPanel {
    protected JTextArea textArea;
    private final static String newline = "\n";

    public LogComponent() {
        super(new GridBagLayout());

        textArea = new JTextArea(5, 20);
        textArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(textArea);

        GridBagConstraints c = new GridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;

        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        c.weighty = 1.0;
        add(scrollPane, c);
    }
    public void setEntry(String entry) {
        SimpleDateFormat s = new SimpleDateFormat("HH:mm:ss");
        Date d = new Date();

        textArea.append("[" + s.format(d) + "] " + entry + newline);
        textArea.setCaretPosition(textArea.getDocument().getLength());
    }
}

但是我想在日志部分中使用颜色。所以我为JEditPane更改了textArea,以便能够使用HTML。 这有点工作,我能够设置,但我不知道,如何保留旧的东西......如果我试图保留旧条目,那么不会出现新的条目:

package view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLEditorKit;

public class HtmlLogComponent extends JPanel {
    protected JEditorPane editor;

    public HtmlLogComponent() {
        super(new GridBagLayout());

        editor = new JEditorPane();
        editor.setEditable(false);

        JScrollPane scrollPane = new JScrollPane(editor);

        HTMLEditorKit kit = new HTMLEditorKit();
        editor.setEditorKit(kit);

        Document doc = kit.createDefaultDocument();
        editor.setDocument(doc);

        GridBagConstraints c = new GridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;

        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        c.weighty = 1.0;
        add(scrollPane, c);

    }

    public void setEntry(String entry) {
        SimpleDateFormat s = new SimpleDateFormat("HH:mm:ss");
        Date d = new Date();


        editor.setText("[" + s.format(d) + "] " + entry);
        System.out.println(editor.getText());
    }

}

你有什么想法:)?

亲切的问候 ACA

1 个答案:

答案 0 :(得分:0)

它不是一个非常“干净”的解决方案,但它运作得很好; D ......这是我的班级:

package view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.DefaultListModel;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;


public class HtmlLogComponent extends JPanel {
    protected DefaultListModel model = new DefaultListModel();
    protected JList list;
    public static int maxLen = 110;

    public HtmlLogComponent(int maxLen) {
        super(new GridBagLayout());
        this.maxLen = maxLen;

        list = new JList(model);

        JScrollPane scrollPane = new JScrollPane(list);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        list.setSelectionModel(new DisabledItemSelectionModel());

        GridBagConstraints c = new GridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;

        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        c.weighty = 1.0;
        add(scrollPane, c);

    }

    public void setEntry(String entry) {
        SimpleDateFormat s = new SimpleDateFormat("HH:mm:ss");
        Date d = new Date();
        Integer lenght = entry.length();
        String string = "<html>[" + s.format(d) + "] ";
        String substring = entry;

        if (lenght >= maxLen) { // Linebreak needed
            while (lenght >= maxLen) {              
                for (int i=maxLen - 1; i> 0; i--) {
                    if (substring.charAt(i) == ' ') {
                        string += substring.substring(0, i) + "</html>";
                        model.addElement(string);
                        substring = substring.substring(i, lenght);
                        System.out.println(substring);
                        break;
                    }
                }
            lenght = substring.length();
            string = "<html>";          
            }
            model.addElement("<html>" + substring  + "</html>");
        } else {
            model.addElement("<html>[" + s.format(d) + "] " + entry + "</html>");
        }

        int lastIndex = list.getModel().getSize() - 1;
        if (lastIndex >= 0) list.ensureIndexIsVisible(lastIndex);
    }

    class DisabledItemSelectionModel extends DefaultListSelectionModel {

        @Override
        public void setSelectionInterval(int index0, int index1) {
            super.setSelectionInterval(-1, -1);
        }
    }

}

感谢MadProgrammer帮助我:)!