开发者社区> 问答> 正文

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

展开
收起
蛮大人123 2016-02-28 14:29:01 2350 0
1 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载