如何强制将ImageIcon设置为特定大小? [英] How can I force an ImageIcon to be a certain size?

查看:736
本文介绍了如何强制将ImageIcon设置为特定大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我使用以下代码:

Currently, I use this code:

 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/图像适合我想要的大小?

This works fine for buttons, but for some reason is not working for a JLabel with an image. How can I make this JLabel/Image fit to the size I would like?

此刻图像非常小, 实际上,带有JButton的相同代码占用的区域更像是这样: 另外,我也有可能使ImageIcon也大于按钮4,这也是我想避免的.

The image is being really tiny at the moment, When really, the area that the same code with a JButton takes up is more like this: Also, it is possible for me to get the ImageIcon to be bigger than button 4 as well, which I also would like to avoid.

那么我如何拉伸/收缩图像以适合我想要的区域?

So how can I stretch/contract the image to fit the area I would like it to have?

推荐答案

那么我如何拉伸/收缩图像以适合我想要的区域?

So how can I stretch/contract the image to fit the area I would like it to have?

查看Darryl的拉伸"图标.图标会按照组件的大小自动绘制.

Check out Darryl's Stretch Icon. The Icon is automatically painted at the size of the component.

这篇关于如何强制将ImageIcon设置为特定大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆