JToolbar background image

妖精的绣舞 提交于 2020-01-17 03:28:05

问题


I'm using a customized JToolbar using the following code:

public class GeneralToolbar extends JToolBar{

  public GeneralToolbar() {
    super();
    setBackground(Color.white);
    setOpaque(true);
    setPreferredSize(new Dimension(54,54));
    setMinimumSize(new Dimension(54,54));
    setMaximumSize(new Dimension(54,54));
    setSize(new Dimension(54,54));
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Dimension size = this.getSize();
    ImageIcon image = DefaultAction.createImageIcon("/com/aaa/resources/tabback");
    g.drawImage(image.getImage(), 0,0, size.width, size.height, this);
  }
}

Now the image is seen. But I get a opaque rectangle around my buttons. I tried to set the button opaque to false but it didn't add any affect. Thank you for the support


回答1:


Maybe you need to use:

button.setBorderPainted( false );
button.setContentAreaFilled( false );

Of course when you get rid of the Border, then you don't see the effect of clicking on the button.

If you need more help post your SSCCE showing the problem.




回答2:


public GeneralToolbar() {
    super();
    setBackground(Color.white);
    setOpaque(true);
    setPreferredSize(new Dimension(54,54));
    setMinimumSize(new Dimension(54,54));
    setMaximumSize(new Dimension(54,54));
    setSize(new Dimension(54,54));
}
public void paintComponent(Graphics g) {
    super.paintComponent(g);
   Dimension size = this.getSize();
   ImageIcon image = DefaultAction.createImageIcon("/com/aaa/resources/tabback");
   g.drawImage(image.getImage(), 0,0, size.width, size.height, this);
 }


来源:https://stackoverflow.com/questions/1989522/jtoolbar-background-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!