【问题标题】:JTextPane/JScrollPane display issueJTextPane/JScrollPane 显示问题
【发布时间】:2018-06-21 12:29:24
【问题描述】:

我正在尝试用 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

看到奇怪的视觉错误了吗?

【问题讨论】:

  • 请遵循Java命名约定,变量/属性/方法必须以小写字母开头
  • 哦,谢谢你的帮助!
  • 1) 为了尽快获得更好的帮助,请发帖 minimal reproducible exampleShort, Self Contained, Correct Example。 2) Java GUI 必须在不同的语言环境中使用不同的 PLAF 在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作。因此,它们不利于像素完美布局。而是使用布局管理器,或combinations of them 以及white space 的布局填充和边框。 3) Swing 不能很好地处理透明组件。

标签: java swing transparency jscrollpane jtextpane


【解决方案1】:
Bio.setBackground(new Color(0, 0, 0, 0));

不要使用带有 alpha 值的颜色。这违反了 Swing 绘制规则,并且 Swing 不知道如何正确绘制组件并且您会得到绘制工件。

为了完全透明,只需使用:

component.setOpaque( false );

如果您需要使用部分透明度,请查看Backgrounds With Transparency 了解更多信息以及解决方案。

【讨论】:

  • 太棒了!谢谢!
猜你喜欢
  • 2013-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-27
  • 2017-09-08
  • 1970-01-01
  • 2016-09-24
相关资源
最近更新 更多