更改鼠标事件的背景颜色

发布于 2024-12-12 14:23:26 字数 827 浏览 0 评论 0原文

我想在鼠标单击矩形外部时更改背景颜色。我只是不知道如何使用 MouseEvent。

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class AnAppletWithMouseEvents extends Applet implements MouseListener {
public void init()
{
    addMouseListener(this);
}
public void paint(Graphics g)
{

    g.setColor(Color.green);
    g.drawRect(10, 30, 150, 150);
}
public void mouseClicked(MouseEvent e)
{
    String clickDesc;
    if (e.getClickCount() == 2)
        clickDesc = "double";
    else
        clickDesc = "single";

    System.out.println("Mouse was " + clickDesc + "-clicked at location (" +
        e.getX() + ", " + e.getY() + ")");

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

I want to change the background color when the mouse is clicked on the outside of the rectangle. I just dont know how to use the MouseEvent.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class AnAppletWithMouseEvents extends Applet implements MouseListener {
public void init()
{
    addMouseListener(this);
}
public void paint(Graphics g)
{

    g.setColor(Color.green);
    g.drawRect(10, 30, 150, 150);
}
public void mouseClicked(MouseEvent e)
{
    String clickDesc;
    if (e.getClickCount() == 2)
        clickDesc = "double";
    else
        clickDesc = "single";

    System.out.println("Mouse was " + clickDesc + "-clicked at location (" +
        e.getX() + ", " + e.getY() + ")");

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜妞爱困 2024-12-19 14:23:28

mouseClicked 方法中,您应该测试 e.getX()e.getY() 是否位于矩形之外,然后调用 < code>setBackground() :

this.setBackground(Color.red);

矩形边框将保持绿色(这是你想要的吗?)

HTH

In the mouseClicked method, you should test that e.getX() and e.getY() are outside the rectangle, and then invoke setBackground() :

this.setBackground(Color.red);

The rectangle border will stay green (is that what you want?)

HTH

夜夜流光相皎洁 2024-12-19 14:23:27
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class AnAppletWithMouseEvents extends Applet implements MouseListener {
Color color = Color.green;
public void init()
{
    addMouseListener(this);
}
public void paint(Graphics g)
{

    g.setColor(this.color);
    g.drawRect(10, 30, 150, 150);
}
public void mouseClicked(MouseEvent e)
{
    this.color = color.red;
    this.repaint();

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

现在您需要计算点击是否在矩形边界之外。

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class AnAppletWithMouseEvents extends Applet implements MouseListener {
Color color = Color.green;
public void init()
{
    addMouseListener(this);
}
public void paint(Graphics g)
{

    g.setColor(this.color);
    g.drawRect(10, 30, 150, 150);
}
public void mouseClicked(MouseEvent e)
{
    this.color = color.red;
    this.repaint();

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

Now you need to count if the click was outside the bounds of rectangle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文