写完屏幕取色器后,又想到以前用过的屏幕尺,大体想了一下流程发现很简单,就又写了一下。在windows下边没什么问题,在linux中不能拖动到屏幕以外,这可能是系统的差异了,暂时没有找到解决方法.截图如下: import java.awt.Color; import java.awt.Graphics; import java.awt.MouseInfo; import java.awt.Toolkit; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; /** * * Copyright: Copyright 2000 - 2009 Tech. Co. Ltd. All Rights Reserved. * Date: Sep 4, 2009 * Author: Sheng Xingya * Version: V1.0 * Description: 屏幕尺,用来测量屏幕上组件的长宽所占像素 */ public class Rule extends JFrame { /** * */ private static final long serialVersionUID = 7664910731949042762L; private int x = 0, y = 0; //标记目前是否处于水平状态 private boolean isHorizontal = true; public void init() { this.setSize(1024, 100); this.setTitle("屏幕尺"); //设置其始终显示在最前 this.setAlwaysOnTop(true); //设置其不显示标题栏 this.setUndecorated(true); //添加鼠标拖动窗口事件监听器 this.addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { //此处的setLocation()方法在linux中其参数只能为正数,也就是不能将窗口拖动到屏幕外 setLocation(MouseInfo.getPointerInfo().getLocation().x - x, MouseInfo.getPointerInfo().getLocation().y - y); } }); //添加鼠标滚轮拖动事件监听器 this.addMouseWheelListener(new MouseWheelListener() { public void mouseWheelMoved(MouseWheelEvent e) { //获取滚动方向 int move = e.getWheelRotation(); //获取屏幕大小 Toolkit toolkit = Toolkit.getDefaultToolkit(); int x = (int) toolkit.getScreenSize().getWidth(); int y = (int) toolkit.getScreenSize().getHeight(); if (isHorizontal) { //如果是向下滚动,且没有超过屏幕的宽度,则每次加10个像素 if (move == 1) { if (getSize().getWidth() < x) { setSize((int) getSize().getWidth() + 10, (int) getSize().getHeight()); } } else { //如果是向上滚动,且宽度大于100,则每次减10个像素 if (getSize().getWidth() > 100) { setSize((int) getSize().getWidth() - 10, (int) getSize().getHeight()); } } } else { if (move == 1) { if (getSize().getHeight() < y) { setSize((int) getSize().getWidth(), (int) getSize().getHeight() + 10); } } else { if (getSize().getHeight() > 100) { setSize((int) getSize().getWidth(), (int) getSize().getHeight() - 10); } } } repaint(); } }); //添加一个鼠标按下的事件监听器,用来记录拖动点与窗口位置的距离 this.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { x = MouseInfo.getPointerInfo().getLocation().x - getLocation().x; y = MouseInfo.getPointerInfo().getLocation().y - getLocation().y; } //添加一个鼠标释放的事件监听器,当用户点击右键时退出程序 public void mouseReleased(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON3) { System.exit(0); } } //添加一个鼠标点击的事件监听器,当用户双击时,切换水平和垂直模式 public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { if (isHorizontal) { setSize(100, 600); isHorizontal = false; } else { isHorizontal = true; setSize(1024, 100); } //重画 repaint(); } } }); this.setLayout(null); JPanel bPanel = new JPanel(); this.add(bPanel); this.setVisible(true); } public void paint(Graphics g) { super.paint(g); int width = (int) this.getSize().getWidth(); int height = (int) getSize().getHeight(); g.setColor(Color.BLACK); int max = width; // int max = ((width - 20) / 100) * 100; if (isHorizontal) { g.clearRect(0, 0, width, height); g.drawString("双击切换垂直、水平模式,右击关闭", 200, 50); g.fillRect(10, 0, max, 2); g.fillRect(10, height - 2, max, 2); for (int i = 0; i < max; i += 10) { if (i % 100 == 0) { g.fillRect(i + 10, 0, 1, 10); g.fillRect(i + 10, height - 12, 1, 10); if (i == 0) { g.drawString(String.valueOf(i), i + 8, 20); g.drawString(String.valueOf(i), i + 8, height - 13); } else { g.drawString(String.valueOf(i), i, 20); g.drawString(String.valueOf(i), i, height - 13); } } else if (i % 50 == 0) { g.fillRect(i + 10, 0, 1, 8); g.fillRect(i + 10, height - 10, 1, 8); g.drawString(String.valueOf(i), i, 20); g.drawString(String.valueOf(i), i, height - 13); } else { g.fillRect(i + 10, 0, 1, 5); g.fillRect(i + 10, height - 7, 1, 5); } } } else { g.clearRect(0, 0, width, height); // g.drawString("双击切换垂直、水平模式,右击关闭", 50, 200); g.fillRect(0, 10, 2, height); g.fillRect(width - 2, 10, 2, height); for (int i = 0; i < height; i += 10) { if (i % 100 == 0) { g.fillRect(0, i + 10, 10, 1); g.fillRect(width - 12, i + 10, 10, 1); if (i == 0) { g.drawString(String.valueOf(i), 12, 15); g.drawString(String.valueOf(i), width - 20, 15); } else if (i == 1000) { g.drawString(String.valueOf(i), 10, i + 15); g.drawString(String.valueOf(i), width - 38, i + 15); } else { g.drawString(String.valueOf(i), 10, i + 15); g.drawString(String.valueOf(i), width - 33, i + 15); } } else if (i % 50 == 0) { g.fillRect(0, i + 10, 8, 1); g.fillRect(width - 10, i + 10, 8, 1); g.drawString(String.valueOf(i), 10, i + 15); if (i == 50) { g.drawString(String.valueOf(i), width - 25, i + 15); } else { g.drawString(String.valueOf(i), width - 32, i + 15); } } else { g.fillRect(0, i + 10, 5, 1); g.fillRect(width - 5, i + 10, 5, 1); } } } } public void repaint() { this.paint(this.getGraphics()); } public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (UnsupportedLookAndFeelException e) { e.printStackTrace(); } new Rule().init(); } }