java - JPanel 中的图像尺寸变小

标签 java image swing imageicon

我尝试创建一个如下图所示的 GUI

enter image description here

但是,当我运行我的文件时,图像的大小仍然保持原始大小

enter image description here

如何缩小图像的大小以便在旁边添加文本?​​

public class HomePage extends JFrame {
  public static void main(String[] args) throws IOException {
    new HomePage();
  }

  public HomePage() throws IOException {
    this.setTitle("Picture Application");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setBackground(Color.yellow);
    BufferedImage myPicture = ImageIO.read(new File("C:\\Users\\seng\\Desktop\\logo.png"));
    myPicture.getScaledInstance(10,30,Image.SCALE_SMOOTH);
    JLabel picLabel = new JLabel(new ImageIcon(myPicture));
    add(picLabel);
    this.pack();
    this.setVisible(true);
  }
}

最佳答案

myPicture.getScaledInstance(10,30,Image.SCALE_SMOOTH);

上述语句返回一个您从未引用的新 BufferedImage。

代码应该是:

//myPicture.getScaledInstance(10,30,Image.SCALE_SMOOTH);    
Image scaled = myPicture.getScaledInstance(10,30,Image.SCALE_SMOOTH);
JLabel picLabel = new JLabel(new ImageIcon(scaled));

关于java - JPanel 中的图像尺寸变小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37596423/

相关文章:

java - 当我在 JPanel 上绘制背景图像时,它在 Windows 下的行为与在 Linux 下的行为不同

java - CancelSelection() 后 JFileChooser 不会消失

java - 在Windows上将mongoDB安装为docker镜像

java - 显示来自数据的图像

jquery - 如何强制图像首先加载

image - scipy.misc 图像函数(例如 imread、imresize、imsave、imshow 等)上的 AttributeError、ImportError

java - JSpinner 在 Action 监听器中不起作用

Java MapReduce 计数器 - Oozie

java - Oracle ORA-29536 关于 DDL-将 java 源加载到数据库中

python - 调整地面实况图像的大小,而不更改标签