我想写一个简单的换色游戏 我写了一些代码,但没有更新 GUI 只有一个被点击的按钮,但另一个不是。 问题是什么? 如何更容易识别动态按钮?
`
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;<p></p>
public class MainFrame extends JFrame {
public int[] table = {
2, 2, 2,
0, 1, 0,
1, 1, 0,
0, 1, 0,
3, 3, 3,};
public int click;
public int[] clicked = {-1, -1};
Color color[] = {Color.white, Color.lightGray, Color.red, Color.blue, Color.green};
public MainFrame() {
setLayout(new java.awt.GridLayout(5, 3));
setSize(400, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
for (int i = 0; i < 15; ++i) {
final JButton b = new JButton(String.valueOf(i));
add(b);
if (table[i] == 0) {
b.setEnabled(false);
b.setBackground(null);
}
b.setBackground(color[table[i]]);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent f) {
if (f.paramString().contains("cmd=0,")) {
click = 0;
} else if (f.paramString().contains("cmd=1,")) {
click = 1;
} else if (f.paramString().contains("cmd=2,")) {
click = 2;
} else if (f.paramString().contains("cmd=4,")) {
click = 4;
} else if (f.paramString().contains("cmd=6,")) {
click = 6;
} else if (f.paramString().contains("cmd=7,")) {
click = 7;
} else if (f.paramString().contains("cmd=10,")) {
click = 10;
} else if (f.paramString().contains("cmd=12,")) {
click = 12;
} else if (f.paramString().contains("cmd=13,")) {
click = 13;
} else if (f.paramString().contains("cmd=14,")) {
click = 14;
}
if (clicked[0] == -1) {
clicked[0] = click;
} else if (clicked[1] == -1) {
clicked[1] = click;
}
if (clicked[0] != -1 && clicked[1] != -1) {
int temp = table[clicked[0]];
table[clicked[0]] = table[clicked[1]];
table[clicked[1]] = temp;
b.setBackground(color[table[clicked[0]]]);
b.setBackground(color[table[clicked[1]]]);
clicked[0] = -1;
clicked[1] = -1;
}
}
}
);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MainFrame();
}
});
}
}
`
最佳答案
将每个按钮保存在一个数组中。稍后您可以使用数组的索引来引用按钮。
public final JButton[] buttons = new JButton[15];
您还可以使用 setActionCommand
在每个按钮上存储 ID(数组索引)。
for (int i = 0; i < 15; ++i)
{
buttons[i] = new JButton(String.valueOf(i));
buttons[i].setActionCommand(String.valueOf(i));
...
}
稍后您可以使用 getActionCommand
从 ActionEvent
中检索该 ID。
buttons[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent f)
{
click = Integer.parseInt(f.getActionCommand());
if (clicked[0] == -1) {
clicked[0] = click;
} else if (clicked[1] == -1) {
clicked[1] = click;
}
...
}
});
现在您已经能够识别按钮,您可以更正颜色交换逻辑以更改相应按钮上的颜色。
if (clicked[0] != -1 && clicked[1] != -1)
{
int temp = table[clicked[0]];
table[clicked[0]] = table[clicked[1]];
table[clicked[1]] = temp;
// get button references from array using index -> set swapped background color
buttons[clicked[0]].setBackground(color[table[clicked[0]]]);
buttons[clicked[1]].setBackground(color[table[clicked[1]]]);
clicked[0] = -1;
clicked[1] = -1;
}
关于Java 图形用户界面更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31168737/