2014-04-29 8 views
-1

도시를 추가하고 서로 연결할 수 있어야하는 경로 찾기 프로그램을 만들고 있습니다. "도시 추가 중"부분이 정상적으로 작동하지만 추가 된 도시를 선택하려고 시도하거나 다른 곳을 클릭하면 오류가 발생합니다.지도에서 도시 추가를 선택하려고 할 때 "java.util.ClassCastException"수신

다음은 프로그램 코드입니다.

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

그리고 이것은 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; 
} 


} 

그리고 이것은 내가 얻는 오류입니다.

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) 

답변

1

addMouseListener (musLyss2);

이 줄은 pathLinder 인 부모 컨트롤에 musLyss2를 추가합니다. 그런 다음 methode mouseClicked를 호출하면 MouseEvent 소스가 Pathfinder가됩니다. 그런 다음 Stad에 대입하려고 시도하는 동안 ClassCastException이 발생합니다.

"stad.addMouseListener (musLyss2);"를 사용할 수 있습니다. 대신. 그러면 MouseEvent 소스가 Stad로 캐스팅 할 수 있습니다.