使用Java Swing,mouseEntered被mouseMoved所掩盖。这有一个简单的解决方案吗?

时间:2012-12-01 17:35:18

标签: java swing jpanel drawstring mousemotionevent

基本上,我必须简单地绘制“鼠标输入”文本,然后是其坐标。我的代码在未实现mouseMoved时成功执行此操作。如果是,它从不显示鼠标输入并直接进入“鼠标移动到”。我可以理解mouseMoved如何做到这一点,因为进入面板也是在该位置移动鼠标。我已经尝试将这些移动存储在一个向量中并显示它们(对于这个项目来说还没有必要),但它仍然没有画出鼠标输入的内容。这让我怀疑有更深层次的东西导致这种情况。有一个简单的解决方法吗?

我为原始的,未完成的代码道歉(未完成的原因是它不能完成我需要的所有工作,但它确实可以编译并运行它的GUI对应物。)

import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;

public class DrawingArea extends JPanel {

int x1, x2, y1, y2;
int shapeType;
char mouseAction;
Vector<String> eventList = new Vector<String>();

public DrawingArea() {
    initialize();
}

public void initialize() {


    addMouseListener(new MouseAdapter() {

      public void mouseEntered(MouseEvent m) {
          x1 = m.getX();
          y1 = m.getY();
          mouseAction = 'a';
          shapeType = 0;
          repaint();

      }

     public void mouseExited (MouseEvent m) {
         x1 = m.getX();
         y1 = m.getY();
         mouseAction = 'b';
         shapeType = 0;
         repaint();
     }

      public void mousePressed(MouseEvent m) {

          x1 = m.getX();
          y1 = m.getY();
          mouseAction = 'd';
          shapeType = 0;
          repaint();
      }
      public void mouseReleased(MouseEvent m) {

          x2 = m.getX();
          y2 = m.getY();
          repaint();
      }
    });

  addMouseMotionListener(new MouseMotionAdapter() {

      public void mouseMoved (MouseEvent m) {

          x2 = m.getX();
          y2 = m.getY();
          mouseAction = 'c';
          shapeType = 0;
          repaint();
      }

       public void mouseDragged(MouseEvent m) {

             x2 = m.getX();
             y2 = m.getY();
             repaint();
          }
      });
}

public void output(String event, MouseEvent m) {

}

public void setShapeType(int num) {

    if (num == 1) {
        shapeType = 1;
        mouseAction = 'z';
        repaint();
    }
    else if (num == 2) {
        shapeType = 2;
        mouseAction = 'z';
        repaint();
    }
    else if (num == 3) {
        shapeType = 0;
        mouseAction = 'z';
        repaint();
    }
}

public void paint(Graphics g) {
    super.paint(g);

    if (shapeType == 1) {

        g.drawString("Rectangle", 25,25);

    }
    else if (shapeType == 2)
        g.drawString("Circle", 25, 25);

    if (mouseAction == 'a') {
        g.drawString("Mouse entered at (" + x1+ ", " + y1 + ")", 25, 25);
    }

    else if (mouseAction == 'b') {
        g.drawString("Mouse exited at (" + x1 + ", " + y1 + ")", 25, 25);

    }

    else if (mouseAction == 'c') {
        g.drawString("Mouse moved at (" + x2 + ", " + y2 + ")", 25, 25);

    }

    else if (mouseAction == 'd') 
        g.drawString("Mouse clicked at (" + x1 + ", " + y1 + ")", 25, 25);
}
}

供参考,这是GUI代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SimpleGUI extends JFrame {

DrawingArea drawArea = new DrawingArea();


public SimpleGUI() {
    createGUI();
}


public void createGUI() {

    JFrame main = new JFrame();
    main.setVisible(true);
    main.setSize(500, 600);
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel right = new JPanel(new GridLayout(20,1));
    JPanel bottom = new JPanel();

    JButton rect = new JButton("Rectangle");
    JButton circ = new JButton("Circle");
    JButton erase = new JButton("Erase");
    JButton send = new JButton("Send");

    JTextField text = new JTextField(30);

    right.add(rect);
    right.add(circ);
    right.add(erase);

    bottom.add(text);
    bottom.add(send);

    drawArea.setBackground(Color.WHITE);

    main.add(drawArea, BorderLayout.CENTER);
    main.add(right, BorderLayout.EAST);
    main.add(bottom, BorderLayout.SOUTH);

    rect.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            drawArea.setShapeType(1);
        }
    });

    circ.addActionListener (new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            drawArea.setShapeType(2);
        }

    });

    erase.addActionListener (new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            drawArea.setShapeType(3);
        }
    });

}




public static void main (String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new SimpleGUI();
        }
    });
}

}

1 个答案:

答案 0 :(得分:2)

问题不在于听众,而在于你的渲染逻辑......

if (mouseAction == 'a') {
    g.drawString("Mouse entered at (" + x1 + ", " + y1 + ")", 25, 25);
} else if (mouseAction == 'b') {
    g.drawString("Mouse exited at (" + x1 + ", " + y1 + ")", 25, 25);
} else if (mouseAction == 'c') {
    g.drawString("Mouse moved at (" + x2 + ", " + y2 + ")", 25, 25);
} else if (mouseAction == 'd') {
    g.drawString("Mouse clicked at (" + x1 + ", " + y1 + ")", 25, 25);
}

基本上,当鼠标进入时,它会触发mouseAction amouseAction c后立即触发a

我猜测重绘经理永远没有时间来渲染c,因为g.drawString("Mouse " + (mouseEntered ? "entered" : "exited") + " at (" + x1 + ", " + y1 + ")", 25, 50); if (mouseAction == 'c') { g.drawString("Mouse moved at (" + x2 + ", " + y2 + ")", 25, 25); } else if (mouseAction == 'd') { g.drawString("Mouse clicked at (" + x1 + ", " + y1 + ")", 25, 25); } 会覆盖它。

如果您更新了油漆代码以区分运动和动作之间的逻辑,那么您应该能够看到差异。

mouseEntered

我添加了boolean字段,这是一个简单的true值,设置为来自mouseEntered的{​​{1}}和来自false的{​​{1}} < / p>

此外,约定更喜欢覆盖mouseExited方法而不是paintComponentpaint是双缓冲的,paintComponent不是。