图像无法更新其他组件(MVC) [英] The image cannot update other components (MVC)
问题描述
import java.awt.event.ActionListener;
import java.util.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.AdjustmentEvent;
我还有其他几个帧(两个只是在打数字,另一个也是滚动条)。我可以使用其他组件来更新此视图(这基本上显示基于温标的一盒图像)。同样,我可以从此视图更新其他组件。
I have several other frames (two are just punching numbers in, and another is also a scrollbar). I can use other components to update this view (this basically displays a box of image based on the temperature scale). Similarly, I can update other components from this view.
然而 (已修改),栏会移动,但图像保持不变....
任何人都可以看到一个错误?感谢您对此的任何意见/谢谢!
However (edited), the bar does move, but the image stays the same.... Can anyone see a bug? I appreciate any inputs into this/ Thank you!
推荐答案
因为您要更换标签,所以需要 remove()
组件和 validate()
容器
。或者,只需替换图标。
Because you're replacing the label, you need to remove()
the component and validate()
the Container
. Alternatively, just replace the icon.
附录:我认为后一种方法更可取。缺少图像和剩余代码,让我们从这个示例开始:初始化标签和滑块:
Addendum: I think the latter approach is preferable. Lacking your images and remaining code, let's start from this example: initialize the label and slider:
final JLabel label = new JLabel(pig);
final JSlider slider = new JSlider();
然后在监听器中,使用 setIcon()
:
Then in the listener, use setIcon()
:
@Override
public void stateChanged(ChangeEvent e) {
if (slider.getValue() < 50) {
label.setIcon(pig);
} else {
label.setIcon(dog);
}
}
这篇关于图像无法更新其他组件(MVC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!