最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Java圖形化編程中的鼠標事件設計

 更新時間:2015年10月08日 17:32:44   投稿:goldensun  
這篇文章主要介紹了Java圖形化編程中的鼠標事件設計,是Java的GUI開發(fā)中的基礎部分,需要的朋友可以參考下

鼠標事件的事件源往往與容器相關,當鼠標進入容器、離開容器,或者在容器中單擊鼠標、拖動鼠標時都會發(fā)生鼠標事件。java語言為處理鼠標事件提供兩個接口:MouseListener,MouseMotionListener接口。
MouseListener接口

MouseListener接口能處理5種鼠標事件:按下鼠標,釋放鼠標,點擊鼠標、鼠標進入、鼠標退出。相應的方法有:
(1) getX():鼠標的X坐標
(2) getY():鼠標的Y坐標
(3) getModifiers():獲取鼠標的左鍵或右鍵。
(4) getClickCount():鼠標被點擊的次數(shù)。
(5) getSource():獲取發(fā)生鼠標的事件源。
(6) addMouseListener(監(jiān)視器):加放監(jiān)視器。
(7) removeMouseListener(監(jiān)視器):移去監(jiān)視器。

要實現(xiàn)的MouseListener接口的方法有:
(1) mousePressed(MouseEvent e);
(2) mouseReleased(MouseEvent e);
(3) mouseEntered(MouseEvent e);
(4) mouseExited(MouseEvent e);
(5) mouseClicked(MouseEvent e);

【例】小應用程序設置了一個文本區(qū),用于記錄一系列鼠標事件。當鼠標進入小應用程序窗口時,文本區(qū)顯示“鼠標進來”;當鼠標離開 窗口時,文本區(qū)顯示“鼠標走開”;當鼠標被按下時,文本區(qū)顯示“鼠標按下”,當鼠標被雙擊時,文本區(qū)顯示“鼠標雙擊”;并顯示鼠標的坐標。程序還顯示一個紅色的圓,當點擊鼠標時,圓的半徑會不斷地變大。

import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyPanel extends JPanel{
  public void print(int r){
    Graphics g = getGraphics();
    g.clearRect(0,0,this.getWidth(),this.getHeight());
    g.setColor(Color.red);
    g.fillOval(10,10,r,r);
  }
}
class MyWindow extends JFrame implements MouseListener{
  JTextArea text;
  MyPanel panel;
  int x,y,r =10;
  int mouseFlg=0;
  static String mouseStates[]={"鼠標鍵按下","鼠標松開","鼠標進來","鼠標走開","鼠標雙擊"};
  MyWindow(String s){
    super(s);
    Container con = this.getContentPane();
    con.setLayout(new GridLayout(2,1));
    this.setSize(200,300);
    this.setLocation(100,100);
    panel = new MyPanel();
    con.add(panel);
    text = new JTextArea(10,20);
    text.setBackground(Color.blue);
    con.add(text);
    addMouseListener(this);
    this.setVisible(true);
    this.pack();
  }
  public void paint(Graphics g){
    r = r+4;
    if(r>80){
      r=10;
    }
    text.append(mouseStates[mouseFlg]+"了,位置是:" +x+","+y+"\n");
    panel.print(r);
  }
  public void mousePressed(MouseEvent e){
    x = e.getX();
    y = e.getY();
    mouseFlg = 0;
    repaint();
  }
  public void mouseRelease(MouseEvent e){
    x = e.getX();
    y = e.getY();
    mouseFlg = 1;
    repaint();
  }
  public void mouseEntered(MouseEvent e){
    x = e.getX();
    y = e.getY();
    mouseFlg = 2;
    repaint();
  }
  public void mouseExited(MouseEvent e){
    x = e.getX();
    y = e.getY();
    mouseFlg = 3;
    repaint();
  }
  public void mouseClicked(MouseEvent e){
    if(e.getClickCount()==2){
      x = e.getX();
      y = e.getY();
      mouseFlg = 4;
      repaint();
    }
    else{}
  }
}
public class Example6_8 extends Applet{
  public void init(){
    MyWindow myWnd = new MyWindow("鼠標事件示意程序");
  }
}

任何組件上都可以發(fā)生鼠標事件:鼠標進入、鼠標退出、按下鼠標等。例如,在上述程序中添加一個按鈕,并給按鈕對象添加鼠標監(jiān)視器,將上述程序中的init()方法修改成如下形式,即能示意按鈕上的所有鼠標事件。

JButton button;
public void init(){
  button = new JButton(“按鈕也能發(fā)生鼠標事件”);
  r = 10;
  text = new JTextArea(15,20);
  add(button);
  add(text);
  button.addMouseListener(this);
}

如果程序希望進一步知道按下或點擊的是鼠標左鍵或右鍵,鼠標的左鍵或右鍵可用InputEvent類中的常量BUTTON1_MASK和BUTTON3_MASK來判定。例如,以下表達式判斷是否按下或點擊了鼠標右鍵:

  e.getModifiers()==InputEvent. BUTTON3_MASK


MouseMotionListener接口

MouseMotionListener接口處理拖動鼠標和鼠標移動兩種事件。

注冊監(jiān)視器的方法是:
    addMouseMotionListener(監(jiān)視器)
要實現(xiàn)的的接口方法有兩個:
(1) mouseDragged(MouseEvent e)
(2) mouseMoved(MouseEvent e)

【例】一個滾動條與顯示窗口同步變化的應用程序。窗口有一個方塊,用鼠標拖運方塊,或用鼠標點擊窗口,方塊改變顯示位置,相應水平和垂直滾動條的滑塊也會改變它們在滾動條中的位置。反之,移動滾動條的滑塊,方塊在窗口中的顯示位置也會改變。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyWindow extends JFrame{
  public MyWindow(String s){
    super(s);
    Container con = this.getContentPane();
    con.setLayout(new BorderLayout());
    this.setLocation(100,100);
    JScrollBar xAxis = new JScrollBar(JScrollBar.HORIZONTAL,50,1,0,100);
    jScrollBar yAxis = new jScrollBar(JScrollBar.VERTICAL,50,1,0,100);
    MyListener listener = new MyListener(xAxis,yAxis,238,118);
    Jpanel scrolledCanvas = new JPanel();
    scrolledCanvas.setLayout(new BorderLayout());
    scrolledCanvas.add(listener,BorderLayout.CENTER);
    scrolledCanvas.add(xAix,BorderLayout.SOUTH);
    scrolledCanvas.add(yAix,BorderLayout.EAST);
    con.add(scrolledCanvas,BorderLayout.NORTH);
    this.setVisible(true);
    this.pack();
  }
  public Dimension getPreferredSize(){
    return new Dimension(500,300);
  }
}
class MyListener extends JComponent implements MouseListener, MouseMotionListener,AdjustmentListener{
  private int x,y;
  private JScrollBar xScrollBar;
  private JScrollBar yScrollBar;
  private void updateScrollBars(int x,int y){
    int d;
    d = (int)(((float)x/(float)getSize().width)*100.0);
    xScrollBar.setValue(d);
    d = (int)(((float)y/(float)getSize().height)*100.0);
    yScrollBar.setValue(d);
  }
  public MyListener(JScrollBar xaxis,JScrollBar yaxis,int x0,int y0){
    xScrollBar =xaxis;
    yScrollBar =yaxis;
    x = x0;
    y=y0;
    xScrollBar.addAdjustmentListener(this);
    yScrollBar.addAdjustmentListener(this);
    this.addMouseListener(this);
    this.addMouseMotionListener(this);
  }
  public void paint(Graphics g){
    g.setColor(getBackground());
    Dimension size = getSize();
    g.fillRect(0,0,size.width,size.height);
    g.setColor(Color.blue);
    g.fillRect(x,y,50,50);
  }
  public void mouseEntered(MouseEvent e){}
  public void mouseExited(MouseEvent e){}
  public void mouseClicked(MouseEvent e){}
  public void mouseRelease(MouseEvent e){}
  public void mouseMoved(MouseEvent e){}
  public void mousePressed(MouseEvent e){
    x = e.getX();
    y = e.getY();
    updateScrollBars(x,y);
    repaint();
  }
  public void mouseDragged(MouseEvent e){
    x = e.getX();
    y = e.getY();
    updateScrollBars(x,y);
    repaint();
  }
  public void adjustmentValueChanged(AdjustmentEvent e){
    if(e.getSource()==xScrollBar)
      x=(int)((float)(xScrollBar.getValue()/100.0)*getSize().width);
    else if(e.getSource()==yScrollBar)
      y = (int)((float)(yScrollBar.getValue()/100.0)*getSize().height);
    repaint();
  }
}
public class Example6_9{
  public static void main(){
    MyWindow myWindow = new MyWindow("滾動條示意程序");
  }
}

上述例子中,如果只要求通過滑動滑塊,改變內(nèi)容的顯示位置,可以簡單地使用滾動面板JScrollPane。如果是這樣,關于滾動條的創(chuàng)建和控制都可以免去,直接由JScrollPane內(nèi)部實現(xiàn)。參見以下修改后的MyWindow的定義:

class MyWindow extends JFrame{
  public MyWindow(String s){
    super(s);
    Container con = this.getContentPane();
    con.setLayout(new BorderLayout());
    this.setLocaltion(100,100);
    MyListener listener = new MyListener();
    listener.setPreferredSize(new Dimension(700,700));
    JScrollPane scrolledCanvas = new JScrollPane(listener);
    this.add(scrolledCanvas,BorderLayout.CENTER);
    this.setVisible(true);
    this.pack();
  }
  public Dimension getPreferredSize(){
    return new Dimension(400,400);
  }
}

鼠標指針形狀也能由程序控制 ,setCursor()方法能設置鼠標指針形狀。例如,代碼setCursor(Cursor.getPredefinedCursor(cursor.WAIT_CURSOR))。

相關文章

  • java eclipse 出現(xiàn) xxx cannot be resolved to a type 錯誤解決方法

    java eclipse 出現(xiàn) xxx cannot be resolved to a type 錯誤解決方法

    這篇文章主要介紹了java eclipse 出現(xiàn) xxx cannot be resolved to a type 錯誤解決方法的相關資料,需要的朋友可以參考下
    2017-03-03
  • Idea 解決 Could not autowire. No beans of ''xxxx'' type found 的錯誤提示

    Idea 解決 Could not autowire. No beans of ''xxxx'' type found

    這篇文章主要介紹了Idea 解決 Could not autowire. No beans of 'xxxx' type found 的錯誤提示,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • 解決BeanUtils.copyProperties之大坑

    解決BeanUtils.copyProperties之大坑

    這篇文章主要介紹了解決BeanUtils.copyProperties之大坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 使用JPA自定義id策略避免主鍵自增

    使用JPA自定義id策略避免主鍵自增

    這篇文章主要介紹了使用JPA自定義id策略避免主鍵自增問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Spring Security在標準登錄表單中添加一個額外的字段

    Spring Security在標準登錄表單中添加一個額外的字段

    這篇文章主要介紹了Spring Security在標準登錄表單中添加一個額外的字段,我們將重點關注兩種不同的方法,以展示框架的多功能性以及我們可以使用它的靈活方式。 需要的朋友可以參考下
    2019-05-05
  • 使用ShardingJDBC進行數(shù)據(jù)分片以及讀寫分離

    使用ShardingJDBC進行數(shù)據(jù)分片以及讀寫分離

    ShardingJDBC是一個輕量級的Java框架,提供了數(shù)據(jù)分片、讀寫分離、分布式主鍵生成等數(shù)據(jù)訪問功能,本文將給大家介紹如何使用ShardingJDBC進行數(shù)據(jù)分片以及讀寫分離,需要的朋友可以參考下
    2024-01-01
  • java中Servlet監(jiān)聽器的工作原理及示例詳解

    java中Servlet監(jiān)聽器的工作原理及示例詳解

    這篇文章主要介紹了java中Servlet監(jiān)聽器的工作原理及示例詳解。Servlet監(jiān)聽器用于監(jiān)聽一些重要事件的發(fā)生,監(jiān)聽器對象可以在事情發(fā)生前、發(fā)生后可以做一些必要的處理。感興趣的可以來了解一下
    2020-07-07
  • Springboot項目實現(xiàn)Mysql多數(shù)據(jù)源切換的完整實例

    Springboot項目實現(xiàn)Mysql多數(shù)據(jù)源切換的完整實例

    這篇文章主要給大家介紹了關于Springboot項目實現(xiàn)Mysql多數(shù)據(jù)源切換的完整實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • Java安全后端返回文件流方式

    Java安全后端返回文件流方式

    這篇文章主要介紹了Java安全后端返回文件流方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • SpringBoot注解@ConditionalOnClass底層源碼實現(xiàn)

    SpringBoot注解@ConditionalOnClass底層源碼實現(xiàn)

    這篇文章主要為大家介紹了SpringBoot注解@ConditionalOnClass底層源碼實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02

最新評論

米林县| 驻马店市| 揭阳市| 大余县| 六安市| 大关县| 宝坻区| 阳城县| 肃北| 如东县| 霍林郭勒市| 天水市| 客服| 曲麻莱县| 环江| 鄂伦春自治旗| 桂林市| 宁德市| 芷江| 华亭县| 句容市| 于田县| 台前县| 马边| 卫辉市| 鹿泉市| 靖安县| 双江| 井冈山市| 北安市| 广元市| 定州市| 灵璧县| 陆河县| 桐乡市| 岗巴县| 平和县| 秦安县| 大连市| 永州市| 永登县|