BufferedImage 上的 JButton 透明度

发布于 2024-12-04 03:32:37 字数 579 浏览 2 评论 0原文

我有一个问题:

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

许仙没带伞 2024-12-11 03:32:37

我看到几个问题,即使我不确定其中哪一个导致了您的问题。我尝试按顺序列出它们:

  1. 您的 TestPanel 没有指定 LayoutManager (我希望您在代码中的其他地方指定它)。
  2. 您正在扩展 JPanel 而不调用 super PaintComponent 方法(不要使用 Paint)。您应该在您的paintComponent方法中执行任何其他操作之前执行此操作:

    public void PaintComponent(图形g){
        super.paintComponent(g); 
    }
    
  3. 删除dispose方法调用。您不得破坏图形对象。

编辑:

  1. 这是一个问题:

    foo.setBounds(20, 100, 300, 50);
    

    您正在尝试显式设置 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:

  1. Your TestPanel doesn't specify a LayoutManager (I hope you are specifying it somewhere else in your code).
  2. 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:

    public void paintComponent(Graphics g){
        super.paintComponent(g); 
    }
    
  3. remove the dispose method call. You must not destroy your graphic object.

EDIT:

  1. this is a problem:

    foo.setBounds(20, 100, 300, 50);
    

    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.

沫尐诺 2024-12-11 03:32:37

有几个问题

  • 是,重写paint是错误的,而是重写paintComponent,
  • 按钮具有完全透明的背景,但对于不透明返回true,从而欺骗了绘图机制,
  • 处理作为参数

工作代码传入的图形是错误的(编辑:意外删除了透明颜色-设置线,固定)

public TestPanel(){
    JButton foo = new JButton("test");
    foo.setBackground(new Color(0, 0, 0, 0));
    foo.setOpaque(false);
    foo.setBorder(BorderFactory.createLineBorder(Color.RED));
    this.add(foo);
}

@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.drawImage(ImageFactory.getImg(), 0, 0, null); //get a BufferedImage
   //   g2.dispose();
}

正如其他人已经指出的:LayoutManagers 是 Swing/AWT 中的必须 - 不使用它们会使 ui 代码变得脆弱且难以维护。

Several problems

  • it's wrong to override paint, instead override paintComponent
  • the button has a fully transparent background but returns true for opaque, thus fooling the paint mechanism
  • it's wrong to dispose the Graphics passed in as parameter

working code (Edit: accidentally removed the transparent color-setting line, fixed)

public TestPanel(){
    JButton foo = new JButton("test");
    foo.setBackground(new Color(0, 0, 0, 0));
    foo.setOpaque(false);
    foo.setBorder(BorderFactory.createLineBorder(Color.RED));
    this.add(foo);
}

@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.drawImage(ImageFactory.getImg(), 0, 0, null); //get a BufferedImage
   //   g2.dispose();
}

As others already noted: LayoutManagers are a must in Swing/AWT - not using them makes the ui code brittle and hard to maintain.

聊慰 2024-12-11 03:32:37

仅当您将布局设置为 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 is FlowLayout. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文