public class MouseClickEvent extends JFrame{
public MouseClickEvent() {
Container container = getContentPane();
containeraddMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent arg0) {
int buttonFlag = arg0getButton();
switch (buttonFlag) {
case MouseEventBUTTON1:
Systemoutprintln("释放的是鼠标左键!\n");
break;
case MouseEventBUTTON2:
Systemoutprintln("释放的是鼠标滚轮!\n");
break;
case MouseEventBUTTON3:
Systemoutprintln("释放的是鼠标右键!\n");
break;
}
}
@Override
public void mousePressed(MouseEvent arg0) {
int buttonFlag = arg0getButton();
switch (buttonFlag) {
case MouseEventBUTTON1:
Systemoutprintln("按下的是鼠标左键!\n");
break;
case MouseEventBUTTON2:
Systemoutprintln("按下的是鼠标滚轮!\n");
break;
case MouseEventBUTTON3:
Systemoutprintln("按下的是鼠标右键!\n");
break;
}
}
@Override
public void mouseExited(MouseEvent arg0) {
Systemoutprintln("光标移出应用程序窗口了!\n");
}
@Override
public void mouseEntered(MouseEvent arg0) {
Systemoutprintln("光标进入应用程序窗口了!\n");
}
@Override
public void mouseClicked(MouseEvent arg0) {
int buttonFlag = arg0getButton();
switch (buttonFlag) {
case MouseEventBUTTON1:
Systemoutprintln("点击的是鼠标左键!\n");
break;
case MouseEventBUTTON2:
Systemoutprintln("点击的是鼠标滚轮!\n");
break;
case MouseEventBUTTON3:
Systemoutprintln("点击的是鼠标右键!\n");
break;
}
int clickCount = arg0getClickCount();
Systemoutprintln("点击次数为"+clickCount+"\n");
}
});
}public static void main(String[] args) {
MouseClickEvent testFrame = new MouseClickEvent();
testFramesetTitle("鼠标点击事件测试程序!");
testFramesetVisible(true);
testFramesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
testFramesetBounds(0, 0, 500, 400);
}
}//类名 MyMouseLister
import javaawteventMouseEvent;
import javaawteventMouseListener;
public class MyMouseLister implements MouseListener{
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
}
//类名OnMouseOver
import javaawtGridLayout;
import javaawteventMouseEvent;
import javaxswingJFrame;
import javaxswingJLabel;
import javaxswingJPanel;
public class OnMouseOver extends MyMouseLister{
private JLabel test1;
private JLabel test2;
private JLabel test3;
private JLabel test4;
private JPanel p;
private JFrame f;
public OnMouseOver(){
test1 = new JLabel("test1text");
test1addMouseListener(this);
test2 = new JLabel("test2text");
test2addMouseListener(this);
test3 = new JLabel("test3text");
test3addMouseListener(this);
test4 = new JLabel("test4text");
test4addMouseListener(this);
p = new JPanel(new GridLayout(2,2));
padd(test1,0);
padd(test2,1);
padd(test3,2);
padd(test4,3);
f = new JFrame("Test");
fadd(p);
fsetVisible(true);
fsetBounds(300, 200, 300, 200);
fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
}
//事件可以自己换
public void mouseEntered(MouseEvent e) {
if(egetSource() == test1){
Systemoutprintln(test1getText());
}else if(egetSource() == test2){
Systemoutprintln(test2getText());
}else if(egetSource() == test3){
Systemoutprintln(test3getText());
}else if(egetSource() == test4){
Systemoutprintln(test4getText());
}
}
public static void main(String[] args) {
new OnMouseOver();
}
}
希望能帮到你虽然这个是JLabel但是道理是相通的,你看看就明白了。
//附上 *** 作表格的方法:
getSelectedColumn()返回列索引
getSelectedRow()返回行索引
getValueAt()返回单元格的值
用该类进行读写文件。。
RandomAccessFile
void seek(long pos)
设置到此文件开头测量到的文件指针偏移量,在该位置发生下一个读取或写入 *** 作。
可以设置 pos的值为 0 就行了。。
自己去查一下API文档可以更好的理解。。
祝你好运。。。。
以上就是关于java中MouseEvent事件例子全部的内容,包括:java中MouseEvent事件例子、JAVA中如何获得当前鼠标选中的值、Java读取文件定位光标等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)