Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何判断哪个项目触发了鼠标侦听器_Java_Swing_Mouseevent_Jlabel_Mouselistener - Fatal编程技术网

Java 如何判断哪个项目触发了鼠标侦听器

Java 如何判断哪个项目触发了鼠标侦听器,java,swing,mouseevent,jlabel,mouselistener,Java,Swing,Mouseevent,Jlabel,Mouselistener,大家好 我正在尝试编写一个简单的星级组件。我对Java语言相当陌生,我不确定我想要完成的事情是否可以用Java来完成。我是否可以在JLabel数组中添加一个JLabel,数组中的每个JLabel都有一个鼠标事件侦听器。现在是否可以设置它,以便当鼠标事件在标签[3]上触发时,我可以获得它的索引值 下面是我如何建立我的面板 public Rating(Integer max,int size) { JLabel position = new JLabel[max]; this.set

大家好

我正在尝试编写一个简单的星级组件。我对Java语言相当陌生,我不确定我想要完成的事情是否可以用Java来完成。我是否可以在JLabel数组中添加一个JLabel,数组中的每个JLabel都有一个鼠标事件侦听器。现在是否可以设置它,以便当鼠标事件在标签[3]上触发时,我可以获得它的索引值

下面是我如何建立我的面板

public Rating(Integer max,int size) {

JLabel  position = new JLabel[max];


    this.setLayout(new FlowLayout());
    for(int i=0; i != max;i++){
    position[i]=new JLabel(icons.getIcon("star-empty", size));
    position[i].setOpaque(true);
    position[i].addMouseListener(this);
    add(position[i]);
    }
}


@Override
public void mouseEntered(MouseEvent e) {
    JLabel a= (JLabel) e.getComponent();
    //****Have some code in here to tell me where in the position array the event came from????***
    int index = ?????
   }
请给我一些想法/想法/建议

注:我曾想过使用按钮,但它看起来很凌乱,希望能找到一种使用图像图标的方法


谢谢。

只要知道JButtons可以按你喜欢的方式显示。它们可以有图像图标,甚至不必看起来像按钮。

我通常的做法是:

int i;
for (i = 0; i <max; i++)
  if (position[i] == e.getcomponent())
    break;
inti;

对于(i=0;i您可以使用其setName方法根据索引命名每个JLabel,然后使用MouseEvent的getComponent方法获取原始JLabel,在其上使用getName,这就是您的索引。这是一种方法,但需要将索引信息存储在两个位置(隐式地表示它在数组中的位置,并显式地表示为标签的名称),因此它非常希望出现不一致性


您也可以在数组中搜索从getComponent获得的JLabel引用,但这也不是很好,尤其是对于大型数组。

为什么索引很重要?您知道如何获取组件,所以只需在数组中循环以获取索引

注:我曾想过使用按钮,但它看起来很凌乱,希望能找到一种使用图像图标的方法

使用按钮如何解决确定索引的问题?但是,我也同意使用按钮比使用标签好,然后您将使用ActionListener而不是MouseListener。您可以使用以下方法使按钮看起来像标签:

button.setBorderPainted( false );

现在,如果您使用ActionListener,您可以使用setActionCommand(…)方法存储按钮的索引值。然后在事件中使用getActionCommand(…)方法。

而不是像您那样为每个标签使用相同的侦听器:

position[i].addMouseListener(this);
…您可以创建一个特殊的侦听器类,该类接受索引号,并允许您以后查找它:

position[i].addMouseListener(new RatingMouseListener(i));
每个标签都有一个单独的侦听器实例,其索引值不同。内部类的代码如下所示:

private class RatingMouseListener extends MouseAdapter {
    private final int index;

    public RatingMouseListener(int index) {
        this.index = index;
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        System.out.println("Mouse entered for rating " + index);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        System.out.println("Mouse exited for rating " + index);
    }
}
然后,您只需重写MouseAdapter中的任何方法


此外,正如其他人所说,您可能希望使用
JButton
s而不是
JLabel
s,因为它们对动作事件有更好的支持。您仍然可以为它们提供图标。

索引之所以重要,是因为任何小于或等于单击的索引都需要设置为“true”因此,我可以绘制一个填充的开始,并可以返回其他用途的评级。因此,您可以在按钮数组中循环,并将每个按钮的图标设置为包括已单击的按钮。无论如何,您已经得到了如何执行此操作的答案。