2014-09-11 22 views
0

目前,我用這個代碼:如何強制將ImageIcon設爲特定尺寸?

public class Test extends JFrame { 
static Test t = null; 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       t = new Test(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

public static void addComponentsToPane(Container pane) { 

    JButton button; 
    pane.setLayout(new GridBagLayout()); 
    GridBagConstraints c = new GridBagConstraints(); 

    ImageIcon icon; 
     icon = new ImageIcon(System.getProperty("user.dir") + "/res/background.png"); 
    Image img = icon.getImage() ; 
     Image newimg = getScaledImage(img, frame.getWidth(), frame.getHeight()) ; 
     icon = new ImageIcon(newimg); 
    JLabel background=new JLabel(icon); 

     //frame.getContentPane().add(background); 

     background.setLayout(new FlowLayout()); 

    c.fill = GridBagConstraints.HORIZONTAL; 
    c.ipady = frame.getHeight() * 10; 
    c.weightx = 0.0; 
    c.gridwidth = 3; 
    c.gridx = 0; 
    c.gridy = 1; 

    pane.add(background, c); 


} 
private static Image getScaledImage(Image srcImg, int w, int h){ 
    BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 
    Graphics2D g2 = resizedImg.createGraphics(); 
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
    g2.drawImage(srcImg, 0, 0, w, h, null); 
    g2.dispose(); 
    return resizedImg; 
} 

private void createAndShowGUI() { 
    frame = new JFrame("GridBagLayoutDemo"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 

static JFrame frame = null; 

public Test() { 

    createAndShowGUI(); 
    addComponentsToPane(frame.getContentPane()); 
    frame.pack(); 
    frame.setVisible(true); 
    frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH); 
} 

} 

這工作正常的按鈕,但由於某種原因未與圖像工作的JLabel。 我該如何讓這個JLabel/Image適合我想要的尺寸?

圖像被此刻真的很小, enter image description here 時候,確實,與一個JButton相同的代碼佔用面積更是這樣的: enter image description here 此外,它可以讓我得到的ImageIcon比按鈕4還要大,這也是我想避免的。

那麼,我該如何拉伸/收縮圖像以適合我希望它具有的區域?

+0

1)爲了更好地提供幫助,請發佈[MCVE](http://stackoverflow.com/help/mcve)(最小,完整,可驗證示例)。 2)獲取圖像的一種方法是熱點鏈接到[本問答](http://stackoverflow.com/q/19209650/418556)中列出的圖像。 – 2014-09-11 11:51:56

+1

@AndrewThompson要麼有人知道答案,要麼他們不知道。由於這是唯一相關的代碼,因此我不理解將代碼放入IDE中的問題。 不知道你的第二個鏈接正在進行什麼:/。 – Joehot200 2014-09-11 11:54:49

+0

*「因爲這是唯一的代碼相關..」*着名的遺言。但是,如果通過「相關」,你的意思是代碼I或其他人可以複製/粘貼,編譯和測試(這與我有關,作爲潛在的幫手),然後,不,它不是所有相關的代碼.. – 2014-09-11 11:57:21

回答

1

所以,我怎麼能伸展/收縮圖像,以適應我想它有什麼地方呢?

查看Darryl的Stretch Icon。圖標會自動以組件的大小繪製。

+0

嘿!非常感謝!另一個答案很好,但StretchIcon要容易得多。 但是,它並不完全拉伸。有什麼辦法解決這個問題? http://gyazo.com/437111079ae300dd0309127b8b914a5c.png – Joehot200 2014-09-12 10:17:18

+0

@ Joehot200,您可以將類配置爲按比例拉伸(默認)或拉伸以填充空間。如果你想填充空間,你需要使用不同的構造函數。 – camickr 2014-09-12 13:59:32

+0

我可以只是愚蠢的,並要求你告訴我它的代碼,讓它伸展填補空間? – Joehot200 2014-09-12 14:10:43

3

你可以使用代碼片段從http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/IconDemoProject/src/components/IconDemoApp.java創建縮放圖像... (從http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html

只是衡量你按鈕,調整圖像尺寸

/** 
* Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. 
* Resizes an image using a Graphics2D object backed by a BufferedImage. 
* @param srcImg - source image to scale 
* @param w - desired width 
* @param h - desired height 
* @return - the new resized image 
*/ 
private Image getScaledImage(Image srcImg, int w, int h){ 
    BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 
    Graphics2D g2 = resizedImg.createGraphics(); 
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
    g2.drawImage(srcImg, 0, 0, w, h, null); 
    g2.dispose();   
    return resizedImg; 
} 

我要拿起來自MadProgammer的關於使用scaledInstance的提示:https://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html

那麼如何測量按鈕尺寸?

final Button b = new Button(); //create it on your custom way... 
b.addComponentListener(new ComponentAdapter() { 

    @Override 
    public void componentResized(ComponentEvent e){ 
     int w = b.getWidth(); 
     int h = b.getHeight();    
     setIconSize(w,h); //TODO by yourself (sorry, if more help required please say so) 
    } 
}); 
+0

那沒有工作出於某種原因。我在OP中發佈了更新的代碼。 – Joehot200 2014-09-11 12:58:15

+0

我沒有看到你在你的按鈕上添加你的compontListener的地方......請添加一個,讓我知道這是否幫助你... – 2014-09-11 14:50:14

+0

爲什麼我需要添加組件監聽器? – Joehot200 2014-09-11 14:51:10