gpt4 book ai didi

java - 如何为 JTextPane 设置默认背景色

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:16:32 25 4
gpt4 key购买 nike

我已经在网上搜索过,并尝试了几种方法,试图为 JTextPane 设置默认背景色,但它仍然显示默认的白色。

我正在尝试模拟控制台输出,我需要整个背景黑色,即使没有文字。

似乎 setCharacterAttributes()setParagraphAttributes() 只处理任何插入的文本,但背景的其余部分仍然是默认的白色。

我看到了一些关于设置背景颜色的错误。

我该怎么做?

它是纯文本,不是任何 HTML。

谢谢!

更新:

我终于找到了有用的东西。

使用 setBackground(Color.BLACK) 只会设置背景在任何插入的文本下,但 JTextPane 背景的其余部分仍然存在默认的白色,在我的 Windows 机器上。

我开始考虑更改 UIDefault,然后就成功了!这是我使用的:

UIDefaults defs = UIManager.getDefaults();
defs.put("TextPane.background", new ColorUIResource(Color.BLACK));
defs.put("TextPane.inactiveBackground", new ColorUIResource(Color.BLACK));

当它开始时,没有文本,整个 JTextPane 现在是黑色的,就像我想要的那样任何插入的文本都是我需要的方式。

我尝试的所有其他方法都让 JTextPane 的其余部分变白,我尝试了许多不同的“解决方案”。

感谢您的回复。

最佳答案

试试这个 SSCCE。它演示了在 JTextPane 上设置背景颜色。

import java.awt.Component;
import java.awt.Container;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

/**
* http://stackoverflow.com/questions/19435181/how-to-set-default-background-color-for-jtextpane
*/
public class Q19435181 {
public static void main(String... args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Example setting background color on JTextPane");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container pane = frame.getContentPane();
pane.add(blackJTextPane());
frame.setSize(800, 600);
frame.setVisible(true);
}

private Component blackJTextPane() {
JTextPane pane = new JTextPane();
pane.setBackground(Color.BLACK);
pane.setForeground(Color.WHITE);
pane.setText("Here is example text");
return pane;
}
});
}
}

关于java - 如何为 JTextPane 设置默认背景色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19435181/

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