0

我的项目是打开一个图像并再次保存,我使用 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);
}
}

抱歉,我无法上传图片

4

2 回答 2

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"图像输出

于 2013-11-13T12:27:31.557 回答
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();

所有人的tks

于 2013-11-24T14:18:00.247 回答