gpt4 book ai didi

java - 如何更改 JScrollPane 内容的颜色?

转载 作者:行者123 更新时间:2023-12-02 02:51:35 25 4
gpt4 key购买 nike

我想用更改的文本颜色、文本框中的背景和自己的字体来制作 JScrollPane,但我的实现不起作用 - 我已经看到了 JScrollPane 的默认形式(白色背景,标准黑色字体)。谁能告诉我为什么它不起作用以及如何修复它?

public class TextField extends JFrame
{
public TextField()
{
JScrollPane scroll = new JScrollPane(new JTextArea(15, 45));
scroll.setPreferredSize(new Dimension(500, 300));
scroll.getViewport().setBackground(Color.BLUE);
scroll.getViewport().setForeground(Color.YELLOW);

Font font = new Font("Dialog", Font.BOLD + Font.ITALIC, 14);
scroll.getViewport().setFont(font);
add(scroll);
pack();
}
}

最佳答案

您想要自定义的实际 View Component 是通过 scroll.getViewport().getView() 获取的,而不是 scroll.getViewport() > .

public class TextField extends JFrame
{
public TextField()
{
JScrollPane scroll = new JScrollPane(new JTextArea(15, 45));
scroll.setPreferredSize(new Dimension(500, 300));
scroll.getViewport().getView().setBackground(Color.BLUE);
scroll.getViewport().getView().setForeground(Color.YELLOW);

Font font = new Font("Dialog", Font.BOLD + Font.ITALIC, 14);
scroll.getViewport().getView().setFont(font);
add(scroll);
pack();
}
}

关于java - 如何更改 JScrollPane 内容的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43762972/

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