在JPanel上使用MouseClick检测到JLabel

时间:2016-01-16 16:10:29

标签: java swing user-interface mouse

我正在使用netbeans,我动态地将带有形状的标签添加到我的jpanel中。但是,我在我的面板上添加了一个mouselistener,但它只检测鼠标不在jlabel上时的动作?有没有办法在我的mouseListener中将jLabel包含在我的jPanel中,而不向每个单独的jLabel添加监听器?

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
   if (segue.identifier == "mySegueID") {
       // in this line you need to set the name of the class of your `UITableViewController` 
       let viewController = segue.destinationViewController as UITableViewController
       / here you need to pass the data using the reference viewController
   }
}

我通过对jpanel

执行常规来创建我的jLabel
private void jPanel2MouseClicked(java.awt.event.MouseEvent evt) {                                     
    for(int a = 0; a< states.size(); a++) {
        if(states.get(a).contains(evt.getPoint())) {
            if(states.get(a).getIcon() == rectangleIcon) {
                if(yellow == true) {
                    states.get(a).setIcon(yellow2Icon);
                    yellow = false;
                } else if (yellow != true){
                    states.get(a).setIcon(yellowIcon); 
                    jPanel2.revalidate();
                    jPanel2.repaint();
                    yellow = true;
                }
            } else {
                states.get(a).setIcon(rectangleIcon);
                yellow = false;
            }
            x2 = evt.getPoint();
        } 
    }
}    

1 个答案:

答案 0 :(得分:0)

尝试通过在其中某处粘贴print行来确保此功能甚至被调用。像这样:

private void jPanel2MouseClicked(java.awt.event.MouseEvent evt) {
    System.out.println("this method was called");                                 
    for(int a = 0; a < states.size(); a++) {
        if (states.get(a).contains(evt.getPoint())) {
            if (states.get(a).getIcon() == rectangleIcon) {
                if (yellow)
                    states.get(a).setIcon(yellow2Icon);
                else {
                    states.get(a).setIcon(yellowIcon); 
                    jPanel2.revalidate();
                    jPanel2.repaint();
                }
                yellow = !yellow;
            } else {
                states.get(a).setIcon(rectangleIcon);
                yellow = false;
            }
            x2 = evt.getPoint();
        } 
    }
}