简体   繁体   English

JButton的图标没有改变(UNO纸牌游戏)

[英]JButton's icon isn't changing (UNO card game)

I'm implementing a UNO game using sockets and stuff. 我正在使用套接字和东西实现UNO游戏。 So far I've been working on the GUI for each client, I've succeeded when getting a random set of cards for each player and a random central card but got stuck on the following: I'm using a button to represent each card of a hand, say I want to select a Red card (because the central card is also Red) I've done all the validation methods that allow you to place or not to place your card. 到目前为止,我一直在为每个客户端使用GUI,在为每个玩家获得一张随机的卡组和一张随机的中央卡组时,我已经成功了,但是我坚持以下几点:我正在使用一个按钮来代表每张卡例如,说我想选择一张红牌(因为中央卡也是红色的),我已经完成了所有允许您放置或不放置卡的验证方法。 But the central button's icon isn't changing (the central card variable changes but not the button's icon that represents it). 但是中央按钮的图标没有改变(中央卡变量改变,但代表它的按钮图标没有改变)。

I read this question and tried to use the SwingUtilities.invokeLater but the icon doesn't change ( Dynamically change jButton icon ). 我阅读了此问题,并尝试使用SwingUtilities.invokeLater,但图标没有更改( 动态更改jButton icon )。

Here's the code where I change the JButton's icon: 这是我更改JButton图标的代码:

public void setBtnIcon( final javax.swing.JButton btn,  final modelo.Card c) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            btn.updateUI();
            Color color = c.getColor();
            btn.setBackground(color);
            if (c instanceof modelo.NumberCard) {
                short value = ((modelo.NumberCard) c).getValue();


                String str = Short.toString(value);
                btn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jugadoruno/vista/" + str + ".png")));

            } else if (c instanceof modelo.ActionCard) {
                modelo.ActionType t = ((modelo.ActionCard) c).getActionType();
                String str = t.toString();

                btn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jugadoruno/vista/" + str + ".png")));

            }

        }
    });
}

It receives a card (to get it's color, value/actionType), it changes the JButton's background to that same color, and searches for the correct icon (if a card is red and has a value 1 it will change the Button's background to red and search for 1.png that is a transparent image with the number 1) 它收到一张卡片(获取它的颜色,值/ actionType),将JButton的背景更改为相同的颜色,并搜索正确的图标(如果卡片为红色且值为1,它将把Button的背景更改为红色并搜索数字为1的透明图像1.png

If there's a better way to doing this I'll be pleased to know it. 如果有更好的方法可以做到这一点,我将很高兴知道。 Right now It just sets the icon at the beginning of the execution. 现在,它只是在执行开始时设置图标。 Thanks for your time! 谢谢你的时间!

GUI

Two things to check: 检查两件事:

  1. Make sure you're calling setBtnIcon inside some kind of event listener such that it fires when you want it to. 确保在某种事件侦听器中调用setBtnIcon ,以便在需要时触发它。 I couldn't tell from your original question whether "it just sets the icon at the beginning of execution" meant the method was only being called once. 我不能从您最初的问题中看出“是否只是在执行开始时设置图标”就意味着该方法仅被调用了一次。
  2. Run through your code with a debugger (or add a print statement or two) to make sure that the result of getClass().getResource(...) isn't null . 使用调试器运行代码(或添加一个或两个打印语句),以确保getClass().getResource(...)的结果不为null Getting co-located resources can be a tricky thing, especially if you're distributing your app in a JAR file. 获取位于同一位置的资源可能是一件棘手的事情,尤其是如果您要在JAR文件中分发应用程序时。

您是否尝试过删除JButton的“最终”语句?

Solved: I wasn't calling the above function within the update function on the Observer, silly thing but true; 解决:我没有在Observer的update函数中调用上述函数,这很愚蠢,但确实如此; thanks for your help, some of your answers were helpful. 感谢您的帮助,您的一些回答很有帮助。 the client window is the observer, when centralCard is changed, then I need to call the function I wrote above. 客户窗口是观察者,当centralCard更改时,我需要调用上面编写的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM