java中JLabel添加监听事件_[求助]关于JLabel添加监听器的问题。请各位帮忙!!

该博客讨论了在Java中为JLabel添加监听事件的问题,特别是遇到点击椭圆图标无响应的情况。作者展示了如何创建并添加MouseListener,以及在不同标签被点击时执行相应操作的代码实现。
摘要由CSDN通过智能技术生成

package paint;

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

/**

* @author Administrator

*

*/

public class Tools extends JPanel implements MouseListener{

private JLabel lineLabel ;

private JLabel rectLabel ;

private JLabel polyLabel ;

private JLabel ovalLabel ;

private JLabel rrectLabel;

private JLabel emLabel;

private JLabel emrLabel;

private JLabel fullLabel;

private JLabel textLabel ;

Icon lineBug = new ImageIcon( getClass().getResource( "image/bug1.JPG" ) );

Icon rectBug = new ImageIcon( getClass().getResource( "image/bug2.JPG" ) );

Icon polyBug = new ImageIcon( getClass().getResource( "image/bug3.JPG" ) );

Icon ovalBug = new ImageIcon( getClass().getResource( "image/bug4.JPG" ) );

Icon rrectBug = new ImageIcon( getClass().getResource( "image/bug5.JPG") );

Icon textBug = new ImageIcon( getClass().getResource( "image/bug6.JPG" ) );

Icon emBug = new ImageIcon(getClass().getResource( "image/embug.JPG" ));

//Icon emrBug = new ImageIcon(getClass().getResource( "image/emrbug.JPG" ));

//Icon fullBug = new ImageIcon(getClass().getResource( "image/fullbug.JPG" ));

private Handle handle;

static Color color;

public Tools(Handle handle){

this.handle = handle;

handle.setTools(this);

//setLayout(null);

this.setBackground( Color.WHITE);

this.setBounds(0,4,61,500);

lineLabel= new JLabel(lineBug);

add(lineLabel);

lineLabel.setVisible(true);

rectLabel= new JLabel(rectBug);

add(rectLabel);

rectLabel.setVisible(true);

polyLabel= new JLabel(polyBug);

add(polyLabel);

polyLabel.setVisible(true);

ovalLabel = new JLabel();

ovalLabel.setIcon(ovalBug);

add(ovalLabel);

ovalLabel.addMouseListener(this);

ovalLabel.setVisible(true);

rrectLabel= new JLabel(rrectBug);

add(rrectLabel);

rrectLabel.setVisible(true);

textLabel= new JLabel(textBug);

add(textLabel);

textLabel.setVisible(true);

}

public void mouseClicked(MouseEvent e){

Object o = e.getSource();

if(o == lineLabel){

}

else if(o == rectLabel){

}

else if(o ==polyLabel){

}

else if(o == ovalLabel){

handle.getDrawJpanel().setLineTag(true);

emLabel= new JLabel(lineBug);

add(emLabel);

setLayout(null);

emLabel.setVisible(true);

}

else if(o == rrectLabel){

}

else if(o == textLabel){

}

}

public void mousePressed(MouseEvent e){

}

public void mouseReleased(MouseEvent e){

}

public void mouseEntered(MouseEvent e){

}

public void mouseExited(MouseEvent e){

}

}

请问为什么我点了那个椭圆的图标,没有反应呢,我本来想点击的时候再下面再显示一个图标。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值