![](/img/trans.png)
[英]How To Make a JPanel visible after clicking a JButton on a JFrame
[英]how to dispaly image in jpanel by clicking on jbutton in jframe
我想知道如何通過在jframe中單擊jbutton在jpanel中顯示圖像。
我在jframe上創建了jpanel,並在jframe上添加了一個按鈕,以在jpanel中顯示圖像,
我想在jbuttonActionPerformed {}中添加什么代碼,
您可以使用Jfilechooser加載圖像,也可以直接放置圖像路徑。這里的照片是JLable。
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG, GIF, and BMP Images", "jpg", "gif", "bmp");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile().getAbsoluteFile();
try {
img = ImageIO.read(file);
} catch (IOException e1) {
e1.printStackTrace();
}
img = img.getScaledInstance(photo.getWidth(), photo.getHeight(), img.SCALE_AREA_AVERAGING);
photo.setIcon(new ImageIcon(img));
jInternalFrame1.setContentPane(photo);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.