简体   繁体   中英

How can I make an image the same size as the panel?

I created a simple frame program that includes a image. But the image don't have the same size as the frame. If i enlarge the frame the image size stays the same?

How can i make the image the same size as the frame?

Here is my current code:

    JPanel panel1 = new JPanel();
    ImageIcon image1 = new ImageIcon("C:\\Users\\Dark Mangetsu\\Downloads\\Ceng102_Lab10.1\\image\\flower.jpg");
    JLabel label1 = new JLabel(image1);
    panel1.add(label1);
    Color color1 = new Color (200, 0 ,100);
    panel1.setBorder(BorderFactory.createLineBorder(color1, 3));
    JFrame f = new JFrame("Frame");
    f.setLayout(new BorderLayout(5,5));
    f.add((panel1),BorderLayout.WEST);
    f.setSize(320,200);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

You can paint the image instead of using a label.

ImageIcon icon = new ImageIcon("C:\\Users\\Dark Mangetsu\\Downloads\\Ceng102_Lab10.1\\image\\flower.jpg");
Image image = icon.getImage();
JPanel panel1 = new JPanel() {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, getWidth(), getHeight());
    }
};

Also not sure, but I think you may want to add the panel to the CENTER and not the west (if you want the image to be centered in the frame).

Also not, if you want a preferredSize for the panel, you will have to override the getPreferredSize() of the panel also.

JPanel panel1 = new JPanel() {
    ...
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(320, 200);
    }
};

Then you can just pack() the frame, which is preferred, instead of setting the size

f.pack();
//f.setSize(320, 200);

在此处输入图片说明在此处输入图片说明在此处输入图片说明

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestBackgroundResize {
    public TestBackgroundResize() {
        JFrame frame = new JFrame();
        frame.setContentPane(createBackgroundPanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private JPanel createBackgroundPanel() {
        return new JPanel() {
            BufferedImage image;
            {
                try {
                    image = ImageIO.read(getClass().getResource("/marioblobs/mario.png"));
                } catch (IOException ex) {
                    Logger.getLogger(TestBackgroundResize.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
            }
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 200);
            }
        };
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new TestBackgroundResize();
            }
        });
    }
}

I think the code is missing call to pack() method.

Here is a sample code:

public class ImageToPanel {
    public static void main(String[] args) {
        ImageToPanel itp = new ImageToPanel();
        itp.go();
    }

    private void go() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() { createAndShowGUI(); }
        });
    }

    private void createAndShowGUI() {
        JFrame f =new JFrame();           
        f.setLayout(new BorderLayout());
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.setPreferredSize(new Dimension(640,400));

        JLabel label = new JLabel( new ImageIcon("wallpaper.jpg") );
        f.add(label, BorderLayout.CENTER);

        JButton button = new JButton("Quit");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
               System.exit(0);
            }
        });       
        f.add(button, BorderLayout.SOUTH);

        f.pack();
        f.setVisible(true);
    }
}

SCREENSHOT

In the screenshot below, a 1920x1200 wallpaper is constrained seamlessly in a 640x400 frame.

受640x400 JFrame限制的1920x1200图像

Tested on

  • Windows 7
  • Java 7

EDIT:

ImageIcon image1 = new ImageIcon("wallpaper.jpg");
JLabel label1 = new JLabel(image1);

Color color1 = new Color (200, 0 ,100);
label1.setBorder(BorderFactory.createLineBorder(color1, 3));

JFrame f = new JFrame("Frame");
f.setLayout(new BorderLayout(5,5));
f.add(label1,BorderLayout.WEST);
f.setSize(320,200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Before resize

在此处输入图片说明

After resize

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM