鼠标单击侦听器不起作用?

时间:2018-05-21 21:58:37

标签: java mouseclick-event

我正在尝试绘制一个呈现国家/地区的多个圈子,当我点击这些圈子中的任何一个时,这个国家/地区的颜色必须从蓝色变为红色,现在当我运行程序时它显示没有错误但是听众没有不行。我猜我附上的地方是错的,任何人都可以指导我!

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

    }  


  }

}   

2 个答案:

答案 0 :(得分:0)

最可能的原因是您的包含函数直接使用Ellipse2D的包含函数。此函数检查给定的x和y是否在Ellipse内部,但不会影响它在屏幕上的位置偏移。请尝试以下代码,看看它是否有所作为:

public boolean contian(Point x)
{
    return shape.contains(new Point(x.getX()-this.x, x.getY()-this.y));
}

作为旁注,存储颜色信息的方式应该不同。您使用单个全局值来存储在类 Algo 中声明的颜色。但是,您的代码建议您将其存储在 Country 类中,因为您将方法 changeColor()放在那里。还请将颜色变量移动到类 Country

答案 1 :(得分:0)

  

我想我附加它们的地方是错误的

你在哪里附上它们?我的代码中没有看到addMouseListener(...)语句。

您需要将MouseListener添加到面板构造函数中的面板。