jLabel.addMouseListener(new java.awt.event.MouseAdapter() { 
public void mousePressed(java.awt.event.MouseEvent e) { 
if (e.getButton() == MouseEvent.BUTTON1) { 
offsetX = e.getX(); 
offsetY = e.getY(); 


}); jLabel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() { 
public void mouseDragged(java.awt.event.MouseEvent e) { 
jLabel.setLocation(e.getX()+(jLabel.getX()-offsetX), e.getY()+(jLabel.getY()-offsetY)); 
              } 
}); 这样是把LABEL整个移动了,我的目的是移动LABEL上的图片。因为,LABEL就那么大,但上面图片很大,我要像移动地图那样移动一点露出一点。 
我自己也想了,jLabel.setLocation(e.getX()+(jLabel.getX()-offsetX), e.getY()+(jLabel.getY()-offsetY)); 
这句应该不是jLabel.setLocation  而应该是图片.setLocation 但具体的我不会写,希望有人能够帮助我

解决方案 »

  1.   

    答:你应该将jLabel放在一个JScrollPane中,这样就可以实现图片的拖动了(像移动地图那样移动一点露出一点 
    ).这样的代码网上一大堆.
      

  2.   

    应该比较简单,在鼠标拖拽的事件中(当然,鼠标的拖拽是由几个事件合起来的)计算鼠标的移动方位,然后将 LABEL 重绘即可,也就是只画出图片的一部分。
      

  3.   

    看到这个标题不得不顶,现在工作多不容易啊。顺便提示下楼主,你应该转到gui版块去,那儿对swing或者swt熟悉的人多。
      

  4.   

    楼主我做了一个给你参考。看这里:
    http://blog.csdn.net/YidingHe/archive/2009/02/14/3890410.aspx
      

  5.   

    我想这玩意靠JLabel是实现不了的。
      

  6.   


    JLabel 放在JScrollPane中?你太搞笑了。
      

  7.   

    8 楼说的对。先获取Image,然后根据你鼠标的移动重画。双缓冲,呵呵。
      

  8.   

               swing??还没学过!!!头痛
      

  9.   

    自己找个大点的图片放里面试package test;import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;public class SwingDnDTest extends Applet {
    Image image;
    Point pressed = new Point(), lastTranslate = new Point(); public void init() {
    image = getImage(getCodeBase(), "334.jpg");
    try {
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(image, 0);
    mt.waitForID(0);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
    Point loc = e.getPoint(); // adjust mouse pressed location for
    // translation ...
    pressed.x = loc.x - lastTranslate.x;
    pressed.y = loc.y - lastTranslate.y;
    }
    });
    addMouseMotionListener(new MouseMotionAdapter() {
    public void mouseDragged(MouseEvent e) {
    Point loc = e.getPoint();
    Point translate = new Point(loc.x - pressed.x, loc.y
    - pressed.y);
    Graphics g = getGraphics(); try {
    g.clearRect(0, 0, getSize().width, getSize().height);
    g.translate(translate.x, translate.y);
    showStatus("Translating Graphics: " + translate); g.drawImage(image, 0, 0, SwingDnDTest.this);
    } finally {
    g.dispose();
    }
    lastTranslate = translate;
    }
    });
    } public void paint(Graphics g) {
    g.drawImage(image, 0, 0, this);
    }
    }
      

  10.   

    我也觉得和JLabel 没有关系的嘛  就是JScrollPane嘛 
      

  11.   

    解决了 程序正常运行了
    把所有代码弄上来给 有这类似问题的 朋友看看
    程序入口类:import java.awt.image.BufferedImage;
    import java.io.File;import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.JButton;public class mapin extends JFrame {
    private static final long serialVersionUID = 1L; private JPanel jPanel = null; // @jve:decl-index=0:visual-constraint="26,7" private JLabel jLabel = null; private JLabel jLabel1 = null; private JButton jButton = null; private JButton jButton1 = null; private JLabel jLabel2 = null; private JLabel jLabel3 = null; private BufferedImage source1; private BufferedImage source2; public static BufferedImage now1; public static BufferedImage now2; private File currentFile; private JButton jButton2 = null; int i = 0, j = 0; mapin() {
    super();
    init();
    } public void init() {
    this.setSize(600, 365);
    this.setContentPane(getJPanel());
    this.setTitle("后台");
    } /**
     * This method initializes jPanel
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getJPanel() {
    if (jPanel == null) {
    jLabel3 = new JLabel();
    jLabel3.setBounds(new java.awt.Rectangle(255, 77, 226, 247));
    jLabel3.setText("");
    jLabel2 = new JLabel();
    jLabel2.setBounds(new java.awt.Rectangle(9, 78, 230, 245));
    jLabel2.setText("");
    jLabel1 = new JLabel();
    jLabel1.setBounds(new java.awt.Rectangle(6, 15, 309, 21));
    jLabel1.setText("此为地图后台程序,请在此导入各类地图。");
    jLabel = new JLabel();
    jLabel.setText("使用说明:");
    jLabel.setBounds(new java.awt.Rectangle(5, 0, 309, 17));
    jPanel = new JPanel();
    jPanel.setLayout(null);
    jPanel.setSize(new java.awt.Dimension(571, 339));
    jPanel.add(jLabel, null);
    jPanel.add(jLabel1, null);
    jPanel.add(getJButton(), null);
    jPanel.add(getJButton1(), null);
    jPanel.add(jLabel2, null);
    jPanel.add(jLabel3, null);
    jPanel.add(getJButton2(), null);
    }
    return jPanel;
    } /**
     * This method initializes jButton
     * 
     * @return javax.swing.JButton
     */
    private JButton getJButton() {
    if (jButton == null) {
    jButton = new JButton();
    jButton.setBounds(new java.awt.Rectangle(61, 38, 116, 29));
    jButton.setText("二维地图");
    jButton.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent e) {
    System.out.println("输入二维完毕"); // TODO Auto-generated Event
    // stub mouseClicked()
    doOpenFile1(); i = 1;
    }
    });
    }
    return jButton;
    } /**
     * This method initializes jButton1
     * 
     * @return javax.swing.JButton
     */
    private JButton getJButton1() {
    if (jButton1 == null) {
    jButton1 = new JButton();
    jButton1.setBounds(new java.awt.Rectangle(316, 38, 119, 31));
    jButton1.setText("三维地图");
    jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent e) {
    System.out.println("输入三维完毕"); // TODO Auto-generated Event
    // stub mouseClicked()
    doOpenFile2(); j = 1;
    }
    });
    }
    return jButton1;
    } private void showImage1(File f) {
    try {
    source1 = ImageIO.read(f);
    jLabel2.setText("");
    jLabel2.setIcon(new ImageIcon(source1));
    } catch (Exception exe) {
    exe.printStackTrace();
    }
    } private void doOpenFile1() {
    JFileChooser jf = new JFileChooser(".");
    jf.addChoosableFileFilter(new MyExtension(".gif", "gif图像"));
    jf.addChoosableFileFilter(new MyExtension(".png", "png图像"));
    jf.addChoosableFileFilter(new MyExtension(".jpg", "JPG图像"));
    int i = jf.showDialog(this, "打开");
    if (i == JFileChooser.APPROVE_OPTION) {
    currentFile = jf.getSelectedFile();
    showImage1(currentFile); }
    } private void showImage2(File f) {
    try {
    source2 = ImageIO.read(f);
    jLabel3.setText("");
    jLabel3.setIcon(new ImageIcon(source2));
    } catch (Exception exe) {
    exe.printStackTrace();
    }
    } private void doOpenFile2() {
    JFileChooser jf = new JFileChooser(".");
    jf.addChoosableFileFilter(new MyExtension(".gif", "gif图像"));
    jf.addChoosableFileFilter(new MyExtension(".png", "png图像"));
    jf.addChoosableFileFilter(new MyExtension(".jpg", "JPG图像"));
    int i = jf.showDialog(this, "打开");
    if (i == JFileChooser.APPROVE_OPTION) {
    currentFile = jf.getSelectedFile();
    showImage2(currentFile); }
    } class MyExtension extends javax.swing.filechooser.FileFilter {
    private String ends, des; public MyExtension(String ends, String des) {
    this.ends = ends.toLowerCase();
    this.des = des;
    } public boolean accept(File f) {
    if (f.isDirectory()) {
    return true;
    } else {
    return f.getName().toLowerCase().endsWith(ends);
    }
    } public String getDescription() {
    return des;
    } } /**
     * This method initializes jButton2
     * 
     * @return javax.swing.JButton
     */
    private JButton getJButton2() {
    if (jButton2 == null) {
    jButton2 = new JButton();
    jButton2.setBounds(new java.awt.Rectangle(481, 39, 87, 31));
    jButton2.setText("进入前台");
    jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent e) {
    System.out.println("进入前台"); // TODO Auto-generated Event
    // stub mouseClicked()
    if (i == 1 && j == 1) {
    now1 = source1;
    now2 = source2;
    maptest1 mt;
    try {
    mt = new maptest1();
    mt.setVisible(true);
    mapin.this.dispose();
    } catch (Exception e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    } } else {
    JOptionPane.showMessageDialog(null, "请依次输入二维和三维地图!");
    } }
    });
    }
    return jButton2;
    } public static void main(String[] args) {
    mapin m = new mapin();
    m.setVisible(true);
    }
    }
      

  12.   

    第二个类import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.event.MouseEvent;
    import java.awt.geom.AffineTransform;
    import java.awt.image.AffineTransformOp;
    import java.awt.image.BufferedImage;
    import java.io.File;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JSlider;
    import javax.swing.UIManager;public class maptest1 extends JFrame { /**
     * 
     */
    private static final long serialVersionUID = 1L; private JPanel jPanel = null; // @jve:decl-index=0:visual-constraint="52,36" private JPanel jPanel1 = null; private JButton jButton = null; private JButton jButton1 = null; private JSlider jSlider = null; private static BufferedImage source = mapin.now1; BufferedImage sourcefinally; private static BufferedImage temp1; private static BufferedImage temp2; private int offsetX, offsetY; private static int currentXnow, currentYnow; private static int flag; private DragStatus status = DragStatus.Ready; // 拖拽状态 private Point imagePosition = new Point(0, 0), // 图片的当前位置 imageStartposition = new Point(0, 0), // 每次拖拽开始时图片的位置(也就是上次拖拽后的位置) mouseStartposition; // 每次拖拽开始时鼠标的位置 maptest1() throws Exception {
    super();
    init();
    } public void init() throws Exception {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    this.setSize(500, 335);
    this.setContentPane(getJPanel());
    this.setTitle("地图");
    this.repaint();
    temp1 = mapin.now1; flag = 1;
    } /**
     * This method initializes jPanel
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getJPanel() {
    if (jPanel == null) {
    jPanel = new JPanel();
    jPanel.setLayout(null);
    jPanel.setSize(new java.awt.Dimension(483, 298));
    jPanel.add(getJPanel1(), null);
    jPanel.add(getJButton(), null);
    jPanel.add(getJButton1(), null);
    jPanel.add(getJSlider(), null);
    }
    return jPanel;
    } /**
     * This method initializes jPanel1
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getJPanel1() {
    if (jPanel1 == null) {
    jPanel1 = new JPanel();
    jPanel1.setLayout(null);
    jPanel1.setBounds(new java.awt.Rectangle(64, 35, 412, 255));
    jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseReleased(java.awt.event.MouseEvent e) { System.out.println("mouseReleased()"); // TODO if (status == DragStatus.Dragging) {
    status = DragStatus.Ready;
    } } public void mousePressed(java.awt.event.MouseEvent e) {
    System.out.println("mousePressed()"); // TODO
    // Auto-generated
    // Event stub
    // mousePressed() if (status == DragStatus.Ready
    && e.getButton() == MouseEvent.BUTTON1) {// 点击左键才有效
    status = DragStatus.Dragging;
    mouseStartposition = e.getPoint();
    imageStartposition.setLocation(imagePosition
    .getLocation());
    offsetX = e.getX();
    offsetY = e.getY();
    System.out.println(offsetX + "   " + offsetY);
    } }
    });
    jPanel1
    .addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
    public void mouseDragged(java.awt.event.MouseEvent e) {
    System.out.println("mouseDragged()"); // TODO
    // Auto-generated
    // Event
    // stub
    // mouseDragged() if (status == DragStatus.Dragging) {
    moveImage(e.getPoint());
    } int currentX = (int) imageStartposition.getX()
    + (e.getX() - offsetX);
    int currentY = (int) imageStartposition.getY()
    + (e.getY() - offsetY);
    currentXnow = currentX;
    currentYnow = currentY;
    repaint(); // if (flag == 2) {
    //
    // jPanel1.getGraphics().drawImage(temp2, currentX,
    // currentY, temp2.getWidth(),
    // temp2.getHeight(), null);
    //
    // }
    // if (flag == 1) {
    //
    // jPanel1.getGraphics().drawImage(temp1, currentX,
    // currentY, temp1.getWidth(),
    // temp1.getHeight(), null);
    //
    // } }
    });
    }
    return jPanel1;
    }
      

  13.   

    /**
     * This method initializes jButton
     * 
     * @return javax.swing.JButton
     */
    private JButton getJButton() {
    if (jButton == null) {
    jButton = new JButton();
    jButton.setBounds(new java.awt.Rectangle(251, 4, 73, 28));
    jButton.setText("二维");
    jButton.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent e) {
    System.out.println("查看二维地图"); // TODO Auto-generated Event
    // stub mouseClicked()
    flag = 1;
    source = mapin.now1;
    int current = jSlider.getValue();
    double rate = 1 + current / 50.0 + 0.1;
    AffineTransform af = AffineTransform.getScaleInstance(rate,
    rate);
    AffineTransformOp aop = new AffineTransformOp(af, null);
    temp1 = aop.filter(mapin.now1, new BufferedImage(
    (int) (mapin.now1.getWidth() * rate),
    (int) (mapin.now1.getHeight() * rate),
    BufferedImage.TYPE_INT_ARGB)); jPanel1.getGraphics().drawImage(temp1, currentXnow,
    currentYnow, temp1.getWidth(), temp1.getHeight(),
    null);
    }
    });
    }
    return jButton;
    } /**
     * This method initializes jButton1
     * 
     * @return javax.swing.JButton
     */
    private JButton getJButton1() {
    if (jButton1 == null) {
    jButton1 = new JButton();
    jButton1.setBounds(new java.awt.Rectangle(336, 4, 78, 27));
    jButton1.setText("三维");
    jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent e) {
    System.out.println("查看三维地图"); // TODO Auto-generated Event
    // stub mouseClicked()
    flag = 2;
    source = mapin.now2;
    int current = jSlider.getValue();
    double rate = 1 + current / 50.0 + 0.1;
    AffineTransform af = AffineTransform.getScaleInstance(rate,
    rate);
    AffineTransformOp aop = new AffineTransformOp(af, null);
    temp2 = aop.filter(mapin.now2, new BufferedImage(
    (int) (mapin.now2.getWidth() * rate),
    (int) (mapin.now2.getHeight() * rate),
    BufferedImage.TYPE_INT_ARGB)); jPanel1.getGraphics().drawImage(temp2, currentXnow,
    currentYnow, temp2.getWidth(), temp2.getHeight(),
    null);
    }
    });
    }
    return jButton1;
    } /**
     * This method initializes jSlider
     * 
     * @return javax.swing.JSlider
     */
    private JSlider getJSlider() {
    if (jSlider == null) {
    jSlider = new JSlider();
    jSlider.setBounds(new java.awt.Rectangle(10, 37, 46, 241));
    jSlider.setMinimum(-50);
    jSlider.setMinorTickSpacing(10);
    jSlider.setMajorTickSpacing(50);
    jSlider.setOrientation(javax.swing.JSlider.VERTICAL);
    jSlider.setInverted(false);
    jSlider.setPaintTicks(true);
    jSlider.setPaintLabels(true);
    jSlider.setValue(0);
    jSlider.setMaximum(50);
    jSlider.addChangeListener(new javax.swing.event.ChangeListener() {
    public void stateChanged(javax.swing.event.ChangeEvent e) { int current = jSlider.getValue();
    double rate = 1 + current / 50.0 + 0.1;
    AffineTransform af = AffineTransform.getScaleInstance(rate,
    rate);
    AffineTransformOp aop = new AffineTransformOp(af, null);
    if (source == mapin.now1) {
    temp1 = aop.filter(mapin.now1, new BufferedImage(
    (int) (mapin.now1.getWidth() * rate),
    (int) (mapin.now1.getHeight() * rate),
    BufferedImage.TYPE_INT_ARGB));
    // jPanel1.getGraphics().drawImage(temp1, currentXnow,
    // currentYnow,
    // temp1.getWidth(), temp1.getHeight(), jPanel1);
    repaint();
    } else if (source == mapin.now2) {
    temp2 = aop.filter(mapin.now2, new BufferedImage(
    (int) (mapin.now2.getWidth() * rate),
    (int) (mapin.now2.getHeight() * rate),
    BufferedImage.TYPE_INT_ARGB)); // jPanel1.getGraphics().drawImage(temp2, currentXnow,
    // currentYnow,
    // temp2.getWidth(), temp2.getHeight(), jPanel1);
    repaint(); } }
    });
    }
    return jSlider;
    } class MyExtension extends javax.swing.filechooser.FileFilter {
    private String ends, des; public MyExtension(String ends, String des) {
    this.ends = ends.toLowerCase();
    this.des = des;
    } public boolean accept(File f) {
    if (f.isDirectory()) {
    return true;
    } else {
    return f.getName().toLowerCase().endsWith(ends);
    }
    } public String getDescription() {
    return des;
    } } private void moveImage(Point point) {
    // 图片的当前位置等于图片的起始位置加上鼠标位置的偏移量。
    imagePosition.setLocation(imageStartposition.getX()
    + (point.getX() - mouseStartposition.getX()),
    imageStartposition.getY()
    + (point.getY() - mouseStartposition.getY())); } @Override
    public void paint(Graphics g) {
    super.paint(g);
    if (flag == 1) {
    jPanel1.getGraphics().drawImage(temp1, currentXnow, currentYnow,
    temp1.getWidth(), temp1.getHeight(), null);
    }
    if (flag == 2) {
    jPanel1.getGraphics().drawImage(temp2, currentXnow, currentYnow,
    temp2.getWidth(), temp2.getHeight(), null);
    }
    } private enum DragStatus { Ready, Dragging
    }
    }
    直接运行入口类就可以了