안녕하세요, 저는 방금 레이블에 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