【问题标题】:Auto-resizing JButton Icon自动调整 JButton 图标大小
【发布时间】:2011-11-22 22:24:03
【问题描述】:

所以我有这个添加图标的 JButton。图标最初太大了,所以我事先调整了它们的大小,它工作正常。除了当我调整窗口大小时,JButtons 会改变大小,但 Icons 不会改变,这是有问题的。

有没有办法让图标只填充它所附加的 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 是一个个人函数,它接受一个图标、一个宽度参数和一个高度参数,并返回一个调整大小的图标(显然)。 我尝试更改布局,但它没有改变任何东西。我尝试获取 JButton 的宽度/高度,但由于添加图标时它们还不存在,所以它不起作用。

你们知道如何度过这个难关吗?它不一定是一个图标,只要我的 JButton 充满了我给它的图像,它就很棒:)

谢谢!

【问题讨论】:

    标签: java swing resize jbutton imageicon


    【解决方案1】:

    在 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();
                }
            });
        }
    }
    

    【讨论】:

    • 为了更简单的方法,更改为 int margin = 0; Dimension dim = this.getParent().getSize(); 而无需覆盖 getMinimumSize()getPreferredSize(),因为父组件已经设置了边距
    • 太棒了!所以它可以绘制一个形状,但唯一的是,一旦我将 g.setColor(Color.red);g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2); 替换为 Icon icon = new ImageIcon("src/triangle.jpg"); icon.paintIcon(this, g, 60, 60); ,它就会停止显示任何东西。你知道这是为什么吗?
    • 仍然没有调整 image 的大小(据我所知,这是 OP 的问题)基本上,将组件添加到组件是一种非常迂回的方式实现一个目标,特别是如果它没有实现它或者我错过了什么?
    • 确实,这个答案没有达到预期。
    • @Tomáš Zato 可能是你的另一个错误
    【解决方案2】:
    1. 覆盖paintComponent
    2. Draw the image 直接指向它的 Graphics 对象

    并且在绘制图像时,提供尺寸参数getWidth()getHeight()。通过这样做,调整大小将是自动化的。此外,在调整大小时,您需要进行一些抗锯齿处理,这样图像就不会太像素化。

    【讨论】:

      【解决方案3】:

      您可以向该按钮添加一个组件侦听器,在调整大小时调整其中的图像大小

      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
              }
          });
      

      希望对你有帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-29
        • 1970-01-01
        • 2014-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-29
        • 1970-01-01
        相关资源
        最近更新 更多