【问题标题】:Problems with the alpha parameter when setting a background color for JTextFields为 JTextFields 设置背景颜色时 alpha 参数的问题
【发布时间】:2016-10-18 17:15:23
【问题描述】:

我正在编写一个聊天程序,并试图通过向主 JPanel(构建所有内容的 JPanel)添加背景图像来使其更有趣。当我尝试使用代码将 JTextField 或 JTextArea 的透明度更改为 50% 时

MainPanel = new JPanel(){

@Override
    public void paintComponent(Graphics g) {
        ImageIcon im = new ImageIcon("background.jpg");
        Image i = im.getImage();

        g.drawImage(i,0,0,this.getSize().width,this.getSize().height,this);
    }
};

...

outputWindow.setBackground(new Color(0,0,0,128));

其中'outputWindow' 是添加到'MainPanel' 的JTextArea,一旦我在该区域中显示一些文本,就会出现很多乱码。 Screenshot of the problem

我按顺序输入了“hi”和“hello world”,你可以在输出屏幕的顶部看到,但是有什么办法可以消除多余的乱码吗?

【问题讨论】:

    标签: java swing user-interface background transparency


    【解决方案1】:

    对于每个部分透明的 Swing 组件,请务必调用 setOpaque(false);

    https://tips4java.wordpress.com/2009/05/31/backgrounds-with-transparency/

    【讨论】:

    • 谢谢。这很有效,现在我有了更好的理解。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 2012-07-31
    • 2010-11-08
    • 2020-12-06
    相关资源
    最近更新 更多