总结::监听到底该怎么用
事件的区分是靠判断还是
package com.a.b; //我想实现,当我点击一个按钮时,这个frame里可以画出实心的矩形 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.awt.event.MouseListener; import java.util.Random; import javax.swing.*; class Biao extends JFrame { int x, y; JButton jb; public Biao() { ini(); this.setBackground(Color.red);// 不就 this.setDefaultCloseOperation(3); this.setSize(500, 500); // 设置界面 // 看到冒,默认窗体是不显示,但是我没有设置 this.setVisible(true); jb = new JButton("yes"); // jb.addActionListener(this); jb.addActionListener(new A()); } public class A implements ActionListener { public void actionPerformed(ActionEvent e) { } } Random r = new Random(); public void paint(Graphics g) { g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255))); g.drawOval(x, y - 44, 44, 44);// 画一个空心圆 // 这里需要剪个半径,否则圆会默认跑出来 // g.fillRect(x, y, 44, 44); g.fillOval(x, y, 55, 55);// 画圆 // g.fillOval(x, y, 44, 33);//画个椭圆试试 g.drawLine(x, y, 44, 44);// 画直线 } public void ini() { // z // 实现当我鼠标点击时,会画图 //这是一个匿名的内部类 this.addMouseListener(new MouseListener() {// 这个方法是自动导入的 @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { if (e.getClickCount() >= 2) { x = e.getX(); y = e.getY(); repaint();// 重新画图啊 } } }); } } public class South { public static void main(String[] args) { new Biao(); } }