Coin163

首页 > Java源码-屏幕分辨率测试程序

Java源码-屏幕分辨率测试程序

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1074

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

Java How To Program第12章-GUI控件(上篇)中的一个示例,演示了鼠标事件的监控,包括:

1. 单击(click-按下某个鼠标键(左/中/右键)后松开)

2. 移动(move-不按下任何鼠标键的同时,移动鼠标)

3. 拖(drag-按下左/中/右键后不松开,并移动鼠标)

4. 放(release-拖操作之后,松开左/中/右键)。


程序会监控各种鼠标操作发生的位置,在移动操作中,将鼠标移动到屏幕最右下角,可看到X坐标和Y坐标同时达到最大值(1919,982)。

由于JFrame窗口最大化后并没有遮住Windows任务栏,任务栏的高度并没有加入到Y坐标,由此推出屏幕分辩率为1920*1080P,高清屏,嘿嘿。


代码如下:

//Fig. 12.28: MouseTrackerFrame.java
//Mouse event handling.
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MouseTrackerFrame extends JFrame
{
private final JPanel mousePanel; // panel in which mouse events occur
private final JLabel statusBar; // displays event information

// MouseTrackerFrame constructor sets up GUI and 
// registers mouse event handlers
public MouseTrackerFrame()
{
   super("Demonstrating Mouse Events");

   mousePanel = new JPanel(); 
   mousePanel.setBackground(Color.WHITE); 
   add(mousePanel, BorderLayout.CENTER); // add panel to JFrame

   statusBar = new JLabel("Mouse outside JPanel"); 
   add(statusBar, BorderLayout.SOUTH); // add label to JFrame

   // create and register listener for mouse and mouse motion events
   MouseHandler handler = new MouseHandler(); 
   mousePanel.addMouseListener(handler); 
   mousePanel.addMouseMotionListener(handler); 
} 

private class MouseHandler implements MouseListener, 
   MouseMotionListener 
{
   // MouseListener event handlers
   // handle event when mouse released immediately after press
   @Override
   public void mouseClicked(MouseEvent event)
   {
      statusBar.setText(String.format("Clicked at [%d, %d]", 
         event.getX(), event.getY()));
   } 

   // handle event when mouse pressed
   @Override
   public void mousePressed(MouseEvent event)
   {
      statusBar.setText(String.format("Pressed at [%d, %d]", 
         event.getX(), event.getY()));
   }

   // handle event when mouse released 
   @Override
   public void mouseReleased(MouseEvent event)
   {
      statusBar.setText(String.format("Released at [%d, %d]", 
         event.getX(), event.getY()));
   }

   // handle event when mouse enters area
   @Override
   public void mouseEntered(MouseEvent event)
   {
      statusBar.setText(String.format("Mouse entered at [%d, %d]", 
         event.getX(), event.getY()));
      mousePanel.setBackground(Color.GREEN);
   }

   // handle event when mouse exits area
   @Override
   public void mouseExited(MouseEvent event)
   {
      statusBar.setText("Mouse outside JPanel");
      mousePanel.setBackground(Color.WHITE);
   }

   // MouseMotionListener event handlers
   // handle event when user drags mouse with button pressed
   @Override
   public void mouseDragged(MouseEvent event)
   {
      statusBar.setText(String.format("Dragged at [%d, %d]", 
         event.getX(), event.getY()));
   } 

   // handle event when user moves mouse
   @Override
   public void mouseMoved(MouseEvent event)
   {
      statusBar.setText(String.format("Moved at [%d, %d]", 
         event.getX(), event.getY()));
   } 
} // end inner class MouseHandler

public static void main(String[] args)
{ 
   MouseTrackerFrame mouseTrackerFrame = new MouseTrackerFrame(); 
   mouseTrackerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   mouseTrackerFrame.setSize(300, 100); 
   mouseTrackerFrame.setVisible(true); 
} 

} // end class MouseTrackerFrame

原文

Java How To Program第12章-GUI控件(上篇)中的一个示例,演示了鼠标事件的监控,包括: 1. 单击(click-按下某个鼠标键(左/中/右键)后松开) 2. 移动(move-不按下任何鼠标键的同时,移动鼠

------分隔线----------------------------
相关推荐