相关疑难解决方法(0)

如何从文件夹中的图像将图标设置为JLabel?

每当从JComboBox中选择一个项目时,我都试图从图像文件夹中将图标设置为JLabel.JComboBox中的项目名称和文件夹中图像的名称相同.因此,无论何时从JComboBox中选择一个项目,都应将具有相同名称的相应图像设置为JLabel的图标.我想做这样的事情.

private void cmb_movieselectPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt){                                                             
        updateLabel(cmb_moviename.getSelectedItem().toString());
}





protected void updateLabel(String name) {
        ImageIcon icon = createImageIcon("C:\\Users\\xerof_000\\Pictures\\tmspictures\\" + name + ".jpg");
        if(icon != null){
            Image img = icon.getImage(); 
            Image newimg = img.getScaledInstance(lbl_pic.getWidth(), lbl_pic.getHeight(),  java.awt.Image.SCALE_SMOOTH);
            icon = new ImageIcon(newimg);
            lbl_pic.setIcon(icon);
            lbl_pic.setText(null);
        }
        else{
            lbl_pic.setText("Image not found");
            lbl_pic.setIcon(null);
        }
    }





protected static ImageIcon createImageIcon(String path) {
        URL imgURL;
        imgURL = NowShowing.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            return null;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我认为问题出现在"C:\ Users\xerof_000\Pictures\tmspictures \"中我尝试使用"C:/ Users/xerof_000/Pictures/tmspictures /",但即使这样也行不通.无论我做什么,它只在JLabel上显示"Image not found".

java swing jlabel jcombobox imageicon

6
推荐指数
1
解决办法
6万
查看次数

JLabel刷新图标,更新图像

我正试图在图像处理方面做一个实验.基本上我有一个图像,由计时器不断更新,我在JLabel中显示该图像.

我的问题是JLabel没有刷新图像.

这是我的计时器代码:

Timer timer = new Timer(200, new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            count++;

            System.out.println("timer");
            System.out.println(filename);

            ImageIcon icon = new ImageIcon(filename);

            label = new JLabel();
            label.setIcon(icon);
            label.setText(""+count);

            panel = new JPanel();
            panel.add(label);

            frame.getContentPane().removeAll();
            frame.getContentPane().add(panel);

            frame.repaint();
            frame.validate();

            try{
                FileWriter fstream;

                fstream = new FileWriter(filename,true);

                BufferedWriter out = new BufferedWriter(fstream);

                out.write("text to append");
                out.close();
            }catch (Exception ex){
                System.err.println("Error: " + ex.getMessage());
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

其中filename是我的图像的路径.

显示图像但JLabel从不刷新我的图像.我测试了我的代码,如果我在两个不同的图像之间切换,它正在工作......

编辑:

每次创建最后一个图像并使用时间戳重命名时,我都会通过复制解决.

java swing refresh jlabel imageicon

3
推荐指数
1
解决办法
7885
查看次数

标签 统计

imageicon ×2

java ×2

jlabel ×2

swing ×2

jcombobox ×1

refresh ×1