2010-10-11 64 views
0

好的,我從一個類創建一個程序,我們稱之爲「John」類。因此,我從頭開始創建「John」類,通過複製「Dave」類中的大部分代碼(該類具有類似的設置,但功能差別很大),然後對其進行大量修改以適合我的需要。netbeans項目停止檢查新代碼

問題是,當我點擊'運行文件'按鈕時,程序的行爲就好像它是「戴夫」一樣。這很荒謬,我已經改變了很多代碼,現在沒有辦法讓「John」看起來像「Dave」。所以這一定是netbeans在做的。如何解決它?

編輯:

所以這裏的約翰:

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package javaapplication1; 

/** 
* 
* @author PCKhoi 
*/ 
import java.awt.*; 
import java.awt.event.*; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.imageio.ImageIO; 

public class ImageBlink { 
    static DrawingCanvas canvas; 
    private BufferedImage bi; 
    private int w,h; 
    public ImageBlink() { 
     Frame f = new Frame("Click!"); 
     f.addWindowListener(new WindowAdapter() { 
      public void windowClosing(WindowEvent e) { 
       System.exit(0); 
      } 
     }); 
     URL imageSrc = null; 
     try { 
      imageSrc = new URL("what_I_think.jpg"); 
     } catch (MalformedURLException ex) { 
      Logger.getLogger(ImageDemo.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     try { 
      bi = ImageIO.read(imageSrc); 
      w = bi.getWidth(null); 
      h = bi.getHeight(null); 
     } catch (IOException e) { 
      System.out.println("Image could not be read"); 
      System.exit(1); 
     } 
     canvas = new DrawingCanvas(); 
     f.add(canvas, BorderLayout.CENTER); 
     f.pack(); 
     f.setVisible(true); 
    } 
    public static void main(String[] args) { 
     new PaintDemo(); 
    } 
    class DrawingCanvas extends Canvas implements MouseListener, MouseMotionListener { 
     private int x1=0,y1=0,x2=0,y2=0; 
     public Dimension getPreferredSize() { 
      return new Dimension(600,600); 
     } 
     public DrawingCanvas() { 
      super(); 
      addMouseListener(this); 
      addMouseMotionListener(this); 
      setBackground(Color.white); 
     } 
     public void paint(Graphics g) { 
      Graphics2D g2D = (Graphics2D) g; 
      g2D.drawImage(bi,x2,y2,x2+w,y2+h,x1,y1,x1+w,y1+h,null); 
     } 
     public void mousePressed(MouseEvent e) { 
      throw new UnsupportedOperationException("Not supported yet."); 
     } 
     public void mouseReleased(MouseEvent e) { 
      throw new UnsupportedOperationException("Not supported yet."); 
     } 
     public void mouseClicked(MouseEvent e) { 
      x1 = x2; 
      y1 = y2; 
      x2 = (int)Math.random()*400; 
      y2 = (int)Math.random()*449; 
      canvas.repaint(); 
     } 
     public void mouseDragged(MouseEvent e) { 
      throw new UnsupportedOperationException("Not supported yet."); 
     } 

     public void mouseEntered(MouseEvent e) { 
      throw new UnsupportedOperationException("Not supported yet."); 
     } 

     public void mouseExited(MouseEvent e) { 
      throw new UnsupportedOperationException("Not supported yet."); 
     } 

     public void mouseMoved(MouseEvent e) { 
      throw new UnsupportedOperationException("Not supported yet."); 
     } 
    } 
} 

和這裏的戴夫:

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package javaapplication1; 

/** 
* 
* @author PCKhoi 
*/ 
import java.awt.*; 
import java.awt.event.*; 

public class PaintDemo { 
    static DrawingCanvas canvas; 
    private Stroke lineStroke; 
    public PaintDemo() { 
     Frame f = new Frame("Stroke a line!"); 
     f.addWindowListener(new WindowAdapter() { 
      public void windowClosing(WindowEvent e) { 
       System.exit(0); 
      } 
     }); 
     canvas = new DrawingCanvas(); 
     lineStroke = new BasicStroke(2.f); 
     f.add(canvas, BorderLayout.CENTER); 
     f.pack(); 
     f.setVisible(true); 
    } 
    public static void main(String[] args) { 
     PaintDemo pd = new PaintDemo(); 
    } 
    class DrawingCanvas extends Canvas implements MouseListener, MouseMotionListener { 
     private int x1=0,y1=0,x2=200,y2=200; 
     public Dimension getPreferredSize() { 
      return new Dimension(300,300); 
     } 
     public DrawingCanvas() { 
      super(); 
      addMouseListener(this); 
      addMouseMotionListener(this); 
      setBackground(Color.white); 
     } 
     public void paint(Graphics g) { 
      Graphics2D g2D = (Graphics2D) g; 
      g2D.setStroke(lineStroke); 
      g2D.drawLine(x1, y1, x2, y2); 
     } 
     public void mousePressed(MouseEvent e) { 
      x1 = e.getX(); 
      y1 = e.getY(); 
      x2 = x1; 
      y2 = y1; 
      System.out.println("x1: "+x1+"y1: "+y1); 
      canvas.repaint(); 
     } 
     public void mouseReleased(MouseEvent e) { 
      x2 = e.getX(); 
      y2 = e.getY(); 
      System.out.println("x2: "+x2+"y2: "+y2); 
      canvas.repaint(); 
     } 
     public void mouseClicked(MouseEvent e) { 
      canvas.repaint(); 
     } 
     public void mouseDragged(MouseEvent e) { 
      throw new UnsupportedOperationException("Not supported yet."); 
     } 

     public void mouseEntered(MouseEvent e) { 
      throw new UnsupportedOperationException("Not supported yet."); 
     } 

     public void mouseExited(MouseEvent e) { 
      throw new UnsupportedOperationException("Not supported yet."); 
     } 

     public void mouseMoved(MouseEvent e) { 
      throw new UnsupportedOperationException("Not supported yet."); 
     } 
    } 
} 
+1

你能張貼代碼 – 2010-10-11 09:13:41

+0

我不是很家庭iar與Netbeans,但我認爲你應該檢查'運行文件'按鈕是否實際運行文件(而不是,以前運行的文件或其他東西) – Jorn 2010-10-11 09:16:14

+0

當我右鍵單擊時,此「運行文件」按鈕位於菜單內文件。它和其他班級一起工作得很好。我創建了許多小程序,它們在一個大項目文件夾下運行。 – Khoi 2010-10-11 09:23:36

回答

2

在約翰的主要方法,你有

new PaintDemo(); // i.e. John's and Dave's main method initiate the same code 
+0

啊,現在正在工作。這麼小的錯誤。謝謝一堆! – Khoi 2010-10-11 09:25:48

+1

@Khoi:這就是爲什麼複製/粘貼代碼是一個壞主意。新代碼通常包含您粘貼的舊代碼中的遺留物。 – Qwerky 2010-10-11 09:53:20