如何使图像大小适合ImageIcon和JLabel大小

时间:2013-11-10 19:20:37

标签: java swing jpanel jlabel imageicon

我的项目是打开一个图像并再次保存它,我使用ImageIcon,JLabel和JPanel来显示它(我使用了ImageIcon和JPanel,但它没有用,ImageIcon无法添加到JPanel)。当我打开它时,它总是显示图像但不是JFrame的全尺寸。 我在OpenImage类中编写的代码扩展了JFrame

public class Draw_JPanel extends JFrame{
       Load_image panel_im = new Load_image();
public void OpenImage()
{   
    JFileChooser fc = new JFileChooser();
    int result = fc.showOpenDialog(null);
    if(result == JFileChooser.APPROVE_OPTION)
    {
        File file = fc.getSelectedFile();
        String name = file.getName();
        try {
            image = ImageIO.read(file);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        imageic = new ImageIcon(image);
        height1 = imageic.getIconHeight();
        width1 = imageic.getIconWidth();
         picLabel = new JLabel(new ImageIcon(image));
         panel_im.add(picLabel,BorderLayout.CENTER);
    }
    this.pack();

}

和Load_image类的代码

public class Load_image extends JPanel{
public Load_image()
{   
    this.setBackground(Color.RED);
}
}

抱歉,我无法上传图片

2 个答案:

答案 0 :(得分:0)

我为你写了一个简单的例子。它绘制图像并在imgscalr library的帮助下调整大小。您还可以保存图像。尝试一下,我觉得它可以帮到你。

public class Example extends JPanel{

    private static BufferedImage scaledImg;

    public static void main(String[] args) {
        Example e = new Example();
        JFrame f = new JFrame();
        f.setLayout(new GridBagLayout());

        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1;
        c.weighty = 1;
        f.add(e,c);

        c.fill = GridBagConstraints.NONE;
        c.weightx = 0;
        c.weighty = 0;
        c.gridx = 1;
        JButton b = new JButton("save");
        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                File outputfile = new File("c:\\image.png");
                try {
                    ImageIO.write(scaledImg, "png", outputfile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        f.add(b,c);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }

    @Override
    protected void paintComponent(Graphics arg0) {
        super.paintComponent(arg0);
        Graphics2D g = (Graphics2D) arg0;
        BufferedImage img = null;
        try {
            img = ImageIO.read( Example.class.getResource("/res/3_disc.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        scaledImg = Scalr.resize(img,getSize().height,getSize().width,new BufferedImageOp[]{});
        g.drawImage (scaledImg, null, 0, 0);
    }

}

此处"/res/3_disc.png"是我项目中的图片

"c:\\image.png"输出图片

答案 1 :(得分:0)

我修复了我的代码,我添加了setBounds()方法和setLayout()方法,这里修改了我的代码:

setLayout(null);
        imageic = new ImageIcon(image);
        height1 = imageic.getIconHeight();
        width1 = imageic.getIconWidth();
        picLabel = new JLabel(new ImageIcon(image));
        picLabel.setSize(width1, height1);
        picLabel.setBounds(0, 0, width1, height1);
        panel_im.setSize(width1, height1);
        panel_im.setBounds(-5, -5, width1, height1);
        panel_im.add(picLabel);
        this.pack();

所有