java图片 变大了,Java调整图像大小

I would like to display an image in an area in my Jframe but the image takes up much space.

I would like to take it fair precise dimensions.

How can I do this in Java

This is my simple code :

I am open to any proposal if I did not use the right method or the right class to instantiate the image.

import java.awt.*;

import javax.swing.*;

public class ExempleDeplace extends JFrame{

private JLabel myLabel;

public ExempleDeplace(){

setLayout(new FlowLayout());

setTitle("Fenetre, modele Duchi");

setSize(500,700);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel j = new JLabel(new ImageIcon("src/images/bateau.png"));

add(j);

setVisible(true);

}

public static void main (String[] args) {

ExempleDeplace c = new ExempleDeplace();

}

}

解决方案

You can paint the image in a JPanel as the whole panel. Then whenever the panel is resized, the image will be resized along with it. Here's a quick-n-dirty runnable demo:

import java.awt.BorderLayout;

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class ImgFrame extends JFrame {

private static BufferedImage IMG;

static{

try {

IMG = ImageIO.read(new File("img/Original_Doge_meme.jpg")); //Replace with your image path

} catch (IOException e) {

e.printStackTrace();

}

}

public ImgFrame(){

add(new ImgPanel(), BorderLayout.CENTER);

setSize(500,700);

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

class ImgPanel extends JPanel{

@Override

public void paintComponent(Graphics g){

super.paintComponent(g);

g.drawImage(IMG, 0, 0, getWidth(), getHeight(), this);

}

}

public static void main(String[] args){

new ImgFrame();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值