gpt4 book ai didi

java - 无法更新图像 "JLabel setIcon"

转载 作者:行者123 更新时间:2023-12-02 11:29:10 26 4
gpt4 key购买 nike

!( /image/5NtNP.jpg )
我想在读取数组时更新图像的顺序...所以我尝试将图像的目的地作为变量,然后在需要更改顺序时更改变量,但这不起作用。

   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;
}
}

最佳答案

so i tried to put the destination of the images as variables, then change the variables when i need to change the order but that didn't work.

当然这是行不通的。图标不知道您更改了变量,标签也不知道您更改了图标。

所以代码应该是:

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

关于java - 无法更新图像 "JLabel setIcon",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49409032/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com