JToggleButton 不绘画
好的,所以我做了一个 JToggleButton:
JToggleButton button = new JToggleButton(new ImageIcon(features[i].getImage())) {
private static final long serialVersionUID = 1L;
@Override
public void paint(Graphics g) {
super.paint(g);
if (isSelected()) {
g.setColor(Color.RED);
g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
}
}
};
try {
if (bodyButton.isSelected()) {
if (equals(features[i].getImage(), avatar.getBody().getImage())) {
button.setSelected(true);
}
} else if (eyesButton.isSelected()) {
if (equals(features[i].getImage(), avatar.getEyes().getImage())) {
button.setSelected(true);
}
} else if (glassesButton.isSelected()) {
if (equals(features[i].getImage(), avatar.getGlasses().getImage())) {
button.setSelected(true);
}
} else if (hairButton.isSelected()) {
if (equals(features[i].getImage(), avatar.getHair().getImage())) {
button.setSelected(true);
}
} else if (pantsButton.isSelected()) {
if (equals(features[i].getImage(), avatar.getPants().getImage())) {
button.setSelected(true);
}
} else if (shirtButton.isSelected()) {
if (equals(features[i].getImage(), avatar.getShirt().getImage())) {
button.setSelected(true);
}
} else if (shoesButton.isSelected()) {
if (equals(features[i].getImage(), avatar.getShoes().getImage())) {
button.setSelected(true);
}
}
} catch (Exception e) {}
但是我遇到了它不绘画的问题。如您所见,我在启动后立即将 selected true 设置为 true,但它有时不会绘制按钮,其他时候效果很好!我知道它被设置为 true 因为我在它后面添加了一个。
System.out.println(button.isSelected() + " " +i);
Ok, So I make a JToggleButton:
JToggleButton button = new JToggleButton(new ImageIcon(features[i].getImage())) {
private static final long serialVersionUID = 1L;
@Override
public void paint(Graphics g) {
super.paint(g);
if (isSelected()) {
g.setColor(Color.RED);
g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
}
}
};
try {
if (bodyButton.isSelected()) {
if (equals(features[i].getImage(), avatar.getBody().getImage())) {
button.setSelected(true);
}
} else if (eyesButton.isSelected()) {
if (equals(features[i].getImage(), avatar.getEyes().getImage())) {
button.setSelected(true);
}
} else if (glassesButton.isSelected()) {
if (equals(features[i].getImage(), avatar.getGlasses().getImage())) {
button.setSelected(true);
}
} else if (hairButton.isSelected()) {
if (equals(features[i].getImage(), avatar.getHair().getImage())) {
button.setSelected(true);
}
} else if (pantsButton.isSelected()) {
if (equals(features[i].getImage(), avatar.getPants().getImage())) {
button.setSelected(true);
}
} else if (shirtButton.isSelected()) {
if (equals(features[i].getImage(), avatar.getShirt().getImage())) {
button.setSelected(true);
}
} else if (shoesButton.isSelected()) {
if (equals(features[i].getImage(), avatar.getShoes().getImage())) {
button.setSelected(true);
}
}
} catch (Exception e) {}
But I am having problems with it not painting. I setting selected true right after its institated as you can see, but its not painting the button SOMETIMES, Other times it work GREAT! Iknow that it is being set to true because I added a after it.
System.out.println(button.isSelected() + " " +i);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能会重写
JToggleButton
中的paintComponent()
,或者只使用setIcon()
。附录:确保您在 EDT 上运行并正确重写
paintComponent()
。以下简短、完整、可编译的示例 (sscce) 对我来说可靠。正如 @camickr 建议的那样,创建 sscce 可能有助于隔离您遇到的问题。I might override
paintComponent()
inJToggleButton
, or perhaps just usesetIcon()
.Addendum: Be sure you're running on the EDT and overriding
paintComponent()
correctly. The following short, complete, compilable example (sscce) works reliably for me. As @camickr suggested, creating an sscce may help isolate a problem you encounter.我想我在尝试解决我的一个问题时偶然发现了你的问题。由于您使用 setSelected() 来更改按钮的状态,因此请务必使用 setSelectedIcon() 来更改选择时显示的图标。您可以将其设置为与 setIcon() 相同的值,以获得预期的结果。
I think I stumbled across your problem while trying to solve one of mine. Since you are using setSelected() to change the state of the button, be sure to use setSelectedIcon() to change the icon displayed when selected. You can set it to the same value as setIcon() with expected results.