2011-11-22 63 views
8

所以我有这JButtons我添加图标。图标太大,所以我事先调整它们,并且它工作正常。除了当我调整窗口大小时,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是一个个人的功能,这需要一个图标,宽度参数和高度参数,并返回一个调整大小图标(显然)。 我试着改变布局,但它没有改变任何东西。我尝试获取JButton的宽度/高度,但是因为在添加图标时它们不存在,所以它不起作用。

你们有什么想法如何解决这个问题吗?它不一定是一个图标,只要我的JButton充满了我给它的图像,它真棒:)

谢谢!

回答

3
在Swing

您可以任意JComponent添加到另一个JComponent,为ImageJLabel最好JComponent,那么为什么不把JLabel#setIcon()JButton

enter image description hereenter image description here

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

对于更简单的方法,更改为'int margin = 0; Dimension dim = this.getParent()。getSize();'因为父组件的边距已经设置,所以不需要重写'getMinimumSize()'和'getPreferredSize()' –

+0

好极了!所以它可以绘制一个形状,但唯一的是,一旦我替换成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);' ,它根本不显示任何内容。 你知道这是为什么吗? –

+0

@Mikalichov请参阅我的编辑 – mKorbel

2

你可以一个组件的侦听器添加到按钮,调整大小后重新调整你的形象在它

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

希望它能帮助!

3
  1. 覆盖paintComponent
  2. Draw the image直接到它的Graphics对象

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