我正在编写一个图像拼图游戏,代码的一部分是将用户选择的碎片与正确图像的碎片进行比较。
每个图像片段都已作为 ImageIcon 添加到 JButton。
需要一个标识符来区分每个图像片段并进行比较。
我正在为每个创建为标识符的 JButton 设置一个 setText() 。
但是这样做会导致 ImageIcon 和 setText 都显示在 JButton 上。
有没有办法隐藏 setText 值并只显示 ImageIcon ?
private String id;
private int cc;
private JButton[] button = new JButton[9];
cc += 1;
id += ""+cc;
for(int a=0; a<9; a++){
// dd refers to the variable for the image pieces for each JButton
button[a].setIcon(new ImageIcon( dd ));
button[a].setText(id);
}
最佳答案
我建议创建另一个String
数组:
String[] ids = new String[button.length];
然后 button[a]
的 ID 将为 ids[a]
。
这是经过更改的代码:
private String id;
private int cc;
private JButton[] button = new JButton[9];
private String[] ids = new String[button.length];
//cc += 1;
//id += ""+cc;
id += Integer.toString(++cc); //easier way
for(int a=0; a<9; a++){
// dd refers to the variable for the image pieces for each JButton
button[a].setIcon(new ImageIcon( dd ));
ids[a] = id;
}
关于java - 隐藏 JButton 的 setText 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14415473/