java - 隐藏 JButton 的 setText 值

标签 java swing hide jbutton

我正在编写一个图像拼图游戏,代码的一部分是将用户选择的碎片与正确图像的碎片进行比较。

每个图像片段都已作为 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/

相关文章:

java - 为什么在数据库 android 中进行查询后调用 moveToFirst()

java - 带 jdk5 的 helios 中的 windowbuilder 不起作用

javascript - 使用jquery隐藏div

html - 你如何从搜索引擎中隐藏部分网站

java - 如何在java中更新ComBobox的值?我正在使用 Netbeans

java - JVM 一遍又一遍地进行次要垃圾收集,从不抛出 OutOfMemoryError

java - 如何在测试容器上公开容器之间的端口?

java - JTable 设置特定单元格不可编辑?

java - 我将如何从 JPanel 中动态删除 swing 组件

C# 卸载和加载窗体/隐藏和关闭