import javaawteventMouseMotionListener;
import javaxswingJButton;
import javaxswingJFrame;
public class BuTest implements MouseMotionListener{
JFrame jf=new JFrame("在按钮上移动的文字");
JButton[] button=new JButton[9];
int mount=0;
public void init(){
jfsetLayout(null);
jfsetSize(650, 100);
jfsetResizable(false);
jfsetLocationRelativeTo(null);
jfsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
initButton();
}
public void initButton(){
int x=10,y=10;
for(int i=0;i<buttonlength;i++){
button[i]=new JButton();
button[mount]setText("点我");
button[i]setBounds(x, y, 60, 30);
jfadd(button[i]);
button[i]addMouseMotionListener(this);
x+=70;
}
jfsetVisible(true);
}
public static void main(String arg[]) {
new BuTest()init();
}
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
if(egetSource()==button[mount]){
button[mount]setText("");
if(mount!=8){
mount++;
}else{
mount=0;
}
button[mount]setText("点我");
}
}
}其实就是鼠标事件就可以了。在执行鼠标事件的方法中判断即可
youbuttonaddMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent me){
//do something here
if( egetModifiersEx() == ( eBUTTON1_DOWN_MASK | eBUTTON3_DOWN_MASK) ){
Systemoutprintln("Both");
//这里是都按下的执行
}
}
});package comossnmsethercliente2eservicecommonpanel;
import javaawteventMouseEvent;
import javaawteventMouseListener;
import javaxswingJFrame;
import javaxswingJLabel;
public class Tt extends JFrame implements MouseListener{
/
@param args
/
public static void main(String[] args) {
}
/
为JLabel添加鼠标监听
@param labels
/
public void addMouseListener(JLabellabels) {
for(JLabel label:labels){
labeladdMouseListener(this);
}
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
写这么个添加监听的方法,将生成的JLabel传进去不就行了
1//例子12import javaapplet;import javaawt;
3import javaawtevent;
4public class Example18_1 extends Applet implements MouseListener
5{ TextField text;
6 public void init()
7 { text=new TextField(40); add(text);
8 addMouseListener(this) ;//向小程序增加鼠标事件监视器。
9 }
10 public void mousePressed(MouseEvent e)
11 { textsetText("鼠标键按下了,位置是"+egetX()+","+egetY() );
12 }
13 public void mouseReleased(MouseEvent e)
14 { textsetText(" 鼠标松开了,位置是"+egetX()+","+egetY() );
15 }
16 public void mouseEntered(MouseEvent e)
17 { textsetText(" 鼠标进来了,位置是"+egetX()+","+egetY() );
18 }
19 public void mouseExited(MouseEvent e)
20 { textsetText(" 鼠标走开了");
21 }
22 public void mouseClicked(MouseEvent e)
23 { if(egetClickCount()==2)
24 { textsetText("鼠标键双击,位置:"+egetX()+","+egetY());
25 }
26 else {}
27 }
28}
29
30//例子2
31import javaawt;import javaawtevent;
32class MyCanvas extends Canvas implements MouseListener
33{ int left=-1,right=-1; //记录左、右键用的变量。
34 int x=-1,y=-1; //记录鼠标位置用的变量。
35 MyCanvas()
36 { setSize(100,100);
37 setBackground(Colorcyan) ;
38 addMouseListener(this);
39 }
40 public void paint(Graphics g)
41 { if(left==1)
42 { gdrawOval(x-10,y-10,20,20);
43 }
44 else if(right==1)
45 { gdrawRect(x-8,y-8,16,16);
46 }
47 }
48 public void mousePressed(MouseEvent e)
49 { x=egetX(); y=egetY();
50 if(egetModifiers()==InputEventBUTTON1_MASK)
51 { left=1;right=-1;
52 repaint();
53 }
54 else if(egetModifiers()==InputEventBUTTON3_MASK)
55 { right=1; left=-1;
56 repaint();
57 }
58 }
59 public void mouseReleased(MouseEvent e){}
60 public void mouseEntered(MouseEvent e){}
61 public void mouseExited(MouseEvent e)
62 { left=-1;right=-1;
63 repaint();
64 }
65 public void mouseClicked(MouseEvent e){}
66 public void update(Graphics g)
67 { if(left==1||right==1)
68 { paint(g);
69 }
70 else
71 { superupdate(g);
72 }
73 }
74}
75public class Example18_2
76{ public static void main(String args[])
77 { Frame f=new Frame();
78 fsetBounds(100,100,200,200);fsetVisible(true);
79 faddWindowListener(new WindowAdapter() //适配器
80 {public void windowClosing(WindowEvent e)
81 {Systemexit(0);
82 }
83 });
84 fadd(new MyCanvas(),BorderLayoutCENTER);//添加画布。
85 fvalidate();
86 }
87}
88
89//例子3
90import javaawt;import javaawtevent;
91import javaapplet;
92public class Example18_3 extends Applet implements MouseListener
93{ TextField text; Button button;
94 TextArea textArea;
95 public void init()
96 { text=new TextField(10); textaddMouseListener(this);
97 button=new Button("按钮"); buttonaddMouseListener(this);
98 addMouseListener(this);
99 textArea=new TextArea(8,28);
100 add(button);add(text);add(textArea);
101 }
102 public void mousePressed(MouseEvent e)
103 { if(egetSource()==button)
104 {textAreaappend("\n在按钮上鼠标按下,位置:"+"("+egetX()+","+egetY()+")");
105 }
106 else if(egetSource()==text)
107 {textAreaappend("\n在文本框上鼠标按下,位置:"+"("+egetX()+","+egetY()+")");
108 }
109 else if(egetSource()==this)
110 {textAreaappend("\n在容器上鼠标按下,位置:"+"("+egetX()+","+egetY()+")");
111 }
112 }
113 public void mouseReleased(MouseEvent e) {}
114 public void mouseEntered(MouseEvent e) {}
115 public void mouseExited(MouseEvent e) {}
116 public void mouseClicked(MouseEvent e)
117 { if(egetClickCount()>=2)
118 textAreasetText("鼠标连击,位置:"+"("+egetX()+","+egetY()+")");
119 }
120}jTableaddMouseListener(new javaawteventMouseAdapter() {
public void mouseClicked(javaawteventMouseEvent e) {
if(jTablegetValueAt(jTablegetSelectedRow(),0)!=null)
{
String s = (String) jTablegetValueAtjTablegetSelectedRow(),0); //获取所选中的行的第一个位置的内容,当然你也可以指定具体的该行第几格
}
jtextfieldsetText(s);
repaint();
}
});jTableaddMouseListener(new javaawteventMouseAdapter() {
public void mouseClicked(javaawteventMouseEvent e) {
if(jTablegetValueAt(jTablegetSelectedRow(),0)!=null)
{
StringBuffer s = new StringBuffer ((String)jTablegetValueAtjTablegetSelectedRow(),0)); //获取所选中的行的第一个位置的内容,当然你也可以指定具体的该行第几格
for(int i=1;i<jTable的列数;i++){
sappend(((String)jTablegetValueAtjTablegetSelectedRow(),i));//把后面的几列加进来
}
jtextfieldsetText(stoString());
repaint();
}
});
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)