问一个关于swing的问题,有没有见过ActionListener,MouseListener这两个事件冲突的?

hanjs 2008-01-28 09:29:19
package socket;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

import javax.swing.*;

public class UserUI extends JFrame implements ActionListener,MouseListener {

public boolean notshow = true;

private JLabel email = null;

public UserUI() {
super("");
this.setSize(new Dimension(260, 140));
this.setResizable(false);
this.setUndecorated(true);
this.addMouseListener(this);
// this.addMouseListener(new MouseAdapter(){
// public void mouseClicked(MouseEvent e){
// if (e.getSource().equals(email)){
//
// }
// }
// public void mouseEntered(MouseEvent e){
// //notshow = false;
// }
// public void mouseExited(MouseEvent e){
// UserUI.this.setVisible(false);
// //notshow = true;
// }
// });
// this.addMouseMotionListener(new MouseMotionAdapter(){
// public void mouseMoved(MouseEvent e){
//
// }
// });
}

public void showUser(User user) {
JPanel main = new JPanel(null);
//main.setLayout(null);
main.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
Icon icon = user.getPhoto();
JLabel label = new JLabel();
label.setHorizontalAlignment(SwingConstants.LEFT);
label.setVerticalAlignment(SwingConstants.TOP);
//label.setVerticalTextPosition(SwingConstants.TOP);
//label.setHorizontalTextPosition(SwingConstants.LEFT);
label.setText(user.getAccount());
label.setBounds(5, 5, 140, 105);
JLabel photo = new JLabel();
photo.setIcon(icon);
photo.setBounds(this.getWidth() - icon.getIconWidth() - 5, 5, icon.getIconWidth(), icon.getIconHeight());
main.add(label);
main.add(photo);
//增加分隔线
JButton email = new JButton(user.getEmail());
//email = new JLabel(user.getEmail());
email.setVerticalAlignment(SwingConstants.CENTER);
email.setHorizontalAlignment(SwingConstants.LEFT);
email.setBorderPainted(false);
email.setFocusable(false);
email.setContentAreaFilled(false);
email.setBounds(5, 110, 260, 30);
email.addActionListener(this);
//会覆盖上面的监听
// email.addActionListener(new ActionListener(){
// public void actionPerformed(ActionEvent e){
// JOptionPane.showMessageDialog(null,((JLabel)e.getSource()).getText());
// }
// });
main.add(email);
this.getContentPane().add(main);

//this.pack();
}

public void mouseClicked(MouseEvent e) {
//e.getComponent();
if (e.getComponent() == email){
JOptionPane.showMessageDialog(null,email.getText());
}
}

public void mousePressed(MouseEvent e) {

}

public void mouseReleased(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {
//notshow = false;
}

public void mouseExited(MouseEvent e) {
this.setVisible(false);
//notshow = true;
}

public void actionPerformed(ActionEvent e) {
String email = ((JLabel) e.getSource()).getText();
if (email != null && !"".equals(email)) {
try {
Runtime.getRuntime().exec("cmd /c start" + " mailto:hanjias@gmail.com");
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}

}
}

主要目的是

在这个窗口有鼠标enter就不做处理,当鼠标exit的时候需要设置为不显示。

下面的email按钮是调用本地发送邮件,可是在鼠标滑过按钮的时候,窗口就被设置为不可见了。

将email改为label就可以,但是触发不了发送邮件啊?其实最理想的就是email用超链接的形式/
...全文
257 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
hanjs 2008-01-29
  • 打赏
  • 举报
回复
不理想的解决了

整个窗口加一个鼠标监听,而通过鼠标点击的位置来判断是否点击了email,不过email不能使用button,而用label
hanjs 2008-01-28
  • 打赏
  • 举报
回复
up
hanjs 2008-01-28
  • 打赏
  • 举报
回复
论坛斑竹都不帮忙的么?

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧