JToggleButton 不绘画

发布于 2024-08-24 16:52:20 字数 1821 浏览 7 评论 0原文

好的,所以我做了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心的位置 2024-08-31 16:52:20

我可能会重写 JToggleButton 中的 paintComponent(),或者只使用 setIcon()

附录:确保您在 EDT 上运行并正确重写 paintComponent()。以下简短、完整、可编译的示例 (sscce) 对我来说可靠。正如 @camickr 建议的那样,创建 sscce 可能有助于隔离您遇到的问题。

import java.awt.*;
import javax.swing.*;

public class NewMain extends JPanel {

    public NewMain() {
        super(true);
        JToggleButton button = new JToggleButton(new ImageIcon("test.gif")) {

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
            }
        };
        this.add(button);
    }

    private static void create() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new GridLayout(2, 2));
        for (int i = 0; i < 4; i++) {
            f.add(new NewMain());
        }
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                create();
            }
        });
    }
}

I might override paintComponent() in JToggleButton, or perhaps just use setIcon().

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.

import java.awt.*;
import javax.swing.*;

public class NewMain extends JPanel {

    public NewMain() {
        super(true);
        JToggleButton button = new JToggleButton(new ImageIcon("test.gif")) {

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
            }
        };
        this.add(button);
    }

    private static void create() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new GridLayout(2, 2));
        for (int i = 0; i < 4; i++) {
            f.add(new NewMain());
        }
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                create();
            }
        });
    }
}
梦里寻她 2024-08-31 16:52:20

我想我在尝试解决我的一个问题时偶然发现了你的问题。由于您使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文