Creating custom JButton from images containing transparent pixels

前端 未结 6 557
悲哀的现实
悲哀的现实 2020-12-02 00:46

Read edit 2 for what I\'m actually missing to make it work

I\'m currently trying to create some custom JButtons using images created in photoshop that have an

6条回答
  •  温柔的废话
    2020-12-02 01:03

    I think you are on the wrong way. You do not have to override neither paint() nor paintComponent() methods. JButton already "knows" to be shown with image only:

    ImageIcon cup = new ImageIcon("images/cup.gif");
    JButton button2 = new JButton(cup);
    

    See the following tutorial for example: http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JButton.html

    Moreover swing is fully customized. You can control opacity, border, color etc. You probably should override some mentioned methods to change functionality. But in most cases there is better and simpler solution.

提交回复
热议问题