写一个Java程序 要求通过在鼠标点击的位置显示一个绿色方块来响应鼠标点击
1个回答
展开全部
public class ClickTest extends JFrame implements MouseListener {
private int x=0;
private int y=0;
public ClickTest(){
setBounds(300,300,300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addMouseListener(this);
}
public void mouseClicked(MouseEvent e) {
x = e.getX()-3;
y = e.getY()-3;
repaint();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.GREEN);
g.fillRect(x, y, 6, 6);
}
public static void main(String[] args) {
new ClickTest();
}
}
private int x=0;
private int y=0;
public ClickTest(){
setBounds(300,300,300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addMouseListener(this);
}
public void mouseClicked(MouseEvent e) {
x = e.getX()-3;
y = e.getY()-3;
repaint();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.GREEN);
g.fillRect(x, y, 6, 6);
}
public static void main(String[] args) {
new ClickTest();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询