首页 > java swing添加图片问题

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

    }
}

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


参考https://github.com/edagarli/swingchat

【热门文章】
【热门文章】