2014-04-29 3 views
-1

Je crée un programme pathfinding où vous devriez pouvoir ajouter des villes et les relier entre elles. La partie "Ajouter une ville" fonctionne correctement, mais lorsque j'essaie de sélectionner la ville ajoutée (ou cliquez n'importe où sur la carte), une erreur se produit.Recevoir "java.util.ClassCastException" lorsque j'essaie de sélectionner une ville ajoutée sur une carte

Voici le code du programme.

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

Et c'est la classe City.

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; 
} 


} 

Et c'est l'erreur que j'obtiens.

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) 

Répondre

1

addMouseListener (musLyss2);

Cette ligne ajoute musLyss2 au contrôle parent Pathfinder. Ensuite, lorsque la méthode mouseClicked est appelée, la source de MouseEvent sera Pathfinder. Ensuite, pendant que vous essayez de le caser à Stad, il y aura une exception ClassCastException

Vous pouvez utiliser "stad.addMouseListener (musLyss2);" au lieu. Ensuite, la source de MouseEvent sera stad vous pouvez le lancer sur Stad

Questions connexes