嘿,那裏,我只是試圖把一個與JFileChooser在一個標籤上拍攝的圖像;但它沒有按照我想要的方式工作。這是我試過的代碼;如何將帶有JFileChooser的圖像圖標放在標籤上?
import java.io.*;
import javax.swing.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
JFileChooser chooser = new JFileChooser();
JFrame frame = new JFrame("My Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
chooser.showOpenDialog(null);
File file = chooser.getSelectedFile();
ImageIcon icon = new ImageIcon(file.getName());
JLabel label = new JLabel(icon);
// JLabel label2 = new JLabel("try try catch it");
panel.add(label);
// panel.add(label2);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
有什麼建議嗎?
謝謝大家,當我使用getPath()方法時,它確實有效! – makyol 2010-06-28 21:55:15