- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
基本上,我必须简单地绘制文本“鼠标输入于”及其坐标。当未实现 mouseMoved 时,我的代码成功执行此操作。当它是时,它永远不会显示鼠标进入并直接进入“鼠标移动于”。我可以理解 mouseMoved 如何做到这一点,因为进入面板也会将鼠标移动到该位置。我尝试将移动存储在 vector 中并显示它们(对于该项目来说还不是必需的),但它仍然没有绘制鼠标输入的内容。这让我怀疑有更深层次的原因造成了这种情况。有简单的解决方法吗?
我对原始的、未完成的代码表示歉意(未完成是因为它还没有完成我需要它完成的所有事情,但它确实可以使用它的 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();
}
});
}
}
最佳答案
问题不在于监听器,而在于您的渲染逻辑......
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);
}
基本上,当鼠标进入时,会触发a
的mouseAction
,紧接着是c的
.mouseAction
我推测重绘管理器永远没有时间渲染 a
,因为 c
会覆盖它。
如果您更新了绘制代码以分离运动和 Action 之间的逻辑,您应该能够看到差异。
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
值,从 mouseEntered
设置为 true
并且来自mouseExited
的false
此外,约定更喜欢重写 paintComponent
方法而不是 paint
。 paintComponent
是双缓冲的,paint
不是。
关于java - 使用 Java Swing,mouseEntered 的光芒被 mouseMoved 所掩盖。有一个简单的解决办法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13661881/
我有一个很奇怪的问题,网页中的某些数字被随机标记为“*”。asp.net 4 webform 和 IIS Server 7 出现问题。我不知道为什么会这样。 例如:我们在文本框中的文字:45-3791
我是一名优秀的程序员,十分优秀!