簡體   English   中英

自動調整JButton圖標的大小

[英]Auto-resizing JButton Icon

所以我有這個JButtons我添加了圖標。 最初的圖標太大,所以我事先調整它們的大小,它工作正常。 除了當我調整窗口大小時,JButton會改變大小,但不會改變圖標,這是有問題的。

有沒有辦法讓Icon只填充它附加的JButton? 使代碼更清晰的代碼:

public JewelClass(){

    setBackground (new Color (30,30,30)); 
    addActionListener(this);
    setLayout(new GridLayout());

    ImageIcon icon = new ImageIcon(src/carre.jpg);
    setIcon (resizeIcon(icon,60,60));

}

resizeIcon是一個個人函數,它接受一個I​​con,一個width參數和一個height參數,並返回一個調整大小的Icon(顯然)。 我嘗試更改布局,但它沒有改變任何東西。 我嘗試獲取JButton的寬度/高度,但是因為它們在添加Icon時尚不存在,所以它不起作用。

你們有任何想法如何解決這個問題嗎? 它不一定是一個圖標,只要我的JButton充滿我給它的圖像,它真棒:)

謝謝!

在Swing中你可以將任何JComponent添加到另一個JComponent ,因為ImageJLabel最好的JComponent ,那么為什么不將JLabel#setIcon()JButton

在此輸入圖像描述在此輸入圖像描述

import java.awt.*;
import javax.swing.*;

public class ResizeIconInButton extends JFrame {
    private static final long serialVersionUID = 1L;

    public ResizeIconInButton() {
        JButton myButton = new JButton();
        myButton.setLayout(new BorderLayout());
        myButton.add(new CustomComponents0());
        add(myButton, BorderLayout.CENTER);
        setPreferredSize(getPreferredSize());
        setTitle("Resize Icon In Button");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                ResizeIconInButton main = new ResizeIconInButton();

            }
        };
        javax.swing.SwingUtilities.invokeLater(r);
    }
}

class CustomComponents0 extends JLabel {

    private static final long serialVersionUID = 1L;

    @Override
    public Dimension getMinimumSize() {
        return new Dimension(200, 100);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 200);
    }

    @Override
    public void paintComponent(Graphics g) {
        int margin = 10;
        Dimension dim = getSize();
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
    }
}

編輯:

import java.awt.*;
import javax.swing.*;

public class ResizeIconInButton extends JFrame {

    private static final long serialVersionUID = 1L;
    private static final String IMAGE_PATH = "http://duke.kenai.com/misc/Bullfight.jpg";
    private JButton myButton = new JButton();
    private JLabel myLabel = new JLabel();

    public ResizeIconInButton() {
        Icon myIcon = new ImageIcon(IMAGE_PATH);
        myLabel.setIcon(myIcon);
        myButton.setLayout(new BorderLayout());
        myButton.add(myLabel);
        add(myButton, BorderLayout.CENTER);
        setPreferredSize(new Dimension(200, 100));
        setTitle("Resize Icon In Button");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                ResizeIconInButton main = new ResizeIconInButton();
            }
        });
    }
}
  1. 覆蓋paintComponent
  2. 將圖像直接繪制到它的Graphics對象

在繪制圖像時,提供維度參數getWidth()getHeight() 通過這樣做,調整大小將自動化。 此外,在調整大小時,您需要進行一些抗鋸齒處理,以使圖像不會過於像素化。

您可以向該按鈕添加一個Component-Listener,在調整大小時調整Image中的Image

yourButton.addComponentListener(new ComponentListener() {

        @Override
        public void componentShown(ComponentEvent e) {
            // ignore
        }

        @Override
        public void componentResized(ComponentEvent e) {
            resizeIcon(icon, yourButton.getWidth(), yourButton.getHeight());
        }

        @Override
        public void componentMoved(ComponentEvent e) {
            // ignore
        }

        @Override
        public void componentHidden(ComponentEvent e) {
            // ignore
        }
    });

希望能幫助到你!

暫無
暫無

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

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