【问题标题】:Why my mouseClicked() counter increments by 2?为什么我的 mouseClicked() 计数器增加 2?
【发布时间】:2014-11-08 15:55:14
【问题描述】:

我正在尝试计算鼠标点击次数,但我不明白为什么我的计数器每次点击都会增加 2。试过getClickCount(),但也不是我需要的。

计数后我的目标:我会使用计数器以不同的点击次数绘制不同的东西。假设第 1 次和第 2 次将始终获得 drawLine() 的坐标,而第 3 次单击将获得 drawRect()。

package graphics_training_painting;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;

public class U4 extends Canvas implements MouseListener{
    private int x1;
    private int y1;
    private int x2;
    private int y2;
    private int counter = 0;

    public U4() {
        setBackground(Color.white);
    }

    public static void main(String[] args) {
        U4 u = new U4();
        JFrame f = new JFrame();
        f.add(u);
        f.setSize(800, 600);
        f.setVisible(true);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        counter++;
        System.out.println(counter);
    }

    @Override
    public void mouseEntered(MouseEvent e) {


    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
/*      x1 = e.getX();
        y1 = e.getY();*/
    }

    @Override
    public void mouseReleased(MouseEvent e) {
/*      x2 = e.getX();
        y2 = e.getY();
        repaint();*/        
    }

    public void paint(Graphics g) {
        addMouseListener(this);
        g.setColor(Color.blue);
        g.drawLine(x1, y1, x2, y2);

    }   
}

非常感谢您的帮助或建议, 提米!

【问题讨论】:

    标签: java swing graphics mouseclick-event click-counting


    【解决方案1】:

    不要在你的paint方法中添加你的MouseListener!这将添加许多侦听器,每个侦听器在激活时递增计数器。

    您需要知道您无法控制何时或是否调用paint,并且在典型的程序运行期间可能会多次调用它。由于这个原因和其他原因,您不应将程序逻辑、状态更改代码或组件创建放在此方法中。在调用 once 的一些初始化代码(例如构造函数)中添加您的 MouseListener。

    顺便说一句,您不希望像现在这样混合 AWT 和 Swing 组件。相反,您应该让您的 U4 类扩展 JPanel,并在其 paintComponent 方法中进行绘图。

    所以,改变这个:

    public U4() {
        setBackground(Color.white);
    }
    
    
    // ...
    
    public void paint(Graphics g) {
        addMouseListener(this);
        g.setColor(Color.blue);
        g.drawLine(x1, y1, x2, y2);
    } 
    

    到这里:

    public U4() {
        setBackground(Color.white);
        addMouseListener(this);
    }
    
    
    // ...
    
    public void paint(Graphics g) {
        //  addMouseListener(this);
        super.paint(g); 
        g.setColor(Color.blue);
        g.drawLine(x1, y1, x2, y2);
    } 
    

    接下来,进行我推荐的更改

    类似:

    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.Stroke;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    import javax.swing.*;
    
    public class U4b extends JPanel {
       private static final Color BG = Color.white;
       private static final Color DRAW_COLOR = Color.red;
       private static final int PREF_W = 800;
       private static final int PREF_H = 600;
       private static final Stroke BASIC_STROKE = new BasicStroke(3f);
       private int counter = 0;
       private int x1 = 0;
       private int y1 = 0;
       private int x2 = 0;
       private int y2 = 0;
    
       public U4b() {
          setBackground(BG);
          MyMouseListener myMouseListener = new MyMouseListener();
          addMouseListener(myMouseListener);
          addMouseMotionListener(myMouseListener);
       }
    
       @Override
       public Dimension getPreferredSize() {
          if (isPreferredSizeSet()) {
             return super.getPreferredSize();
          }
          return new Dimension(PREF_W, PREF_H);
       }
    
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D) g;
          g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
          g2.setColor(DRAW_COLOR);
          g2.setStroke(BASIC_STROKE);
          g2.drawLine(x1, y1, x2, y2);
       }
    
       private class MyMouseListener extends MouseAdapter {
          @Override
          public void mousePressed(MouseEvent e) {
             counter++;
             System.out.println("Counter: " + counter);
             x1 = e.getX();
             y1 = e.getY();
             x2 = x1;
             y2 = y1;
          }
    
          @Override
          public void mouseDragged(MouseEvent e) {
             x2 = e.getX();
             y2 = e.getY();
             repaint();
          }
    
          @Override
          public void mouseReleased(MouseEvent e) {
             x2 = e.getX();
             y2 = e.getY();
             repaint();
          }
    
       }
    
       private static void createAndShowGui() {
          U4b mainPanel = new U4b();
    
          JFrame frame = new JFrame("U4b");
          frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    

    【讨论】:

    • 非常感谢!现在将开始吸收它。 :-)
    猜你喜欢
    • 2021-05-17
    • 2018-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    • 2011-11-10
    • 1970-01-01
    • 2022-11-24
    相关资源
    最近更新 更多