如何使 AWT 组件在渐进式背景图像上透明?使用了 Window()、Panel()、Button() 但失败
当背景是渐进式图像时,如何使 AWT 组件透明?
注意:仅使用 AWT,其中渐进式 Window() 每秒 5 帧。使用 new Color(255,0,0,0) 时,Panel() 现在不会变得透明。如何让面板透明?
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,当我使用按钮和面板时,没有人可以设置其透明度。
我该如何解决它?对于渐进式背景图像面板或按钮使其透明?
总结:
经过多次测试,我现在达到了一个点,这是其他地方无法提供的。主要是在逐行扫描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
How can i make AWT component transparent when the background is progressive image?
Note: AWT used only, where progressive Window() 5 frames/per second. Panel() is not getting transparent now while using new Color(255,0,0,0). How to make the panel transparent?
AWT? here:
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?: here
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) );
}
}
Note: None really works, with Swing i have problem with JButton, because when the JWindow get refreshed the JButton become invisible. With AWT when i use Button and Panel then none can set its transparency.
How do i solve it? For progressive background image Panel or Button to make transparent?
Summary:
I came to a point now after doing many tests, which is not available anywhere else. Mostly in a progressive scan Window() or JWindow(). You better use Awt for (no transparent objects), and Swing for (transparent objects). Do not make a conclusion like many not to use Awt and Swing, you have to be smart to mix it, else gonna end up with endless summer of 69 :)
Reference:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是你关于这个话题的第二个问题,我仍然不知道你在做什么。您一直在谈论背景图像,但代码中没有显示该图像的代码。
是的,当您使用 alpha 值为 0 的 setColor 时会出现问题。您不应该这样做。正确的解决方案是使组件不透明。
有关详细信息,请参阅透明背景。
或者也许背景面板就是您正在寻找的。
此外,您对约束的使用也很旧:
首先,您不应该对约束值进行硬编码。其次,如果您阅读 API,您会发现您应该使用:
并且在使用布局管理器时,您不设置大小和位置。
This is your second question on this topic and I still can't tell what you are doing. You keep talking about a background image, but no where in your code do you show the code for the image.
Yes there are problems when you use setColor with an alpha value of 0. You should NOT do this. The proper solution is to just make the component non-opaque.
See Background With Transparency for more information.
Or maybe Background Panel is what you are looking for.
Also you usage of constraints is old:
First of all you should not hard code the constraint value. Secondly if you read theAPI you will see that you should be using:
and when using layout managers you don't set the size and location.