Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.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
Swing “接收”;java.util.ClassCastException“;当我';I’我想在地图上选择一个城市_Swing_Awt_Classcastexception_Mouselistener_Jcomponent - Fatal编程技术网

Swing “接收”;java.util.ClassCastException“;当我';I’我想在地图上选择一个城市

Swing “接收”;java.util.ClassCastException“;当我';I’我想在地图上选择一个城市,swing,awt,classcastexception,mouselistener,jcomponent,Swing,Awt,Classcastexception,Mouselistener,Jcomponent,我正在创建一个寻路程序,您可以在其中添加城市,然后将它们相互连接。 “添加城市”部分工作正常,但当我尝试选择添加的城市(或单击地图上的任何其他位置)时,它放弃了一个错误 这是程序代码 import java.util.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.File; public class Pathfinder extends JFrame { JButton

我正在创建一个寻路程序,您可以在其中添加城市,然后将它们相互连接。 “添加城市”部分工作正常,但当我尝试选择添加的城市(或单击地图上的任何其他位置)时,它放弃了一个错误

这是程序代码

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;

public class Pathfinder extends JFrame {

JButton hittaVäg, visaFörbindelse, nyPlats, nyFörbindelse, ändraFörbindelse;
JMenuBar menyBar;
JMenuItem ny, avsluta, hittaVägMeny, visaFörbindelseMeny, nyPlatsMeny, nyFörbindelseMeny, ändraFörbindelseMeny;
String str = System.getProperty("user.dir");
JFileChooser jfc;
BildPanel Bild = null;
MouseListener musLyss = new MouseListener();
MouseListener2 musLyss2 = new MouseListener2();
Stad från = null;
Stad till = null;
ListGraph listGraph = new ListGraph();

Pathfinder(){

    super("PathFinder");
    setLayout(new BorderLayout());
    setSize(590, 400);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);

    jfc = new JFileChooser(".");

    JPanel norra = new JPanel();
    add(norra, BorderLayout.NORTH);


    JButton hittaVäg = new JButton("Hitta väg");
    JButton visaFörbindelse = new JButton("Visa förbindelse");
    JButton nyPlats = new JButton("Ny plats");
    JButton nyFörbindelse = new JButton("Ny förbindelse");
    JButton ändraFörbindelse = new JButton("Ändra förbindelse");

    norra.add(hittaVäg);
    norra.add(visaFörbindelse);
    norra.add(nyPlats);
    norra.add(nyFörbindelse);
    norra.add(ändraFörbindelse);

    hittaVäg.addActionListener(new HittaLyss());
    visaFörbindelse.addActionListener(new VisaLyss());
    nyPlats.addActionListener(new NyPlatsLyss());
    nyFörbindelse.addActionListener(new NyFörbindelseLyss());
    ändraFörbindelse.addActionListener(new NyFörbindelseLyss());


    JMenuBar menyBar = new JMenuBar();
    setJMenuBar(menyBar);

    JMenu arkivMeny = new JMenu("Arkiv");
    JMenu operationerMeny = new JMenu("Operationer");

    menyBar.add(arkivMeny);
    menyBar.add(operationerMeny);


    JMenuItem ny = new JMenuItem("Ny");
    JMenuItem avsluta = new JMenuItem("Avsluta");

    arkivMeny.add(ny);
    arkivMeny.add(avsluta);

    ny.addActionListener(new NyLyss());
    avsluta.addActionListener(new AvslutaLyss());


    JMenuItem hittaVägMeny = new JMenuItem("Hitta väg");        
    JMenuItem visaFörbindelseMeny = new JMenuItem("Visa förbindelse");      
    JMenuItem nyPlatsMeny = new JMenuItem("Ny plats");
    JMenuItem nyFörbindelseMeny = new JMenuItem("Ny förbindelse");
    JMenuItem ändraFörbindelseMeny = new JMenuItem("Ändra förbindelse");

    operationerMeny.add(hittaVägMeny);
    operationerMeny.add(visaFörbindelseMeny);
    operationerMeny.add(nyPlatsMeny);
    operationerMeny.add(nyFörbindelseMeny);
    operationerMeny.add(ändraFörbindelseMeny);

    hittaVäg.addActionListener(new HittaLyss());
    visaFörbindelse.addActionListener(new VisaLyss());
    nyPlats.addActionListener(new NyPlatsLyss());
    nyFörbindelse.addActionListener(new NyFörbindelseLyss());
    ändraFörbindelse.addActionListener(new ÄndraFörbindelseLyss());

    pack();
    setVisible(true);


}
class MouseListener extends MouseAdapter{
    public void mouseClicked(MouseEvent mev){
        boolean o;
        o=true;
        for(;;){                
            String svar = JOptionPane.showInputDialog(null, "Platsens namn: ", "Ny Plats", JOptionPane.OK_CANCEL_OPTION);
            if (svar == null){
                o=false;
                break;  
            }else{
                if (svar.isEmpty()){
                JOptionPane.showMessageDialog(null, "Var vänlig ange ett giltigt namn!", "Error", JOptionPane.ERROR_MESSAGE);
                o=false;
            }


                if(o){
                int x = mev.getX();
                int y = mev.getY();
                Stad stad = new Stad (svar, x-10, y-10);
                listGraph.addNode(stad);
                Bild.add(stad);
                Bild.repaint();
                addMouseListener(musLyss2);
                validate();
                repaint();
                break;
            }
        }
    }
        Bild.removeMouseListener(musLyss);
        Bild.setCursor(Cursor.getDefaultCursor());
}
}
class MouseListener2 extends MouseAdapter{
    public void mouseClicked(MouseEvent mev){
        Stad s = (Stad)mev.getSource();

        if(s.getVald()==false){
            if (från==null){
                från = s;
                s.setVald(true);
        }
            else if(till==null){
                till = s;
                s.setVald(true);
            }
        }else{
            if (från==s){
                från = null;
                s.setVald(false);

            }else if (till==s){
                till = null;
                s.setVald(false);
        }

    }

}
}

class HittaLyss implements ActionListener{
    public void actionPerformed(ActionEvent ave){ 

    }

}
class VisaLyss implements ActionListener{
    public void actionPerformed(ActionEvent ave){ 

    }

}
class NyPlatsLyss implements ActionListener{
    public void actionPerformed(ActionEvent ave){ 

            Bild.addMouseListener(musLyss);
            Bild.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));

    }

}
class NyFörbindelseLyss implements ActionListener{
    public void actionPerformed(ActionEvent ave){ 

    }

}
class ÄndraFörbindelseLyss implements ActionListener{
    public void actionPerformed(ActionEvent ave){ 

    }

}
class NyLyss implements ActionListener{
    public void actionPerformed(ActionEvent ave){ 
        int svar = jfc.showOpenDialog(Pathfinder.this);
        if (svar == JFileChooser.APPROVE_OPTION){
            File f = jfc.getSelectedFile();
            String filnamn = f.getAbsolutePath();
            if (Bild != null)
                remove(Bild);
            Bild = new BildPanel(filnamn);
            add(Bild, BorderLayout.CENTER);
            validate();
            repaint();
            pack();
        }
    }

}
class AvslutaLyss implements ActionListener{
    public void actionPerformed(ActionEvent ave){ 
        System.exit(0);
    }

}
public static void main (String[] args){
    new Pathfinder();
}
}
这是城市班

import javax.swing.*;
import java.awt.*;

public class Stad extends JComponent{
private String namn;
private boolean vald;
private int x, y;

public Stad(String namn, int x, int y){
    setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    this.namn = namn;
    this.x=x;
    this.y=y;
    setBounds(x, y, 50, 50);


}

public void paintComponent(Graphics g){
    if (vald != true)
        g.setColor(Color.BLUE);
    else
        g.setColor(Color.RED);
    g.fillOval(0, 0, 15, 15);
    g.setColor(Color.BLACK);
    g.drawString(namn, 0, 30);
}

public int getX(){
    return x;
}

public int getY(){
    return y;
}

public void setVald(boolean vald){
    this.vald = vald;
    repaint();
}

public boolean getVald(){
    return vald;
}

public String getNamn(){
    return namn;
}


}
这就是我得到的错误

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: Pathfinder cannot be cast to Stad
at Pathfinder$MouseListener2.mouseClicked(Pathfinder.java:133)
at java.awt.Component.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Window.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
addMouseListener(musLyss2)

此行将musLyss2添加到作为Pathfinder的父控件。然后,当调用methode mouseClicked时,MouseEvent的源将是Pathfinder。然后,当您试图将其装箱给Stad时,将出现ClassCastException

您可以使用“stad.addMouseListener(musLyss2);”代替。 然后MouseEvent的源将是stad,您可以将其强制转换为stad