好的,我正在从一个班级创建一个程序,我们称之为“John”类。因此,我不是从头开始,而是通过复制“Dave”类中的大多数代码创建“John”类(此类具有类似的设置但功能上有很大差异),然后根据我的需要进行大大修改。
问题是,当我点击“运行文件”按钮时,程序就像是“Dave”一样。这太荒谬了,我已经改变了很多代码,“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.");
}
}
}
答案 0 :(得分:2)
在John的主要方法中
new PaintDemo(); // i.e. John's and Dave's main method initiate the same code