dabocaiqq
2020-08-02 10:26
采纳率: 76.2%
浏览 58

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

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

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • 这不是鸭头 2020-08-05 10:42
    最佳回答
    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
    //        }
    //    }
    }
    
    
    评论
    解决 无用
    打赏 举报