首页 > 解决方案 > 鼠标单击的侦听器不起作用?

问题描述

我正在尝试绘制一个代表国家的多个圆圈,当我单击这些圆圈中的任何一个时,这个国家的颜色必须从蓝色变为红色,现在当我运行程序时它没有显示错误但听众不起作用。我猜我贴的地方不对,谁能指导我!

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();

    }  


  }

}   

标签: javamouseclick-event

解决方案


最可能的原因是您的包含函数直接使用了 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类。


推荐阅读