国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > Java > 正文

Java實(shí)現(xiàn)的可選擇及拖拽圖片的面板功能【基于swing組件】

2024-07-13 10:16:19
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(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)行效果:

Java,可選擇,拖拽,圖片,面板,swing組件

 

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JAVA教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 望都县| 陆川县| 阳信县| 乐安县| 仁化县| 探索| 延长县| 大宁县| 长岭县| 青岛市| 蒙阴县| 讷河市| 常熟市| 左云县| 山阴县| 屏东县| 新民市| 印江| 凤山县| 临清市| 兴海县| 黄冈市| 大田县| 沙田区| 龙岩市| 泰宁县| 三明市| 富阳市| 双牌县| 陇川县| 乌苏市| 崇礼县| 馆陶县| 昔阳县| 大英县| 孙吴县| 恩施市| 兴山县| 全州县| 沾益县| 旬阳县|