日期:2014-05-20 浏览次数:20978 次
static class Point extends JPanel {
private int x = 0;
private int y = 0;
private String str = " ";
private boolean clear;
public Point() {
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
clear = false;
x = e.getX();
y = e.getY();
str = "(" + x + "," + y + ")";
repaint();
}
public void mouseReleased(MouseEvent e) {
// 当鼠标释放时清除显示的坐标,即显示为空面板
clear = true;
repaint();
}
});
}
protected void paintComponent(Graphics g) {
if (!clear) {
super.paintComponents(g);
g.drawString(str, x, y);
}
else {
g.clearRect(0, 0, getWidth(), getHeight());
}
}
}
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
public class MouseColor extends JFrame{
public MouseColor(){
Point p=new Point();
add(p);
}
public static void main(String[] args){
MouseColor frame=new MouseColor();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setSize(500,400);