想要实现一个点击按钮后出现文件选择器,然后选择一张图片显示的效果,但是选择完图片没有出现,运行起来也不会报错,求高人指点!
点击事件处理函数如下:
public void actionPerformed(ActionEvent add){
JFileChooser fileChooser = new JFileChooser();
/*fileChooser = new JFileChooser();*/
int result = fileChooser.showOpenDialog(this);
if(add.getActionCommand().equals("xuan"))
{
if (result==JFileChooser.APPROVE_OPTION) {
String picPath = fileChooser.getSelectedFile().getPath();
picPath = '"' + picPath + '"';
picPath = picPath.replace("\\","\\\\");
System.out.println(picPath);
ImageIcon icon = new ImageIcon(picPath);
jl2 = new JLabel("这是一个Label",icon,JLabel.CENTER);
jp1.add(jl2);
}
}else if(add.getActionCommand().equals("que"))
{
}
}
参考https://github.com/edagarli/swingchat