[java]代码库
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
public class T1 extends JFrame{
int x;
int y;
Color c;
public T1(){
this.setSize(600,400);
this.setDefaultCloseOperation(2);
this.setVisible(true);
this.addMouseListener(shubiaodianji());
}
public void paint(Graphics g){
super.paint(g);
g.setColor(c);
g.fillOval(x-100, y-100, 200, 200);
}
private MouseListener shubiaodianji() {
MouseListener m = new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// 鼠标松开的那一瞬间
}
@Override
public void mousePressed(MouseEvent e) {
// 鼠标按下的那一瞬间
if(e.getButton()==MouseEvent.BUTTON1){
c = Color.red;
setTitle("左键");
}else if(e.getButton()==MouseEvent.BUTTON2){
c = Color.orange;
setTitle("中键");
}else if(e.getButton()==MouseEvent.BUTTON3){
c = Color.cyan;
setTitle("右键");
}else{
setTitle("不知道啥按键");
}
x=e.getX();
y=e.getY();
repaint();//重新绘图
}
@Override
public void mouseExited(MouseEvent e) {
// 鼠标退出JFrame界面的那一瞬间
}
@Override
public void mouseEntered(MouseEvent e) {
// 鼠标计入JFrame的那一瞬间
}
@Override
public void mouseClicked(MouseEvent e) {
// 鼠标点击事件按钮
}
};
return m;
}
public static void main(String[] args) {
new T1();
}
}
by: 发表于:2017-07-07 15:47:21 顶(0) | 踩(0) 回复
??
回复评论