[Java]JButton按鈕透明背景

下圖是使用html-tag img來顯示圖片,這與setIcon很像,setIcon只可放置於側邊,而setText裡使用html-tag可以有更多的變化。(這一個並無法把字放到圖上,除非合成圖片?)

HtmlImageButton.png

package yku;

import java.awt.Frame;
import javax.swing.JButton;
import java.awt.BorderLayout;

public class HtmlImage extends Frame {

    private static final long serialVersionUID = 1L;
    private JButton jButton = null;

    /**
     * This is the default constructor
     */
    public HtmlImage() {
        super();
        initialize();
    }

    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize() {
        this.setSize(500, 600);
        this.setTitle("Html圖片");

        this.add(getJButton(), BorderLayout.CENTER);
    }

    /**
     * This method initializes jButton    
     *     
     * @return javax.swing.JButton    
     */
    private JButton getJButton() {
        if (jButton == null) {
            yku.tool.Image tool = new yku.tool.Image("/han.png");
            System.out.println(tool.getImagePath());
            jButton = new JButton(
                    "<html><body><img src='"+tool.getImagePath()+"'/><br/>中文字放圖側邊</body>" +
                                    "</html>");
            
        }
        return jButton;
    }
    public static void main(String args[])
    {
        (new HtmlImage()).setVisible(true);
    }
}

發表迴響