java - 如何使 AWT 组件在渐进式背景图像上透明?使用了 Window()、Panel()、Button() 但失败了

标签 java linux swing user-interface awt

当背景是渐进图像时,如何使 AWT 组件透明?

注意:仅使用 AWT,其中渐进式 Window() 每秒 5 帧。使用新颜色 (255,0,0,0) 时,Panel() 现在不会变得透明。如何让面板透明?

enter image description here

AWT?这里:

public class 888 extends Window 
{
    private Button button;

    public 888() 
    {
        super(new Frame());
        // Transparent failed
        getOwner().setBackground(new Color(255, 0, 0, 0) );

        this.setLayout (new BorderLayout ());

        button = new Button("close");

        button.setBackground(Color.RED);
        button.setSize(200,200);
        button.setLocation(0,20);

        this.add("North", button);

        Panel p = new Panel();
        //p.setOpaque(false);
        p.setSize(200,200);
        p.setLocation(400,400);
        // Transparent failed
        p.setBackground(new Color(255,0,0,0));
        p.add("Left", new Button("Test"));
        this.add("North", p);

        //AWTUtilities.setWindowOpacity(this, 0.2f); // Error in Linux

    }


    public static void main(String[] args) 
    {
        Window j = new 888();
        j.setVisible(true);
        //j.setBackground( new Color(255, 0, 0, 0) );
    }

}

Swing ?:这里

public class 888 extends JWindow 
{
    private JButton button;

    public 888() 
    {
        //super(new Frame());
    // Transparent failed
        getOwner().setBackground(new Color(255, 0, 0, 0) ); 

        this.setLayout (new BorderLayout ());

        button = new JButton("close");

        button.setBackground(new Color(255,0,0,255));
        button.setSize(200,200);
        button.setLocation(0,20);

        this.add("North", button);

        JPanel p = new JPanel();
        //p.setOpaque(false);
        p.setSize(200,200);
        p.setLocation(400,400);
        // Transparent failed
        p.setBackground(new Color(255,0,0,0));
        p.add("Left", new Button("Test"));
        this.add("North", p);

        //AWTUtilities.setWindowOpacity(this, 0.2f); // Error in Linux

    }


    public static void main(String[] args) 
    {
        JWindow j = new 888();
        j.setVisible(true);
        //j.setBackground( new Color(255, 0, 0, 0) );
    }

}

注意:没有一个真正起作用,对于 Swing,我对 JButton 有问题,因为当 JWindow 刷新时,JButton 变得不可见。使用 AWT,当我使用 Button 和 Panel 时,没有人可以设置其透明度。

我该如何解决?对于渐进式背景图片 Panel 或 Button 做透明?

总结:

经过多次测试,我现在得出了一个观点,这是其他任何地方都没有的。主要在逐行扫描 Window() 或 JWindow() 中。您最好将 Awt 用于(无透明对象),将 Swing 用于(透明对象)。不要像很多人那样下结论不使用 Awt 和 Swing,你必须聪明地混合使用,否则将以 69 的无尽夏天结束:)

引用:

http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html http://java.sun.com/products/jfc/tsc/articles/threads/threads3.html

最佳答案

这是你关于此主题的第二个问题,我仍然无法判断你在做什么。您一直在谈论背景图片,但您的代码中没有显示图片的代码。

是的,当您使用 alpha 值为 0 的 setColor 时会出现问题。您不应该这样做。正确的解决方案是使组件不透明。

参见 Background With Transparency获取更多信息。

或者可能是Background Panel正是您要找的。

你对约束的使用也很旧:

this.add("North", button); 

首先,您不应该对约束值进行硬编码。其次,如果您阅读 API,您会发现您应该使用:

this.add(button, BorderLayout.NORTH); 

并且在使用布局管理器时,您无需设置大小和位置。

关于java - 如何使 AWT 组件在渐进式背景图像上透明?使用了 Window()、Panel()、Button() 但失败了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6718042/

相关文章:

java - 让 .net 客户端使用来自 Java 服务器的服务的最佳方式是什么?

linux - 哇 |为什么长度不对?

java - 在任意角落绘制图像

java - CardLayout:如何在一个类中的ActionListeners中显示另一类中的面板?

java - 如何使用 JTabbedPane 将多个 JPanel (3) 放入一个 JPanel 中?

java - Gson解析卡住

java - Swing - 从按钮类获取主窗口类实例

java - 通过hibernate动态创建和切换数据库

linux - 企业DevOps架构建议

c++ - 使用经过检查的 STL 实现,有什么免费的吗?