Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 鼠标单击的侦听器不工作?_Java_Mouseclick Event - Fatal编程技术网

Java 鼠标单击的侦听器不工作?

Java 鼠标单击的侦听器不工作?,java,mouseclick-event,Java,Mouseclick Event,我试着画一个多个圆圈来表示当前的国家,当我点击其中任何一个圆圈时,这个国家的颜色必须从蓝色变为红色,现在当我运行程序时,它显示没有错误,但听众不工作。我猜我贴的地方不对,谁能给我指点一下 public class Algo extends JPanel { public String[] Names = {"Egypt", "London", "Korea", "Egypt", "London", "Egypt", "London", "Korea", "Egypt", "London"

我试着画一个多个圆圈来表示当前的国家,当我点击其中任何一个圆圈时,这个国家的颜色必须从蓝色变为红色,现在当我运行程序时,它显示没有错误,但听众不工作。我猜我贴的地方不对,谁能给我指点一下

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

    }  


  }

}   
公共类算法扩展了JPanel
{
公共字符串[]名称={“埃及”、“伦敦”、“韩国”、“埃及”、“伦敦”、“埃及”、“伦敦”、“韩国”、“埃及”、“伦敦”};
公共int[]X={105,324,190,346,162,270,196,277,57,225};
公共int[]Y={110477121444207331239497470297};
public List Countries=new ArrayList();
私有颜色=Color.blue;
公共静态void main(字符串[]args)
{
新算法();
}
公共算法()
{ 
invokeLater(新的Runnable()
{
@凌驾
公开募捐
{
尝试
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} 
catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex)
{
例如printStackTrace();
}
JFrame=新JFrame(“机场”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(新CountriesPane());
frame.pack();
框架设置尺寸(700700);
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类CountriesPane扩展JPanel实现MouseListener
{
公共国家西班牙人()
{
for(int i=0;i
最可能的原因是contain函数直接使用了Ellipse2D的contains函数。此函数检查给定的x和y是否在椭圆内,但不考虑其在屏幕上的位置偏移。请尝试以下代码,看看它是否有区别:

public boolean contian(Point x)
{
    return shape.contains(new Point(x.getX()-this.x, x.getY()-this.y));
}
作为旁注,存储颜色信息的方式应该有所不同。您使用一个全局值来存储在类Algo中声明的颜色。但是,您的代码建议您将其存储在Country类中,因为您将方法changeColor()放在那里。请同时将颜色变量移到类国家/地区

我想我把它们贴在哪里是错的

你把它们放在哪里了?我在您的代码中没有看到
addMouseListener(…)
语句

您需要在面板的构造函数中将鼠标侦听器添加到面板中