0

我正在尝试创建一个带有用鼠标更改的图片的面板。我有 10 张图片(0.png 到 9.png)。我的问题是,我想移动图像,我目前正在查看第二张图片。我用JScrollPane滚动回第一张图像,但只有第一张图像在移动。如何刷新我的面板并能够移动所有图像?

这是我的代码:

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.*;

class MapFrame extends JPanel {
    /**
     * 
     */
    private static final long serialVersionUID = (long) 1.0;
    private static Image image;
    private static JLabel label = new JLabel();
    private static int ind =0;
    private static JFrame frame;
    private static String str;
   //----------------------------------------------------------------- 
    public MapFrame(){      }

   //----------------------------------------------------------------- 

    public MapFrame(String pathe) {
        super(new BorderLayout());
          image = new ImageIcon(getClass().getResource(pathe)).getImage();
          label = new JLabel(new ImageIcon(image));  
          JScrollPane scroll = new JScrollPane(label,JScrollPane.VERTICAL_SCROLLBAR_NEVER,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
          add(scroll);

          HandLcalss hand = new HandLcalss(label);
          JViewport v = scroll.getViewport();
          v.addMouseListener(hand);
          v.addMouseMotionListener(hand);
          v.addMouseWheelListener(hand);
    }

   //----------------------------------------------------------------- 


    public static void ShowGUI(String pathe) {
        frame = new JFrame("Bladi_map");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MapFrame(pathe));
        frame.setSize(500,400);  
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

   //----------------------------------------------------------------- 

    public static void ChangImage(String pathe){
        frame.getContentPane().add(new MapFrame(pathe));        
        label.revalidate();
        frame.setVisible(true);     
    }

    //----------------------------------------------------------------- 
class HandLcalss implements MouseListener,MouseMotionListener,MouseWheelListener{
    private static final int DELAY = 10;
    private  Cursor hc = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
    private  Timer scroller;
    private  JComponent label;
    private  Point startPt = new Point();
    private  Point move    = new Point();
    //----------------------------------------------------------------- 
    public HandLcalss(JComponent comp) {
        this.label = comp;
        comp.getCursor();
        this.scroller = new Timer(DELAY, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {               
            }
        });
    }
    //----------------------------------------------------------------- 

    @Override public void mouseDragged(MouseEvent e) {
        JViewport vport = (JViewport)e.getSource();
        Point pt = e.getPoint();
        int dx = startPt.x - pt.x;
        int dy = startPt.y - pt.y;
        Point vp = vport.getViewPosition();
        vp.translate(dx, dy);
        label.scrollRectToVisible(new Rectangle(vp, vport.getSize()));
        startPt.setLocation(pt);
    }
    @Override
    public void mousePressed(MouseEvent e) {
        ((JComponent)e.getSource()).setCursor(hc);
        startPt.setLocation(e.getPoint());
        move.setLocation(0, 0);
        scroller.stop();
    }
    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        int notches = e.getWheelRotation();

        if (notches < 0) {
            ind++;
            if(ind <0){ind=0;}
            if(ind<=9){
            str="/resources/"+ind+".png";       
             ChangImage(str);
            System.out.println("indink"+str);
            }
        } else {
            ind--;
            if(ind >9){ind=8;}
            if(ind>=0){
            str="/resources/"+ind+".png";
            ChangImage(str);
            System.out.println("ind"+ind);
            }             
        }   
      }
    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub          
    }

    @Override
    public void mouseMoved(MouseEvent arg0) {
        // TODO Auto-generated method stub
    }
    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub      
    }
    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub      
    }
    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub      
    }
}

//----------------------------------------------------------------- 
}

这是主要课程:

public class main { 
    public static void main(String[] args) {
        MapFrame.ShowGUI("/resources/0.png");       
    }
}
4

1 回答 1

2

您的代码中有两个问题:

首先:

public static void ChangImage(String pathe){
    frame.getContentPane().add(new MapFrame(pathe));        
    label.revalidate();
    frame.setVisible(true);     
} 

当您更改图像(在鼠标滚轮事件上调用)时,您只需将带有图像的面板(MapFrame)添加到现有容器:(Container pane = frame.getContentPane())。旧的仍然存在并与新面板重叠。上面的方法应该是:

public static void ChangImage(String pathe) {
    frame.getContentPane().removeAll();
    frame.getContentPane().add(new MapFrame(pathe));
    label.revalidate();
    frame.setVisible(true);
}

第二件事:

为什么复杂?(在 0 值上也有错误ind):

@Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        int notches = e.getWheelRotation() * -1;
        ind += notches;
        if(ind < 0) {
            ind = 0;
        }
        else if(ind > 9) {
            ind = 9;
        }
        str = "/resources/" + ind + ".png";
        ChangImage(str);
    }

最后一件事:

如果您正在实现所有鼠标接口 - 您可以扩展MouseAdapter. 现在你不需要重写你不想要的方法。

于 2012-07-27T16:26:15.853 回答