mouseListener仅识别JLabel

时间:2018-10-29 13:24:11

标签: java swing mouselistener

我在包含几个组件的JPanel上添加了mouseListener。 为了测试mouseListener,我只在进入和退出jpanel时才设置jpanel的setBorder。 问题是,它只能识别jpanel的jlabel上的鼠标事件,而不能识别其他组件或主jpanel本身。不知道为什么...

这是代码:

     import javax.swing.JFrame;


public class compte2 extends javax.swing.JPanel {


    public compte2() {
        initComponents();
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();

        setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        setPreferredSize(new java.awt.Dimension(240, 160));
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                formMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                formMouseExited(evt);
            }
        });
        setLayout(new java.awt.BorderLayout());

        jPanel1.setMinimumSize(new java.awt.Dimension(100, 56));
        jPanel1.setPreferredSize(new java.awt.Dimension(400, 56));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("CLUB");
        jLabel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jLabel1.setPreferredSize(new java.awt.Dimension(240, 28));
        jPanel1.add(jLabel1, java.awt.BorderLayout.PAGE_START);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("ILLIMITE");
        jLabel3.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jLabel3.setPreferredSize(new java.awt.Dimension(190, 28));
        jPanel1.add(jLabel3, java.awt.BorderLayout.CENTER);

        jButton2.setIcon(new javax.swing.ImageIcon("C:\\Users\\pc\\Downloads\\icon\\Pics\\see.png")); // NOI18N
        jButton2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jButton2.setMinimumSize(new java.awt.Dimension(28, 28));
        jButton2.setPreferredSize(new java.awt.Dimension(28, 28));
        jPanel1.add(jButton2, java.awt.BorderLayout.LINE_END);

        add(jPanel1, java.awt.BorderLayout.PAGE_START);

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel2.setMinimumSize(new java.awt.Dimension(100, 28));
        jPanel2.setPreferredSize(new java.awt.Dimension(400, 28));
        jPanel2.setLayout(new java.awt.BorderLayout());

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText("€");
        jLabel4.setPreferredSize(new java.awt.Dimension(15, 28));
        jPanel2.add(jLabel4, java.awt.BorderLayout.LINE_END);

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel6.setText("0,00");
        jLabel6.setMaximumSize(new java.awt.Dimension(50, 28));
        jLabel6.setPreferredSize(new java.awt.Dimension(50, 28));
        jPanel2.add(jLabel6, java.awt.BorderLayout.CENTER);

        jButton1.setText("ENCAISSER");
        jButton1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jButton1.setMinimumSize(new java.awt.Dimension(80, 28));
        jButton1.setPreferredSize(new java.awt.Dimension(80, 28));
        jPanel2.add(jButton1, java.awt.BorderLayout.LINE_START);

        add(jPanel2, java.awt.BorderLayout.PAGE_END);
        add(jScrollPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>                        

    private void formMouseEntered(java.awt.event.MouseEvent evt) {                                  
        compte2 mc = (compte2)evt.getSource();
    mc.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 0, 0), 4));
    }                                 

    private void formMouseExited(java.awt.event.MouseEvent evt) {                                 
       compte2 mc = (compte2)evt.getSource();
    mc.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
    }                                


    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration                   



    public static void main(String[] args) {

        JFrame j = new JFrame();
        compte2 mc = new compte2();
        j.add(mc);
        j.pack();
        j.setVisible(true);
}

}

1 个答案:

答案 0 :(得分:0)

您的问题是,您将子面板添加到主面板中时,没有按预期生成事件。

在您的mouseEntered / Exited方法中添加一些调试代码,以便您查看何时生成事件。

更改代码以使面板具有背景色,以便您可以看到鼠标何时进入/退出面板:

django-filter==2.0.0

我从不使用IDE来生成代码,我也不知道GroupLayout在做什么。因此,您会得到奇怪的行为,因为子组件之间存在间隙。因此,当您在面板之间垂直移动鼠标时,您会看到变化。

按钮和标签已添加到子组件中,因此,如果您在子面板上水平鼠标单击,它们将不会生成事件。