java - 鼠标单击的侦听器不起作用?
问题描述
我正在尝试绘制一个代表国家的多个圆圈,当我单击这些圆圈中的任何一个时,这个国家的颜色必须从蓝色变为红色,现在当我运行程序时它没有显示错误但听众不起作用。我猜我贴的地方不对,谁能指导我!
public class Algo extends JPanel
{
public String[] Names = {"Egypt", "London", "Korea", "Egypt", "London", "Egypt", "London", "Korea", "Egypt", "London"};
public int[] X = {105, 324, 190, 346, 162, 270, 196, 277, 57, 225};
public int[] Y = {110, 477, 212, 444, 207, 331, 230, 497, 470, 297};
public List<Country> Countries = new ArrayList<>();
private Color color = Color.blue;
public static void main(String[] args)
{
new Algo();
}
public Algo()
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex)
{
ex.printStackTrace();
}
JFrame frame = new JFrame("Airport");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new CountriesPane());
frame.pack();
frame.setSize(700, 700);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class CountriesPane extends JPanel implements MouseListener
{
public CountriesPane()
{
for (int i = 0; i < Names.length; i++)
{
Country C = new Country(X[i], Y[i]);
C.Name = Names[i];
Countries.add(C);
}
}
@Override
public void mousePressed(MouseEvent me) {
}
@Override
public void mouseReleased(MouseEvent me) {
}
@Override
public void mouseEntered(MouseEvent me) {
}
@Override
public void mouseExited(MouseEvent me) {
}
@Override
public void mouseClicked(MouseEvent me)
{
Point lol = me.getPoint();
for (int i = 0; i < Countries.size(); i++)
{
if (Countries.get(i).contian(lol))
Countries.get(i).changeColor();
}
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
for (int i = 0; i < Names.length; i++)
{
Countries.get(i).paint(g2d);
g2d.drawString(" " +Names[i],Countries.get(i).x, Countries.get(i).y);
}
}
}
public class Country
{
private int height = 20;
private int width = 20;
private int x;
private int y;
private Ellipse2D shape;
private String Name;
public Country( int w, int z)
{
x = w;
y = z;
shape = new Ellipse2D.Double(w, z, width, height);
}
public boolean contian(Point x)
{
return shape.contains(x);
}
public void paint(Graphics2D g2d)
{
g2d.setColor(color);
g2d.fill(shape);
}
public void changeColor()
{
if (color == Color.BLUE)
{
color = Color.RED;
}
else
{
color = color.BLUE;
}
repaint();
}
}
}
解决方案
最可能的原因是您的包含函数直接使用了 Ellipse2D 的包含函数。此函数检查给定的 x 和 y 是否在椭圆内,但不考虑它在屏幕上的位置偏移。试试下面的代码,看看它是否有所作为:
public boolean contian(Point x)
{
return shape.contains(new Point(x.getX()-this.x, x.getY()-this.y));
}
附带说明一下,存储颜色信息的方式应该不同。您使用单个全局值来存储在您的类Algo中声明的颜色。但是,您的代码建议您要将其存储在Country类中,因为您将方法changeColor()放在那里。请将颜色变量移至Country类。
推荐阅读
- swiftui - 使用 SwiftUI FileDocument 解决冲突?
- android - Room Table 的 @PrimaryKey 在 Android Paging 中产生意外行为
- xml - 使用 XSLT 代码处理 xml 中的重复属性及其值
- java - 将数据从活动传递到 recyclerAdapter - Android
- android-studio - 如何告诉 gradle 在 Android Studio 中从我的本地计算机下载文件
- syntax - (define | :) 在 Inno Setup Preprocessor 文档约定中是什么意思?
- android - ViewModel 如何捕获上层未抛出的异常?
- javascript - 使用 jquery ajax 和表单数据将视频上传到数据库
- javascript - Typescript,AJAX中的Jquery:未定义导出
- json - JSON反序列化,我需要将子部分作为单个字符串返回