gpt4 book ai didi

Java JScrollpane - JTextPane - Log4J AppenderSkeleton 水平滚动问题

转载 作者:行者123 更新时间:2023-12-01 17:16:40 35 4
gpt4 key购买 nike

我有一个带有 JTextPane 的 swing gui,用于从底层应用程序输出日志。我的问题是,在初始化 gui 并记录第一个日志条目后,滚动条将“禁用”,直到我触发操作。下一个日志条目后,水平滚动条“出现”。

第二个问题是有时日志语句的长度是1030个字符。但水平滚动最大为 606 个字符。如果我从 JTextPane 复制文本,我会得到洞(1030char)文本。

我尝试在 MainFrame 中 logTxtArea.setText("... -> 1030 chars..."); <<- 滚动条按预期工作,并且还会显示日志语句。

这是我在大型机中的 JTestPane 初始化代码

private static JTextPane logTxtArea = new JTextPane();
......

logTxtArea.setEditable(false);
logTxtArea.setEditorKit(new NoWrapEditorKit());
JScrollPane logScroller = new JScrollPane(logTxtArea);
logScroller.setFont(new Font("Segoe UI", Font.PLAIN, 12));
logScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
logScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.SOUTHWEST;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = 1;
gbc.gridheight = 8;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(10, 15, 10, 5);
gbc.weightx = 1;
gbc.weighty = 1;
main.add(logScroller, gbc);

还有我的 NoWrapEditorKit ->

public class TextAreaAppender extends AppenderSkeleton {

private static Style style;

protected void append(LoggingEvent event) {
this.layout = new PatternLayout("%d - %m%n");
String msg = this.layout.format(event);
JTextPane textArea = MainFrame.getLogTxtArea();
Color color = Color.BLACK;

if (event.getLevel() == Level.INFO) {
color = Color.BLUE;
} else if (event.getLevel() == Level.WARN || event.getLevel() == Level.ERROR || event.getLevel() == Level.FATAL) {
color = Color.RED;
}

if (msg.contains("INFO:")) {
color = Color.MAGENTA;
}

appendToPane(textArea, msg, color);
MainFrame.getLogTxtArea().setCaretPosition(MainFrame.getLogTxtArea().getDocument().getLength());
}

public void close() {
}

public boolean requiresLayout() {
return false;
}

private void appendToPane(JTextPane tp, String msg, Color c) {
int len = tp.getDocument().getLength();
if (style == null) {
style = tp.addStyle("", null);
}

try {
StyleConstants.setForeground(style, c);
tp.getStyledDocument().insertString(len, msg, style);
} catch (BadLocationException e) {
e.printStackTrace();
}
}
}

所以目标是每行获取一条没有换行符的日志语句。此外,很长的语句(1030 个字符)应该完整显示。

谢谢!

更新

这是一个简短的示例:

public class TextFrame extends JFrame {

private JTextPane txtPane;
private Style style;

public TextFrame() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(true);
this.setMinimumSize(new Dimension(600, 400));
this.setPreferredSize(new Dimension(900, 600));

txtPane = new JTextPane()
{
public boolean getScrollableTracksViewportWidth()
{
return getUI().getPreferredSize(this).width
<= getParent().getSize().width;
}
};
txtPane.setEditable(false);
JScrollPane logScroller = new JScrollPane(txtPane);
logScroller.setFont(new Font("Segoe UI", Font.PLAIN, 12));
logScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
logScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
this.add(logScroller);

append("Short text" + System.lineSeparator(), Color.BLACK);
append("1030 char text - 1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz12345" + System.lineSeparator(), Color.BLACK);
append("2000 char text - 1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz123" + System.lineSeparator(), Color.BLACK);

this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}

private void append(String message, Color color) {

int len = txtPane.getDocument().getLength();
if (style == null) {
style = txtPane.addStyle("", null);
}

try {
StyleConstants.setForeground(style, color);
txtPane.getStyledDocument().insertString(len, message, style);
} catch (BadLocationException e) {
e.printStackTrace();
}

//scroll to bottom
txtPane.setCaretPosition(txtPane.getDocument().getLength());
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
new TextFrame();
}
});
}

}

最佳答案

让它工作。解决方案是将 JTextPane 添加到 JPanel。将 JPanel 的布局设置为 BoarderLayout。我发现滚动速度卡住了,所以我设置了滚动速度。

这是最终的解决方案:

public class TextFrame extends JFrame {

private JTextPane txtPane = new JTextPane();
private Style style;

public TextFrame() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(true);
this.setMinimumSize(new Dimension(600, 400));
this.setPreferredSize(new Dimension(900, 600));


txtPane.setFont(new Font("Segoe UI", Font.PLAIN, 20));
txtPane.setEditable(false);

JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(txtPane);

JScrollPane logScroller = new JScrollPane(panel);
logScroller.getVerticalScrollBar().setUnitIncrement(16);
logScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
logScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
this.add(logScroller);

//txtPane.setText("TEST text in this line..");
append("Short text" + System.lineSeparator(), Color.BLACK);
append("1030 char text - 1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz12345" + System.lineSeparator(), Color.BLACK);
append("2000 char text - 1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz123" + System.lineSeparator(), Color.BLACK);

this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}

private void append(String message, Color color) {

int len = txtPane.getDocument().getLength();
if (style == null) {
style = txtPane.addStyle("", null);
}

try {
StyleConstants.setForeground(style, color);
txtPane.getStyledDocument().insertString(len, message, style);
} catch (BadLocationException e) {
e.printStackTrace();
}

//scroll to bottom
//txtPane.setCaretPosition(txtPane.getDocument().getLength());
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
new TextFrame();
}
});
}

}

关于Java JScrollpane - JTextPane - Log4J AppenderSkeleton 水平滚动问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61374072/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com