Java實現滑鼠拖拽移動介面組件_java

來源:互聯網
上載者:User

  預設的,Frame或者JFrame自身已經實現了滑鼠拖拽標題列移動視窗的功能。

  只是,當你不滿意java的JFrame樣式,隱藏了標題列和邊框,又或者乾脆直接使用JWindow,那你又該怎麼實現滑鼠拖拽移動視窗的目的呢?最開始,我簡單的在mouseDragged方法裡frame.setLocation(e.getX(), e.getY()),結果,frame拖拽的時候不停地閃爍,位置在螢幕上不斷跳動。後來網上查資料,找到了答案。

 這裡給一個簡單的樣本,一看就明白:

 package com.jebysun.test.globalhotkey;  import java.awt.Color; import java.awt.Cursor; import java.awt.Point; import java.awt.event.MouseEvent;  import javax.swing.JLabel; import javax.swing.JWindow; import javax.swing.event.MouseInputListener;  /** * 自訂程式視窗,滑鼠可拖拽移動其位置。 * @author Jeby Sun * */ public class MyFrame extends JWindow {    private static final long serialVersionUID = 1L;      JLabel titleLbl;      public MyFrame() {     //設定背景顏色不能直接調用其setBackground方法,而要設定其ContentPane的背景顏色。     this.getContentPane().setBackground(new Color(0x99FF66));     this.setBounds(100,100,600,400);     this.setLayout(null);          titleLbl = new JLabel(" 自訂視窗標題列");     titleLbl.setOpaque(true);     titleLbl.setBackground(new Color(0x66CC00));     titleLbl.setBounds(0, 0, 600, 30);     this.add(titleLbl);     //滑鼠事件處理類     MouseEventListener mouseListener = new MouseEventListener(this);     titleLbl.addMouseListener(mouseListener);     titleLbl.addMouseMotionListener(mouseListener);          this.setVisible(true);   }    /**   * 滑鼠事件處理   * @author Jeby Sun   *   */   class MouseEventListener implements MouseInputListener {          Point origin;     //滑鼠拖拽想要移動的目標組件     MyFrame frame;          public MouseEventListener(MyFrame frame) {       this.frame = frame;       origin = new Point();     }          @Override     public void mouseClicked(MouseEvent e) {}      /**     * 記錄滑鼠按下時的點     */     @Override     public void mousePressed(MouseEvent e) {       origin.x = e.getX();        origin.y = e.getY();     }      @Override     public void mouseReleased(MouseEvent e) {}      /**     * 滑鼠移進標題列時,設定滑鼠圖示為移動表徵圖     */     @Override     public void mouseEntered(MouseEvent e) {       this.frame.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));     }          /**     * 滑鼠移出標題列時,設定滑鼠圖示為預設指標     */     @Override     public void mouseExited(MouseEvent e) {       this.frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));     }      /**     * 滑鼠在標題列拖拽時,設定視窗的座標位置     * 視窗新的座標位置 = 移動前座標位置+(滑鼠指標當前座標-滑鼠按下時指標的位置)     */     @Override     public void mouseDragged(MouseEvent e) {       Point p = this.frame.getLocation();       this.frame.setLocation(         p.x + (e.getX() - origin.x),          p.y + (e.getY() - origin.y));      }      @Override     public void mouseMoved(MouseEvent e) {}        }      public static void main(String[] args) {     new MyFrame();   }  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.