我已经可以在 MousePressed 和 MouseReleased 面板中创建一个球,并使用 MotionListener 更新坐标,并在鼠标悬停在球上时更改球的颜色。 这在 myPanel 类中工作正常,因为面板定义了尺寸并且鼠标在其中工作。 但是我现在必须做的,但我不确定如何使 Ball 类扩展 Component 并实现 MouseListener。因此,我必须在 Ball 类中使用 MouseEntered 来更改球的颜色。 帮忙?
//Ball
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Ball extends JComponent implements MouseListener{
public int x,y,r;
public Color c = Color.BLUE;
private int distance = 0;
public Ball(int X, int Y, int R){
super();
x=X;
y=Y;
r=R;
addMouseListener(this);
}
public void draw(Graphics g){
g.setColor(c);
g.fillOval(x-r, y-r, 2*r, 2*r);
}
public void mousePressed(MouseEvent me){}
public void mouseReleased(MouseEvent me){ }
public void mouseClicked(MouseEvent me){}
public void mouseEntered(MouseEvent me){
c = Color.ORANGE;
}
public void mouseExited(MouseEvent me){}
}
//myPanel
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
public class myPanel extends JPanel implements MouseListener{
private Color c = new Color(150,200,100);
public Ball ball = new Ball(100,100,50);
private Point mouseCoords = new Point();
public myPanel(){
super();
setLayout(new FlowLayout());
addMouseListener(this);
add(ball);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
ball.draw(g);
}
public void mousePressed(MouseEvent me){
ball.x = me.getX();
ball.y = me.getY();
labelPanel.setX(me.getX()); //Report x and y values
labelPanel.setY(me.getY());
// ball.c = Color.RED; //change color on click
repaint();
}
public void mouseReleased(MouseEvent me){}
public void mouseMoved(MouseEvent me) {}
public void mouseClicked(MouseEvent me){}
public void mouseEntered(MouseEvent me){}
public void mouseExited(MouseEvent me){}
}
// myFrame
import java.awt.*;
import javax.swing.*;
public class myFrame extends JFrame{
public myPanel left = new myPanel();
public labelPanel right = new labelPanel();
public myFrame(){
super("This is my Frame");
setLayout(new BorderLayout());
setSize(900,700);
add(left,BorderLayout.CENTER);
add(right,BorderLayout.EAST);
setVisible(true);
}
public static void main(String[] args){
myFrame mF = new myFrame();
mF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//labelPanel
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
public class labelPanel extends JPanel{
public static JLabel xCoord = new JLabel("X=",JLabel.RIGHT);
public static JLabel yCoord = new JLabel("Y=",JLabel.RIGHT);
public Color c = new Color(100,200,10);
public labelPanel() {
super();
setBackground(c);
setLayout(new GridLayout(2,1));
add(xCoord);
add(yCoord);
}
public static void setX(int x){
xCoord.setText("X=" + x);
}
public static void setY(int y){
yCoord.setText("Y=" + y);
}
}
因此,如果您运行代码,它会像前面提到的那样工作,但我不知道如何将 Ball 类定义为 Component/JComponent 以便它实现 MouseEntered
最佳答案
在构造函数中使用 addMouseListener(this)
:
public Ball(int X, int Y, int R){
super();
x=X;
y=Y;
r=R;
addMouseListener(this);
}
并将其从您的 draw()
方法中移除。
此外,如果您可能想考虑重写 paint(Graphics g)
。这将允许 Swing 确定何时绘制。您始终可以通过调用 repaint();
或调用 paint()
手动选择何时绘制。 repaint()
绘制此组件和所有子组件,并清除组件。 paint()
只绘制此组件,而不是子组件,除非包含在 paint
方法中,否则不会清除屏幕。
如果这不能解决问题,请告诉我。
关于Java Swing : Changing color of a ball on mouseEntered,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9404005/