无法更新图片“JLabel setIcon”

时间:2018-03-21 14:30:29

标签: java swing

!(https://imgur.com/a/nuoSJ
我想在读取数组时更新图像的顺序...所以我试图将图像的目的地作为变量,然后在需要更改顺序时更改变量,但这不起作用。

   JLabel no3 = new JLabel();
    no3.setIcon(new ImageIcon(three));
    panel.add(no3);
    frame.getContentPane().add(panel);
    frame.validate();

    JLabel no4 = new JLabel();
    no4.setIcon(new ImageIcon(four));
    panel.add(no4);
    frame.getContentPane().add(panel);
    frame.validate();


    JLabel no5 = new JLabel();
    no5.setIcon(new ImageIcon(five));
    panel.add(no5);
    frame.getContentPane().add(panel);
    frame.validate();

当我更新图像目标变量“三”“四”和“五”等,点击另一个按钮后图像不会改变....

JButton btnNewButton = new JButton("Solve");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String text=textField.getText();
            int[] arr = Arrays.stream(text.split(" ")).map(String::trim).mapToInt(Integer::parseInt).toArray();
            int[] solve= new int[9];
            for(int i=0; i<9;i++)
            {
                solve[i]=arr[i];
                 switch (i) {
                    case 0:  one="C:\\Users\\Mostafa\\Downloads\\Compressed\\cut_images_LVx5IFG8ujN\\image_part_00"+solve[i]+".jpg";
                             break;
                    case 1:  two="C:\\Users\\Mostafa\\Downloads\\Compressed\\cut_images_LVx5IFG8ujN\\image_part_00"+solve[i]+".jpg";
                             break;
                    case 2:  three="C:\\Users\\Mostafa\\Downloads\\Compressed\\cut_images_LVx5IFG8ujN\\image_part_00"+solve[i]+".jpg";
                             break;
                    case 3:  four="C:\\Users\\Mostafa\\Downloads\\Compressed\\cut_images_LVx5IFG8ujN\\image_part_00"+solve[i]+".jpg";
                             break;
                    case 4:  five="C:\\Users\\Mostafa\\Downloads\\Compressed\\cut_images_LVx5IFG8ujN\\image_part_00"+solve[i]+".jpg";
                             break;
                    case 5:  six="C:\\Users\\Mostafa\\Downloads\\Compressed\\cut_images_LVx5IFG8ujN\\image_part_00"+solve[i]+".jpg";
                             break;
                    case 6:  seven="C:\\Users\\Mostafa\\Downloads\\Compressed\\cut_images_LVx5IFG8ujN\\image_part_00"+solve[i]+".jpg";
                             break;
                    case 7:  eight="C:\\Users\\Mostafa\\Downloads\\Compressed\\cut_images_LVx5IFG8ujN\\image_part_00"+solve[i]+".jpg";
                             break;
                    default: nine="C:\\Users\\Mostafa\\Downloads\\Compressed\\cut_images_LVx5IFG8ujN\\image_part_00"+solve[i]+".jpg";
                             break;
                }
            }

1 个答案:

答案 0 :(得分:1)

  

因此我尝试将图像的目的地作为变量,然后在需要更改订单时更改变量,但这不起作用。

当然这不起作用。图标不知道您更改了变量,标签不知道您更改了图标。

所以代码应该是:

//case 0:  one="C:\\Users\\Mostafa\\....";
Icon icon = new ImageIcon("the file name");
no0.setIcon( icon )