本文實(shí)例講述了Java實(shí)現(xiàn)的可選擇及拖拽圖片的面板功能。分享給大家供大家參考,具體如下:
今天在論壇上看到帖子希望能在 Swing 中實(shí)現(xiàn)像拖地圖一樣拖拽圖片。這里是一個(gè)最簡(jiǎn)單的實(shí)現(xiàn),提供了一個(gè)基本思路。
import javax.swing.*;import javax.swing.filechooser.FileNameExtensionFilter;import java.awt.*;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.io.File;/** * 在窗體上拖拽圖片。使用方法:雙擊窗體空白處將會(huì)彈出打開(kāi)圖片對(duì)話框。打開(kāi)圖片后可以在窗體上拖拽圖片。 */@SuppressWarnings("serial")public class DragingFrame extends JFrame { /** * 構(gòu)造函數(shù) * * @throws HeadlessException ??? */ public DragingFrame() throws HeadlessException { this.setDefaultCloseOperation(EXIT_ON_CLOSE); getContentPane().setLayout(new BorderLayout()); getContentPane().add(new ImagePanel(), BorderLayout.CENTER); } // 程序入口 public static void main(String[] args) throws Exception { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); DragingFrame frame = new DragingFrame(); frame.setSize(400, 300); frame.setLocation(300, 300); frame.setResizable(false); frame.setTitle("www.survivalescaperooms.com 雙擊打開(kāi)圖片,然后拖拽"); frame.setVisible(true); }}/** * 能夠拖拽圖片的面板 */@SuppressWarnings("serial")class ImagePanel extends JPanel { private DragStatus status = DragStatus.Ready; // 拖拽狀態(tài) private Image image; // 要顯示的圖片 private Point imagePosition = new Point(0, 0), // 圖片的當(dāng)前位置 imageStartposition = new Point(0, 0), // 每次拖拽開(kāi)始時(shí)圖片的位置(也就是上次拖拽后的位置) mouseStartposition; // 每次拖拽開(kāi)始時(shí)鼠標(biāo)的位置 ImagePanel() { addMouseListener(new MouseListener() { // 雙擊鼠標(biāo)時(shí)打開(kāi)圖片 public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { openImage(); } } // 按下鼠標(biāo)時(shí),更改狀態(tài),并且記錄拖拽起始位置。 public void mousePressed(MouseEvent e) { if (status == DragStatus.Ready) { status = DragStatus.Dragging; mouseStartposition = e.getPoint(); imageStartposition.setLocation(imagePosition.getLocation()); } } // 松開(kāi)鼠標(biāo)時(shí)更改狀態(tài) public void mouseReleased(MouseEvent e) { if (status == DragStatus.Dragging) { status = DragStatus.Ready; } } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } }); addMouseMotionListener(new MouseMotionListener() { // Java 有拖拽事件,在這個(gè)事件中移動(dòng)圖片位置 public void mouseDragged(MouseEvent e) { if (status == DragStatus.Dragging) { moveImage(e.getPoint()); } } public void mouseMoved(MouseEvent e) { } }); } /** * 移動(dòng)圖片。實(shí)際上畫圖工作在 paintComponent() 中進(jìn)行,這里只是計(jì)算圖片位置,然后調(diào)用該方法。 * * @param point 當(dāng)前的鼠標(biāo)位置 */ private void moveImage(Point point) { // 圖片的當(dāng)前位置等于圖片的起始位置加上鼠標(biāo)位置的偏移量。 imagePosition.setLocation( imageStartposition.getX() + (point.getX() - mouseStartposition.getX()), imageStartposition.getY() + (point.getY() - mouseStartposition.getY()) ); repaint(); } // 打開(kāi)圖片 private void openImage() { System.out.println("Opening image..."); File file = createFileChooser().getSelectedFile(); if (file != null) { image = Toolkit.getDefaultToolkit().getImage(file.getAbsolutePath()); if (image != null) { this.repaint(); } } } // 創(chuàng)建打開(kāi)文件對(duì)話框 private JFileChooser createFileChooser() { JFileChooser chooser = new JFileChooser(); chooser.setDialogTitle("請(qǐng)選擇圖片文件..."); chooser.addChoosableFileFilter(new FileNameExtensionFilter("常用圖片格式", "jpg", "jpeg", "gif", "png")); chooser.showOpenDialog(this); return chooser; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (image != null) { g.drawImage(image, (int) imagePosition.getX(), (int) imagePosition.getY(), this); } } private enum DragStatus { Ready, Dragging }}
運(yùn)行效果:
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選