gpt4 book ai didi

java - 动态更改 JPanel 的背景图像

转载 作者:行者123 更新时间:2023-12-02 06:58:37 25 4
gpt4 key购买 nike

尝试更改 jpanel 的 BG 图像,但我无法在任何正常方法上调用 poaint,当我构建构造函数但我不想重建构造函数时,它效果很好。

...

通过在我的中心框架中放置一个标签并调用 setIcon 找到了一个解决方案,但我需要能够提取相关信息,所以我需要找到一种方法将值存储到我的 Jtoggle 按钮中(比赛或类(class),以便我可以获取它的图片并更改图标)

想法?所有内容都在 iff 语句之外编译,这是我的症结所在

RaceButtons_lft[i] =  new JToggleButton();
RaceButtons_lft[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JToggleButton cb = (JToggleButton)ae.getSource();
for (int j=0; j<MyRaceArray.size(); j++)
{
if (MyRaceArray.get(j).getraceID() == combo_contents.getIndex()){//here is my sticking point, i need to find a way to match MyRaceArray's getRaceID to some value saved withthe Toggle button
final ImageIcon BGCSMs = ScaledImageIcon("Fantasy_Landscape_01.jpg", "Profile Pic", (468-(60*2)), 285);
picLabel.setIcon(BGCSMs);
}//if
}//for
}//action performed;
});//button add action listener

最佳答案

调用

super.paintComponent(..)

可能 - 根据父类(super class) - 用背景颜色填充组件。

public void paintComponent(Graphics g) {
// Let UI Delegate paint first, which
// includes background filling since
// this component is opaque.

super.paintComponent(g);
g.drawString("This is my custom Panel!",10,20);
redSquare.paintSquare(g);
}

(参见A Closer Look at the Paint Mechanism)。在这种情况下,您不需要 repaint(..)。

关于java - 动态更改 JPanel 的背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16978776/

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