总结:

使用匿名类,但是用实现接口的方式呢???

package com.aa;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
//这是要实现画圈
import javax.swing.*; class Yuan extends JFrame {
// 然后构造图形化用户界面
int x, y; public Yuan() {
ini();
this.setSize(500, 500);
this.setVisible(true);
this.setDefaultCloseOperation(3);
this.setBackground(Color.yellow); } public void paint(Graphics g) {
Random r = new Random();
g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
g.fillOval(x, y, 49, 49);
g.drawRect(x, y, 40, 40);
} public void ini() {
this.addMouseListener(new MouseAdapter() { // 这个方法是鼠标监听,当我们点击一下,画圆饿了
public void mouseClicked(MouseEvent m) { if (m.getClickCount() >= 2) {
// 获取这个圈的坐标
x = m.getPoint().x;
y = m.getPoint().y;
repaint();// 没有这个方法,就画不出来 好了,双击时出现一个圆
} } }); } public void actionPerformed(ActionEvent e) { } } public class Texam {
public static void main(String[] args) {
// 一般是个主类
new Yuan(); }
}

  

05-15 07:57