2014-09-11 58 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