BufferedImage 上的 JButton 透明度
我有一个问题:
我正在 JFrame 中渲染 BufferedImage。然后我将 JButton 添加到同一框架。 当我尝试使按钮透明时,按钮变得透明,但忽略其实际位置,它始终透明,就像卡在框架的左上角一样。 我测试了一些不同的方法来使按钮透明,总是得到相同的结果。
有什么想法吗?
谢谢
public class TestPanel extends JPanel {
public TestPanel(){
JButton foo = new JButton("test");
foo.setBackground(new Color(0, 0, 0, 0));
foo.setBounds(20, 100, 300, 50);
this.add(foo);
}
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(ImageFactory.getImg(), 0, 0, null); //get a BufferedImage
g2.dispose();
}
}
I have a Problem:
I'm rendering a BufferedImage in a JFrame. Then i add a JButton to the same frame.
when i try to make the button transparent, the button becomes transparent, but disregarding its actual position, its always transparent like it is stuck in the top left corner of the frame.
I testet some different methods to make the button transparent, always with the same result.
any ideas?
thanks
public class TestPanel extends JPanel {
public TestPanel(){
JButton foo = new JButton("test");
foo.setBackground(new Color(0, 0, 0, 0));
foo.setBounds(20, 100, 300, 50);
this.add(foo);
}
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(ImageFactory.getImg(), 0, 0, null); //get a BufferedImage
g2.dispose();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我看到几个问题,即使我不确定其中哪一个导致了您的问题。我尝试按顺序列出它们:
TestPanel
没有指定LayoutManager
(我希望您在代码中的其他地方指定它)。您正在扩展
JPanel
而不调用 super PaintComponent 方法(不要使用 Paint)。您应该在您的paintComponent方法中执行任何其他操作之前执行此操作:删除dispose方法调用。您不得破坏图形对象。
编辑:
这是一个问题:
您正在尝试显式设置 JButton 的边界。你不应该这样做。如果您使用 LayoutManager,它可能会忽略此指令。如果您使用空布局,这也可能是一个问题。
I see several problems, even if I'm not sure on which of them cause your problem.I try to list them in order:
TestPanel
doesn't specify aLayoutManager
(I hope you are specifying it somewhere else in your code).You are extending a
JPanel
without call super paintComponent method (don't use paint). You should do this before anything else in your paintComponent method:remove the dispose method call. You must not destroy your graphic object.
EDIT:
this is a problem:
you are trying to explicitly set the bounds of your JButton. You shouldn't do that. If you are using a LayoutManager it probably ignore this directive. If you are using a null layout this could be a problem too.
有几个问题
工作代码传入的图形是错误的(编辑:意外删除了透明颜色-设置线,固定)
正如其他人已经指出的:LayoutManagers 是 Swing/AWT 中的必须 - 不使用它们会使 ui 代码变得脆弱且难以维护。
Several problems
working code (Edit: accidentally removed the transparent color-setting line, fixed)
As others already noted: LayoutManagers are a must in Swing/AWT - not using them makes the ui code brittle and hard to maintain.
仅当您将布局设置为 null 时,
setBound()
才会起作用。你的代码没有说类似的话。现在,
JPanel
的默认布局管理器是FlowLayout
。默认情况下,此布局管理器将从左到右然后从上到下排列您的组件。现在,让您的代码按预期工作。在构造函数中添加此行:
setLayout(null)
。但请记住,将布局设置为 null 是一种非常糟糕的做法。
另外,Heisenbug提到的观点也非常有价值。尝试跟随他们。
setBound()
will work only if you have set your layout to null. Your code does not say anything like that.Now, the default layout manager of
JPanel
isFlowLayout
. By default, this layout manager will arrange your components from left to right then top to bottom.Now, to make your code work as expected. Add this line inside your constructor:
setLayout(null)
.But remember, setting the layout to null is a very poor practice.
Also, the points Heisenbug has mentioned are very worthy. Try to follow them.