缩小 JPanel 中的图像尺寸
Smaller down the image size in JPanel
我正在尝试创建如下图所示的 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);
上面的语句returns一个你从未引用过的新 BufferedImage。
代码应该是:
//myPicture.getScaledInstance(10,30,Image.SCALE_SMOOTH);
Image scaled = myPicture.getScaledInstance(10,30,Image.SCALE_SMOOTH);
JLabel picLabel = new JLabel(new ImageIcon(scaled));
我正在尝试创建如下图所示的 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);
上面的语句returns一个你从未引用过的新 BufferedImage。
代码应该是:
//myPicture.getScaledInstance(10,30,Image.SCALE_SMOOTH);
Image scaled = myPicture.getScaledInstance(10,30,Image.SCALE_SMOOTH);
JLabel picLabel = new JLabel(new ImageIcon(scaled));