java swing添加图片问题

想要实现一个点击按钮后出现文件选择器,然后选择一张图片显示的效果,但是选择完图片没有出现,运行起来也不会报错,求高人指点!

点击事件处理函数如下:

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"))
    {

    }
}
阅读 5.7k
2 个回答

if(add.getActionCommand().equals("xuan"))这个判断应该是走不到的吧,你把这个去掉试试

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题