java – 单击鼠标时程序不执行函数

当左键单击鼠标时,应该交换彼此水平相邻的颜色,当右键单击时,垂直相邻的颜色应该交换.单击任一按钮时都没有发生任何事情.

有问题的代码:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

import sun.java2d.loops.DrawRect;

import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

public class Board extends JPanel implements MouseListener
{
//instance variables
private int width;
private int height;
private Block topLeft;
private Block topRight;
private Block botLeft;
private Block botRight;

public Board()  //constructor
{
    width = 200;
    height = 200;
    topLeft=new Block(0,0,width/2-10,height/2-10,Color.RED);
    topRight=new Block(width/2,0,width/2-10,height/2-10,Color.GREEN);
    botLeft=new Block(0,height/2,width/2-10,height/2-10,Color.BLUE);
    botRight=new Block(width/2,height/2,width/2-10,height/2-10,Color.YELLOW);
    setBackground(Color.WHITE);
    setVisible(true);
    //start trapping for mouse clicks
    addMouseListener(this);
}

//initialization constructor
 public Board(int w, int h)  //constructor
 {
    width = w;
    height = h;
    topLeft=new Block(0,0,width/2-10,height/2-10,Color.RED);
    topRight=new Block(width/2,0,width/2-10,height/2-10,Color.GREEN);
    botLeft=new Block(0,height/2,width/2-10,height/2-10,Color.BLUE);
    botRight=new Block(width/2,height/2,width/2-10,height/2-10,Color.YELLOW);
    setBackground(Color.WHITE);
    setVisible(true);
    //start trapping for mouse clicks
    addMouseListener(this);
}



public void update(Graphics window)
{
    paint(window);
}

public void paintComponent(Graphics window)
 {
super.paintComponent(window);
topRight.draw(window);
topLeft.draw(window);
botRight.draw(window);
botLeft.draw(window);


}

 public void swapTopRowColors()
{
Color temp = topLeft.getColor();
topLeft.setColor(topRight.getColor());
topRight.setColor(temp);
repaint();
}

public void swapBottomRowColors()
{
   Color temp = botLeft.getColor();
   botLeft.setColor(botRight.getColor());
   botRight.setColor(temp);
   repaint();
}

public void swapLeftColumnColors()
{
   Color temp = botLeft.getColor();
   botLeft.setColor(topLeft.getColor());
   topLeft.setColor(temp);
   repaint();
}

public void swapRightColumnColors()
{
   Color temp = botRight.getColor();
   botRight.setColor(topRight.getColor());
   topRight.setColor(temp);
   repaint();
}

public void mouseClicked(MouseEvent e)
{
    int mouseX=e.getX();
    int mouseY=e.getY();
    int mouseButton = e.getButton();

    if(mouseButton==MouseEvent.BUTTON1)     //left mouse button pressed
    {
        if(mouseX>=topLeft.getX() && mouseX<=topLeft.getWidth() && mouseY>=topLeft.getY() && mouseY<=topLeft.getY())
        {
            this.swapTopRowColors();
        }

        else if(mouseX>=topRight.getX() && mouseX<=topRight.getWidth() && mouseY>=topRight.getY() && mouseY<=topRight.getY())
        {
            this.swapTopRowColors();
        }

        else if(mouseX>=botLeft.getX() && mouseX<=botLeft.getWidth() && mouseY>=botLeft.getY() && mouseY<=botLeft.getY())
        {
            this.swapBottomRowColors();
        }

        else if(mouseX>=botRight.getX() && mouseX<=botRight.getWidth() && mouseY>=botRight.getY() && mouseY<=botRight.getY())
        {
            this.swapBottomRowColors();
        }

    }
    //right mouse button pressed
    if(mouseX>=topLeft.getX() && mouseX<=topLeft.getWidth() && mouseY>=topLeft.getY() && mouseY<=topLeft.getY())
    {
        this.swapLeftColumnColors();
    }

    else if(mouseX>=topRight.getX() && mouseX<=topRight.getWidth() && mouseY>=topRight.getY() && mouseY<=topRight.getY())
    {
        this.swapRightColumnColors();
    }

    else if(mouseX>=botLeft.getX() && mouseX<=botLeft.getWidth() && mouseY>=botLeft.getY() && mouseY<=botLeft.getY())
    {
        this.swapLeftColumnColors();
    }

    else if(mouseX>=botRight.getX() && mouseX<=botRight.getWidth() && mouseY>=botRight.getY() &&    mouseY<=botRight.getY())
    {
        this.swapRightColumnColors();
    }









}

public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }

//toString
}

以及启动它的代码:

import javax.swing.JFrame;

public class BlockGame extends JFrame
{
private static final int WIDTH = 800;
private static final int HEIGHT = 600;

public BlockGame()
{
    super("Board");
    setSize(WIDTH,HEIGHT);

    getContentPane().add(new Board(500,500));

    setVisible(true);
}

public static void main( String args[] )
{
    BlockGame run = new BlockGame();

}
}

解决方法:

你有两个问题……

mouseX >= topLeft.getX() && mouseX <= topLeft.getWidth()

这是检查鼠标位置是否大于或等于块x位置(这是好的)并且小于或等于它的宽度…. ???所以,如果我有一个100的宽度为10的盒子,我点击了105,那么这个检查就会失败.

105 >= 100 && 105 < 10 // .... ???

然后就是这个……

mouseY >= topLeft.getY() && mouseY <= topLeft.getY()

花点时间检查最后一个条件……您必须在块的顶部边缘单击“完全”才能使此条件成立.

我会做两件事之一.

要么我写一个方法来执行任何块的计算…

public boolean contains(Point p, Block block) {
    return p.x >= block.getX() && p.x <= block.getX() + block.getWidth() && 
            p.y >= block.getY() && p.y <= block.getY() + block.getHeight();
}

这样,如果代码中有错误,它只在一个地方……

或者(最好),我会从Rectangle扩展Block,这样我只需使用contains方法代替……

public class Block extends Rectangle {

    private Color color;

    public Block(int x, int y, int width, int height, Color color) {
        super(x, y, width, height);
        this.color = color;
    }

    public void draw(Graphics2D g) {
        g.setColor(color);
        g.fill(this);
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }
}

并在鼠标单击事件处理程序中…

if (topLeft.contains(e.getPoint()) { ... }
上一篇:Java MouseEvent位置不准确


下一篇:Java:将图像移向鼠标位置