【问题标题】:Comparing components via setName() .通过 setName() 比较组件。
【发布时间】:2013-01-22 07:24:40
【问题描述】:

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

每个图像片段都已作为 ImageIcon 添加到 JButton。

需要一个标识符来区分每个图像片段并进行比较。

我正在为每个创建为标识符的 JButton 设置一个 setName()。

当用户将拼图块从原来的 3x3 网格拖动到另一个 3x 网格进行匹配后,比较就开始了。

我在从比较 if 语句中删除错误时遇到问题。

我从这个 SO 线程中得到了比较的想法 - link

    private JButton[] button = new JButton[9];
    private JButton[] waa = new JButton[9];

    private String id;
    private int cc;
    private String id2;
    private int cc2;

    // setName for each of the 9 buttons in the original 3x3 grid being created 
    // which stores the shuffled puzzle pieces
    for(int a=0; a<9; a++){
        button[a] = new JButton(new ImageIcon());
        id += Integer.toString(++cc);
        button[a].setName(id); 
    }

    // setName for each of the 9 buttons in the other 3x3 grid  
    // where the images will be dragged to by the user
        for(int b=0; b<9; b++){
        waa[b] = new JButton();
        id2 += Integer.toString(++cc2);
        waa[b].setName(id2); 
    }

    // check if puzzle pieces are matched in the correct place
    // compare name of original 'button' array button with the name of 'waa' array buttons 
        button[a].addMouseListener(new MouseAdapter(){

            public void mouseReleased(MouseEvent m){
                if(m.getbutton().getName().equals (waa.getName())){

                    }
                    else{
                         JOptionPane.showMessageDialog(null,"Wrong! Try Again.");
                    }
            }
        }

【问题讨论】:

    标签: java swing compare


    【解决方案1】:

    在您的mouseReleased 事件中,m.getButton() 正在返回被单击的鼠标按钮。你会想做更多这样的事情来让你更接近:

    if (m.getComponent().getName().equals(waa.getName())) {
    

    m.getComponent() 返回触发事件的Component 对象(您的JButton)。从那里您可以与您正在使用的getName 方法进行比较。

    还有一个问题是您的waa 变量是一个数组。我不确定您想如何比较它们,是否遍历数组并确保索引和名称匹配,但这是您需要研究的另一个问题。

    【讨论】:

    • 如果您不关心鼠标事件是否发生在按钮上,MouseEvent 包含一个 getComponent 方法,它应该允许放弃强制转换(至少您确定要这样做)。就个人而言,我还会检查以确保 getName 没有返回空值
    • 其实我在想,为什么我们要在按钮上使用鼠标监听器?
    • 关于getComponent 的好点,我更新了代码。鼠标监听器也是不必要的,可以用addActionListener 来完成。上面的代码显然存在一些问题,但希望这将使 OP 朝着正确的方向开始。
    • 嗯,没错,作为数组的 waa 变量是个问题。 if 语句不知道要使用哪个 getName。试图找出是否有 getDestinationComponentName() 方法哈哈。
    【解决方案2】:

    JButton 使用ActionListener 触发通知返回到您的程序,以指示何时触发。这允许按钮响应不同类型的事件,包括鼠标、键盘和程序触发器。

    作为动作 API 的一部分,您可以为每个按钮提供一个动作命令。见JButton#setActionCommand

    基本上你会以一种类似的方式将它集成到你的鼠标监听器中......

    public void actio Performed(ActionEvent evt) {
        if (command.equals(evt.getActionCommand()) {...}
    }
    

    根据您的要求,使用Action API 可能会更容易

    您实际遇到的问题是 waa 是一个数组,因此它没有 getName 方法。我也不清楚为什么你有两个按钮阵列?

    【讨论】:

    • 哦,第一个 9 个按钮数组是用于显示存储为 ImageIcons 的 9 个打乱的片段。第二组 9 个按钮供用户拖动。
    • @iridescent:听起来好像您不是将 JButtons 用作真正的按钮,而是用作保存 JLabels 的存储库。那为什么要使用 JButtons 呢?为什么不简单地创建并使用一个 JLabels 数组来保存 ImageIcon?
    • 您也可以根据需要使用拖放来移动图标。 1+ 到 Mad 顺便说一句。
    • 哦,这很好。我实际上使用 JButtons 作为存储库,因为空的 JButtons 与空的 JLabels 相比具有“蓝色外观”,后者是白色的。最初,这些片段没有显示。
    猜你喜欢
    • 2021-11-18
    • 1970-01-01
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 2016-02-03
    相关资源
    最近更新 更多