如何将图像尺寸最大化为 JPanel 的尺寸

发布于 2024-09-29 23:01:49 字数 28 浏览 0 评论 0原文

如何让JPanel的大小最大化图片的大小?

How I can maximize the size of picture with the size of JPanel?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意中人 2024-10-06 23:01:49

试试这个 SSCCE。

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.net.URL;
import javax.imageio.ImageIO;

public class ImagePanel extends JPanel {

    private Image image = null;

    ImagePanel(LayoutManager layout) {
        super(layout);
    }

    public void setImage(ImageIcon icon) {
        image = icon.getImage().getScaledInstance(this.getWidth(), this.getHeight(), Image.SCALE_DEFAULT);
        repaint();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (image != null) {
            System.out.println("imagepanel painting");
            g.drawImage(image,0,0,getWidth(),getHeight(),null); // see javadoc for more info on the parameters
        }
    }

    public static void main(String[] args) throws Exception {
        JFrame f = new JFrame("Image Panel");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        URL url = new URL("http://pscode.org/media/citymorn2.jpg");
        Image image = ImageIO.read(url);

        ImagePanel ip = new ImagePanel(new GridLayout(4,4,20,20) );
        ip.setBorder( new EmptyBorder(50,50,50,50) );
        for (int ii=0; ii<16; ii++) {
            ip.add( new JButton("" + ii) );
        }

        ip.setPreferredSize(new Dimension(640,480));
        f.setContentPane(ip);
        f.pack();
        f.setVisible(true);

        ip.setImage( new ImageIcon(image) );
    }
}

Try this SSCCE.

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.net.URL;
import javax.imageio.ImageIO;

public class ImagePanel extends JPanel {

    private Image image = null;

    ImagePanel(LayoutManager layout) {
        super(layout);
    }

    public void setImage(ImageIcon icon) {
        image = icon.getImage().getScaledInstance(this.getWidth(), this.getHeight(), Image.SCALE_DEFAULT);
        repaint();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (image != null) {
            System.out.println("imagepanel painting");
            g.drawImage(image,0,0,getWidth(),getHeight(),null); // see javadoc for more info on the parameters
        }
    }

    public static void main(String[] args) throws Exception {
        JFrame f = new JFrame("Image Panel");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        URL url = new URL("http://pscode.org/media/citymorn2.jpg");
        Image image = ImageIO.read(url);

        ImagePanel ip = new ImagePanel(new GridLayout(4,4,20,20) );
        ip.setBorder( new EmptyBorder(50,50,50,50) );
        for (int ii=0; ii<16; ii++) {
            ip.add( new JButton("" + ii) );
        }

        ip.setPreferredSize(new Dimension(640,480));
        f.setContentPane(ip);
        f.pack();
        f.setVisible(true);

        ip.setImage( new ImageIcon(image) );
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文