HTML-JScrollPane as Log(带时间戳的滚动文本)
HTML-JScrollPane as Log (scrolling text with timestamp)
希望你能帮助我。我尝试做一个"Log",通知用户程序的进展。
没有 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());
}
}
你有什么想法吗:)?
亲切的问候
阿卡
这不是一个非常 "clean" 的解决方案,但效果很好 ;D...这是我的 class:
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 在这里帮助我 :)!
希望你能帮助我。我尝试做一个"Log",通知用户程序的进展。 没有 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());
}
}
你有什么想法吗:)?
亲切的问候 阿卡
这不是一个非常 "clean" 的解决方案,但效果很好 ;D...这是我的 class:
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 在这里帮助我 :)!