我尝试创建一个如下图所示的 GUI
但是,当我运行我的文件时,图像的大小仍然保持原始大小
如何缩小图像的大小以便在旁边添加文本?
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/