簡體   English   中英

JButton在圖標后面顯示

[英]JButton showing behind icon

我試圖創建一個擴展JButton的自定義按鈕。 它接受文本以及按鈕的所需寬度和高度。 構造函數根據輸入的寬度和高度縮放圖像按鈕。 但是,出了問題,如您在所附圖像中看到的那樣。

嘗試創建按鈕。

這是我的課:

class GameButton extends JButton {
    public GameButton(String text, int width, int height) {
        BufferedImage image = null;
        try {
            image = ImageIO.read(new File("Images/Other/buttonImage.png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // Create new (blank) image of required (scaled) size
        BufferedImage scaledImage = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_ARGB);

        // Paint scaled version of image to new image
        Graphics2D graphics2D = scaledImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(image, 0, 0, width, height, null);
        // clean up
        graphics2D.dispose();

        ImageIcon icon = new ImageIcon(scaledImage);

        setIcon(icon);
        setMargin(new Insets(0, 0, 0, 0));
        setIconTextGap(0);
        setBorderPainted(false);
        setOpaque(false);
        setBorder(null);
        setText(text);
        setSize(width, height);
    }
}

原始按鈕圖像仍在繪制中,看起來圖像可能無法正確縮放。 有任何想法嗎?

謝謝!

為Button設置Horizo​​ntalTextPosition

jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM