简体   繁体   English

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

[英]Mouse Clicked Listener does not work?

I am trying to draw a multiple circles that present countries, and when I click any of these circles the color of this country has to change from blue to red, now when I run the program it shows no error but the listeners do not work. 我试图绘制多个表示国家的圆圈,当我单击这些圆圈中的任何一个时,该国家的颜色必须从蓝色更改为红色,现在当我运行该程序时,它没有显示错误,但侦听器不起作用。 I guess the place where I attached them is wrong, can anyone guide me! 我猜我附着它们的地方是错误的,谁能指导我!

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

    }  


  }

}   

The most probable cause is that your contain function directly uses the Ellipse2D's contains function. 最可能的原因是您的包含函数直接使用Ellipse2D的包含函数。 This function checks if the given x and y are inside of the Ellipse, but doesn't factor in it's position offset on screen. 此函数检查给定的x和y是否在Ellipse的内部,但不考虑其在屏幕上的位置偏移。 Try the following code to see if it makes a difference: 尝试下面的代码,看看是否有所不同:

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

As a side note, the way you store the color information should be done differently. 附带说明一下,存储颜色信息的方式应有所不同。 You use a single global value to store the color, declared in your class Algo . 您使用单个全局值存储在类Algo中声明的颜色。 But, your code suggests you want to store it inside the Country class, because you put the method changeColor() there. 但是,您的代码建议您将其存储在Country类中,因为您将方法changeColor()放在了那里。 Please also move the color variable to the class Country . 还要将color变量移至Country类别。

I guess the place where I attached them is wrong 我猜我附着它们的地方是错误的

Where did you attach them? 您在哪里附上它们? I don't see an addMouseListener(...) statement in your code. 我在您的代码中看不到addMouseListener(...)语句。

You need to add the MouseListener to the panel in the constructor of your panel. 您需要在面板的构造函数中将MouseListener添加到面板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM