Java 鼠标按下不工作
我一直在尝试做这个任务,为了让它工作,我需要事件Java 鼠标按下不工作,java,user-interface,mouseevent,Java,User Interface,Mouseevent,我一直在尝试做这个任务,为了让它工作,我需要事件鼠标按下才能工作,但由于某种原因,它对鼠标没有反应。其目的是在按下鼠标时绘制另一个黄色圆圈 import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEven
鼠标按下
才能工作,但由于某种原因,它对鼠标没有反应。其目的是在按下鼠标时绘制另一个黄色圆圈
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class CatchMonster extends JPanel
{
private int height = 300;
private int width = 600;
private final int delay = 4001;
private ImageIcon image;
private Timer timer;
private int x, y, moveX, moveY, xPoint, yPoint;
public CatchMonster() {
DotListener dot = new DotListener();
addMouseListener(dot);
timer = new Timer(delay, new timerListener());
x = 40;
y = 40;
moveX = moveY = 3;
setPreferredSize(new Dimension(width, height));
setBackground(Color.black);
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponents(g);
g.setColor(Color.yellow);
g.fillOval(x, y, 60, 60);
}
private class timerListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
Random gn = new Random();
x = gn.nextInt(width);
y = gn.nextInt(height);
repaint();
}
}
private class DotListener implements MouseListener
{
public void mousePressed(MouseEvent event)
{
repaint();
}
@Override
public void mouseClicked(MouseEvent event) {
}
@Override
public void mouseEntered(MouseEvent event) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent event) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent event) {
// TODO Auto-generated method stub
}
}
}
我需要将事件鼠标按下才能工作,但由于某些原因,它对鼠标没有响应。其目的是在按下鼠标时绘制另一个黄色圆圈
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class CatchMonster extends JPanel
{
private int height = 300;
private int width = 600;
private final int delay = 4001;
private ImageIcon image;
private Timer timer;
private int x, y, moveX, moveY, xPoint, yPoint;
public CatchMonster() {
DotListener dot = new DotListener();
addMouseListener(dot);
timer = new Timer(delay, new timerListener());
x = 40;
y = 40;
moveX = moveY = 3;
setPreferredSize(new Dimension(width, height));
setBackground(Color.black);
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponents(g);
g.setColor(Color.yellow);
g.fillOval(x, y, 60, 60);
}
private class timerListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
Random gn = new Random();
x = gn.nextInt(width);
y = gn.nextInt(height);
repaint();
}
}
private class DotListener implements MouseListener
{
public void mousePressed(MouseEvent event)
{
repaint();
}
@Override
public void mouseClicked(MouseEvent event) {
}
@Override
public void mouseEntered(MouseEvent event) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent event) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent event) {
// TODO Auto-generated method stub
}
}
但它不会绘制另一个圆,因为它所做的只是调用repaint(),这将如何绘制新的内容?如果你想让它创建另一个圆,你必须给它一个这样做的逻辑。例如,如果要绘制多个黄色椭圆形,则需要创建点对象的ArrayList,并在mousePressed方法中将点添加到该数组列表中。然后在paintComponent方法中,可以迭代数组列表,为其包含的每个点绘制椭圆
此外,您还希望更改以下内容:
public void paintComponent(Graphics g) {
super.paintComponents(g); // this is not the "super" method of paintComponent
g.setColor(Color.yellow);
g.fillOval(x, y, 60, 60);
}
为此:
public void paintComponent(Graphics g) {
super.paintComponent(g); // See the difference?
g.setColor(Color.yellow);
g.fillOval(x, y, 60, 60);
}
例如:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class CatchMonster extends JPanel {
private int height = 300;
private int width = 600;
private final int delay = 4001;
private ImageIcon image;
private Timer timer;
private int x, y, moveX, moveY, xPoint, yPoint;
private List<Point> points = new ArrayList<Point>();
public CatchMonster() {
DotListener dot = new DotListener();
addMouseListener(dot);
timer = new Timer(delay, new timerListener());
x = 40;
y = 40;
moveX = moveY = 3;
setPreferredSize(new Dimension(width, height));
setBackground(Color.black);
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.yellow);
g.fillOval(x, y, 60, 60);
int radius = 30;
g.setColor(Color.green);
for (Point p : points) {
int x = p.x - radius;
int y = p.y - radius;
g.fillOval(x, y, 2 * radius, 2 * radius);
}
}
private class timerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Random gn = new Random();
x = gn.nextInt(width);
y = gn.nextInt(height);
repaint();
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Foo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new CatchMonster());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private class DotListener extends MouseAdapter {
public void mousePressed(MouseEvent event) {
points.add(event.getPoint());
repaint();
}
}
}
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.Point;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Random;
导入javax.swing.ImageIcon;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.Timer;
公共类CatchMonster扩展JPanel{
私人内部高度=300;
私有整数宽度=600;
专用最终整数延迟=4001;
私有图像图标图像;
私人定时器;
私有整数x,y,moveX,moveY,xPoint,yPoint;
私有列表点=新的ArrayList();
公共捕兽器(){
DotListener点=新的DotListener();
addMouseListener(dot);
定时器=新定时器(延迟,新定时器寄存器());
x=40;
y=40;
moveX=moveY=3;
setPreferredSize(新尺寸(宽度、高度));
挫折背景(颜色:黑色);
timer.start();
}
公共组件(图形g){
超级组件(g);
g、 setColor(颜色为黄色);
g、 椭圆形(x,y,60,60);
int半径=30;
g、 setColor(Color.green);
对于(点p:点){
int x=p.x-半径;
int y=p.y-半径;
g、 圆角(x,y,2*半径,2*半径);
}
}
私有类timerListener实现ActionListener{
已执行的公共无效操作(操作事件e){
随机gn=新随机();
x=gn.nextInt(宽度);
y=gn.nextInt(高度);
重新油漆();
}
}
公共静态void main(字符串[]args){
JFrame=新JFrame(“Foo”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(新的CatchMonster());
frame.pack();
frame.setLocationByPlatform(真);
frame.setVisible(true);
}
私有类DotListener扩展了MouseAdapter{
公共无效鼠标按下(鼠标事件){
添加(event.getPoint());
重新油漆();
}
}
}
同意,但我看不出您的代码示例有任何差异。。。也许我太累了…@Sebastien:这是可怕的paintComponentS与没有“s”的paintComponent之争谢谢大家它真的很有帮助如果我不想要很多圆圈,但只要一个改变它的位置就行了吗?@user807398:如果你调整你的代码以适应这种情况,最好自己去发现。