漆黑魅影利欧路有用么:关于JAVA中鼠标单击按钮事件的问题

来源:百度文库 编辑:神马品牌网 时间:2024/05/05 08:25:17
工具是Jbuilder9.0,想知道鼠标单击的按钮是哪一个,并且把这个信息通过网络通信传递给另一台电脑,请问该用什么方法?
现在只想知道该用什么方法能获得鼠标单击的是哪一个按钮!!

给你一个例子,太难讲了
我自己写的
package guidemo;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
* <p>Title: 图形用户界面</p>
*
* <p>Description: 简单的图形界面编程</p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author vic
* @version 1.0
*/
public class ColorFrame extends Frame implements MouseListener {
Label L; //标签
TextField T; //文本域
Button B1, B2; //按钮
public ColorFrame() {
this.setLayout(null); //想要手动指定各组件的的位置
L = new Label("输入学号:"); //设定标签L内容
L.setBounds(60, 50, 50, 25); //设定标签L外观
this.add(L); //将标签L添加到窗口中
T = new TextField("请在这里输入"); //设定文本域T的内容
T.setBounds(125, 50, 90, 25); //设定文本域T的外观
this.add(T); //将文本域T添加到窗口中
B1 = new Button("变红!"); //设定按钮B1的内容
B1.setBounds(25, 90, 90, 25); //设定按钮B1的外观
B1.addMouseListener(this);//在B1上注册鼠标监听器
this.add(B1); //将按钮B1添加到窗口中
B2 = new Button("变绿!");
B2.setBounds(125, 90, 90, 25);
B2.addMouseListener(this);
this.add(B2);
WindowDestroyer Listener = new WindowDestroyer(); //创建关闭窗口监听器
this.addWindowListener(Listener); //将监听器添加到窗口中
this.setBackground(Color.yellow); //设定窗口背景颜色
this.setTitle("This is Frame!"); //设定窗口标题文字
this.setBounds(0, 0, 250, 220); //设定窗口位置和大小
this.setVisible(true); //显示窗口
}

public void mouseClicked(MouseEvent e) {
if (e.getComponent() == B1) {//getComponent返回按钮上面的字符串
this.setBackground(Color.red);
}
if (e.getComponent() == B2) {
this.setBackground(Color.green);
}
}

public void mouseExited(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

public void mousePressed(MouseEvent e) {}

public static void main(String[] args) {
new ColorFrame();
}
}