Java语言GUI鼠标点击一系列图形,判断是否和随机出现的数字匹配,并且计算得分?

Java语言GUI鼠标点击一系列图形,判断是否和随机出现的数字匹配,并且计算得分?

1个回答

package com.zepei;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;

// mouse listener action
public class TestMouseListener {
    public static void main(String[] args) {
        new MyFrame1("paint");
    }
}
class MyFrame1 extends Frame {
    // Collection store point
     ArrayList points;

    public MyFrame1(String title){
        super(title);
        setBounds(200,200,400,300);
        // store points
        points = new ArrayList<>();
        setVisible(true);
        // mouse listener to the window
        this.addMouseListener(new MyMouseListener());
    }

    @Override
    public void paint(Graphics g) {
        Iterator iterator = points.iterator();
        while(iterator.hasNext()){
            Point point = (Point)iterator.next();
            g.setColor(Color.GREEN);
            g.fillOval(point.x,point.y,10,10);
        }
    }
    // add a point to interface
    public void addPaint(Point point){
        points.add(point);
    }

    // inner calss
    private class MyMouseListener extends MouseAdapter {
        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame1 myFrame = (MyFrame1)e.getSource();
            // generate a point when click mouse
            myFrame.addPaint(new Point(e.getX(),e.getY()));
            myFrame.repaint(); // refresh
        }
    }
}


package com.listener;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestWindowListener {
    public static void main(String[] args) {
        new WindowFrame();
    }
}

class WindowFrame extends Frame {
    public WindowFrame(){
        setVisible(true);
        setBackground(Color.blue);
        setBounds(100,100,200,200);
        //addWindowListener(new MyWindowListener());
        this.addWindowListener(
                // 匿名内部类
                new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.out.println("YOU CLICKED ON CLOSE");
                        System.exit(0);
                    }

                    @Override
                    public void windowActivated(WindowEvent e) {
                        WindowFrame source = (WindowFrame)e.getSource();
                        source.setTitle("you are activated");
                        System.out.println("windowActivated");
                    }
                }
        );
    }

//    // inner class
//    class MyWindowListener extends WindowAdapter{
//        @Override
//        public void windowClosing(WindowEvent e) {
//            setVisible(false); // hide window using button
//            System.exit(0); // close window
//        }
//    }
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐