为什么我可以点击隐藏的JLabel?

时间:2015-08-06 07:05:13

标签: java click jlabel

我正在使用Java制作GUI 我正在使用JLabel并且使用此代码我给他们一些“onclick”事件:

myJLabel.addMouseListener(new MouseAdapter(){  
    public void mouseClicked(MouseEvent e){
       System.out.println("Yeah !!!!");
    }
});

它正在发挥作用,“是的!!!!”打印。
问题是如果我在“myJLabel”之上有另一个JLabel,我仍然可以点击“myJLabel”。
以下是一些图片来解释:

“myJLabel”是红色的JLabel。当我点击它时,粉红色的JLabel正在移动。所以一切都很好:D)
enter image description here
但如果粉红色的JLabel高于“myJLabel”,我仍然可以点击它:'(。 enter image description here
我的问题是:我该如何预防呢?

<小时/> 编辑:这是我的完整代码:

import javax.swing.*;
import javax.swing.border.*;
import javax.accessibility.*;

import java.awt.*;
import java.awt.event.*;

public class Test extends JPanel{
    private String[] layerStrings = { "Yellow (0)", "Magenta (1)", "Cyan (2)",   "Red (3)", "Green (4)" };
    private Color[] layerColors = { Color.yellow, Color.magenta, Color.cyan, Color.red, Color.green };

    private JLayeredPane layeredPane;
    private JLabel dukeLabel;

    public Test(){
        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

        //Create and set up the layered pane.
        layeredPane = new JLayeredPane();
        layeredPane.setPreferredSize(new Dimension(300, 310));
        layeredPane.setBorder(BorderFactory.createTitledBorder(""));


        JLabel label1 = createColoredLabel(layerStrings[1], layerColors[1], new Point(10, 10));
        JLabel label2 = createColoredLabel(layerStrings[2], layerColors[2], new Point(30, 30));

        label1.addMouseListener(new MouseAdapter(){  
            public void mouseClicked(MouseEvent e){
               System.out.println("Yeah !!!!");
            }
       });

        layeredPane.add(label1, new Integer(1));
        layeredPane.add(label2, new Integer(2));

        add(layeredPane);
    }

    //Create and set up a colored label.
    private JLabel createColoredLabel(String text, Color color, Point origin) {
        JLabel label = new JLabel(text);
        label.setVerticalAlignment(JLabel.TOP);
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setOpaque(true);
        label.setBackground(color);
        label.setForeground(Color.black);
        label.setBorder(BorderFactory.createLineBorder(Color.black));
        label.setBounds(origin.x, origin.y, 140, 140);
        return label;
    }

    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("LayeredPaneDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        JComponent newContentPane = new Test();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

在事件功能中添加此命令。基本上说,如果没有显示,什么都不做。

if(!this.isShowing())return;