JTextPane/JScrollPane 显示问题
JTextPane/JScrollPane display issue
我正在尝试用 JScrollPane 包装 JTextPane,我需要背景是透明的。
这是我的代码:
public CharPanel(){
super.setLayout(null);
JButton LButton = new JButton("<");
LButton.setBounds(0, 300, 50, 50);
super.add(LButton);
JButton RButton = new JButton(">");
RButton.setBounds(950, 300, 50, 50);
super.add(RButton);
JTextPane Bio = new JTextPane();
Bio.setBackground(new Color(0, 0, 0, 0));
JScrollPane BioScroll = new JScrollPane(Bio);
BioScroll.setBackground(new Color(0, 0, 0, 0));
BioScroll.setBounds(0, 500, 1000, 150);
super.add(BioScroll);
}
正常情况下是这样的:https://gyazo.com/db7e4d2a5668b36ffc617cefb1423fc4
这是我输入字符后的样子:https://gyazo.com/1509d952005195d6e14365f0ae2da69a
看到奇怪的视觉错误了吗?
Bio.setBackground(new Color(0, 0, 0, 0));
不要使用带有 alpha 值的颜色。这违反了 Swing 绘制规则,Swing 不知道如何正确绘制组件,因此您会得到绘制工件。
要完全透明,只需使用:
component.setOpaque( false );
如果您需要使用部分透明度,请查看 Backgrounds With Transparency 了解更多相关信息和解决方案。
我正在尝试用 JScrollPane 包装 JTextPane,我需要背景是透明的。
这是我的代码:
public CharPanel(){
super.setLayout(null);
JButton LButton = new JButton("<");
LButton.setBounds(0, 300, 50, 50);
super.add(LButton);
JButton RButton = new JButton(">");
RButton.setBounds(950, 300, 50, 50);
super.add(RButton);
JTextPane Bio = new JTextPane();
Bio.setBackground(new Color(0, 0, 0, 0));
JScrollPane BioScroll = new JScrollPane(Bio);
BioScroll.setBackground(new Color(0, 0, 0, 0));
BioScroll.setBounds(0, 500, 1000, 150);
super.add(BioScroll);
}
正常情况下是这样的:https://gyazo.com/db7e4d2a5668b36ffc617cefb1423fc4
这是我输入字符后的样子:https://gyazo.com/1509d952005195d6e14365f0ae2da69a
看到奇怪的视觉错误了吗?
Bio.setBackground(new Color(0, 0, 0, 0));
不要使用带有 alpha 值的颜色。这违反了 Swing 绘制规则,Swing 不知道如何正确绘制组件,因此您会得到绘制工件。
要完全透明,只需使用:
component.setOpaque( false );
如果您需要使用部分透明度,请查看 Backgrounds With Transparency 了解更多相关信息和解决方案。