我希望用戶能夠點擊一個按鈕,並選擇和圖像哪些將在屏幕上顯示。如何繪製來自JFileChooser的圖像?
這是我寫的代碼。它似乎不工作:
uploadBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int retVal = fc.showOpenDialog(EditImage.this);
if(retVal == JFileChooser.APPROVE_OPTION){
File file = fc.getSelectedFile();
try{
Image img = ImageIO.read(file);
if(img==null){
//TODO: THE FILE IS NOT AN IMAGE. ERROR
}
ImageIcon ic = new ImageIcon(img);
JLabel imageLabel = new JLabel(ic);
imagePreview.add(imageLabel);
}
catch(IOException ex){
//TODO: THE FILE COULD NOT BE OPENED.
}
}
}
});
imagePreview
是一個JPanel,我已經得到某處在屏幕上。
我在做什麼錯?
imagePreview.repaint() – Typo