English 中文(简体)
我怎么能够把一个板块移动?
原标题:How can I move a board piece?

我正在做一个游戏,并给他们带来麻烦。 这批货物将根据冰雪结果移动。 下面是试图做的,但并不奏效。 注:我用图像Icon来代表我的内容。 任何帮助?

    //Puts the player 1 piece on button 1,3,5,7,9 and player 2 piece on button 2,4,6,8,10 
    if ((btnNumber - 1) < 10) 
    { 
        if (((btnNumber - 1) % 2) == 0) 
        { 
            buttons[btnNumber - 1].setIcon(piece1); 
        } 
        else 
        { 
            buttons[btnNumber - 1].setIcon(piece2); 
        } 
    } 
    centerPanel.add(buttons[btnNumber - 1]); 
} 

frame.add(centerPanel, BorderLayout.CENTER); 
最佳回答

它看看着你们掌握的一些基本东西。 下面是关于更接近您目标的一些建议:

  1. button[].addActionListener is a nonsensical statement. You can t add an action listener to an entire array at once. Perhaps you meant to say buttons[btnNumber - 1].addActionListener and place it inside the for loop.
  2. buttons[]==ImageIcon("piece1") is also a meaningless statement (it should not even have compiled). You could try buttons[btnNumber - 1] == ImageIcon("piece1"), though there is an easier way to do this (#3).
  3. Instead of determining the location of the piece based on whether it equals an image icon instance, why not have variables piece1Location and piece2Location that you keep updated as the pieces move. Then you would know instantly where the pieces are, and your logic would simplify to if (btnNumber - 1 == piece1Location)
  4. In Java, arrays start at index 0. The cleanest way to work with an array in Java with a for loop is to start your indexing variable at 0 as well, and change your comparison from <= to <. With the following for(int i = 0; i < 30; i++), you don t have to keep saying btnNumber - 1.
  5. The organization of your code is a little confusing and tightly integrated between model and presentation. You should seek to separate logic dealing with showing the board and pieces (presentation) from logic dealing with the mechanics of the game (rolling dice, moving pieces, etc.). This will help you immensely in maintaining your code and keeping track of what s going on.
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签