怎样修改现成的代码,使其在Netbeans中可以运行?
小弟是java初学者,想请教众位大侠一个问题,谢谢!
在网上找到如下的java源代码,目的是实现拖拽图片的效果,不知道怎么让它运行,请指点迷津!
是应该准备什么条件呢?或者修改哪里?劳烦详细说明!非常感谢!
源代码:
/*
* DragPictureDemo.java
*
* Created on 2008年11月14日, 下午6:46
*/
package myproject;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
*
* @author 60008
*/
public class DragPictureDemo extends javax.swing.JFrame {
JLabel jlPic; //图片,用于拖动
/** Creates new form DragPictureDemo */
public DragPictureDemo() {
super("图片的拖动效果"); //调用父类构造函数
Icon image=new ImageIcon(this.getClass().getResource("E:/FYP/img/1.jpg")); //实例化图标
jlPic = new JLabel(image); //实例化带图片的标签
getContentPane().add(jlPic); //增加标签到容器上
DragPicListener listener=new DragPicListener(); //鼠标事件处理
jlPic.addMouseListener(listener); //增加标签的事件处理
jlPic.addMouseMotionListener(listener);
setSize(300,200); //设置窗口尺寸
setVisible(true); //设置窗口为可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
}
class DragPicListener implements MouseInputListener{ //鼠标事件处理
Point p=new Point(0,0); //坐标点
public void mouseMoved(MouseEvent e){}
public void mouseDragged(MouseEvent e){
Point newP=SwingUtilities.convertPoint(jlPic,e.getPoint(),jlPic.getParent()); //转换坐标系统
jlPic.setLocation(jlPic.getX()+(newP.x-p.x),jlPic.getY()+(newP.y-p.y)); //设置标签的新位置
p=newP; //更改坐标点
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e){
p=SwingUtilities.convertPoint(jlPic,e.getPoint(),jlPic.getParent()); //得到当前坐标点
initComponents();
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new DragPictureDemo();
}
/*public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DragPictureDemo().setVisible(true);
}
});
}
*/
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
}