2011-08-26 60 views
3

这里是我的代码:如何在JLabel中调整Image/IconImage的大小?

String s = "/Applications/Asphalt6.app"; 
JFileChooser chooser = new JFileChooser(); 

File file = new File(s); 
Icon icon = chooser.getIcon(file); 

// show the icon 
JLabel ficon = new JLabel(s, icon, SwingConstants.LEFT); 

现在,从图标中提取的图像是非常小的。我怎样才能调整它?

+1

例如参见[此](http://www.javalobby.org/articles/ultimate-image/#11)。但是一旦它变得更大,你的图标可能会相当丑陋。 – toto2

回答

7

Big Icon

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

class BigIcon { 

    public static void main(String[] args) { 
     JFileChooser chooser = new JFileChooser(); 
     File f = new File("BigIcon.java"); 
     Icon icon = chooser.getIcon(f); 

     int scale = 4; 

     BufferedImage bi = new BufferedImage(
      scale*icon.getIconWidth(), 
      scale*icon.getIconHeight(), 
      BufferedImage.TYPE_INT_ARGB); 
     Graphics2D g = bi.createGraphics(); 
     g.scale(scale,scale); 
     icon.paintIcon(null,g,0,0); 
     g.dispose(); 

     JOptionPane.showMessageDialog(
      null, 
      new JLabel(new ImageIcon(bi))); 
    } 
} 
+1

另请参阅此相关的[示例](http://stackoverflow.com/questions/2900801/wanting-a-type-of-grid-for-a-pixel-editor/2901472#2901472)。 – trashgod

+0

非常感谢 –

相关问题