Java像QQ把窗口放到屏幕边缘并隐藏
来源:互联网 发布:淘宝的详情页怎样发布 编辑:程序博客网 时间:2024/05/22 04:11
其实可以这么来做:
1.添加鼠标监听事件,判定鼠标是否在类似QQ窗口标题栏按下.记录当前鼠标位置和标题栏右侧距离X.
2.在鼠标事件监听中判定前一条件成立下的鼠标释放时的标题栏左侧位置(记为newX).若newX>屏幕宽度,调用窗口重绘方法(自己定义:即把窗口绘制成一条竖线的样子,但是必须得有面积,并且保存此时窗口位置)将窗口添加到桌面边缘并隐藏主窗口.
3.在2中添加一个监听线程,判定用户鼠标动作是否落在重绘窗口范围内,若true则调用主窗口显示,并且将边缘窗口隐藏.
上面有个地方打错了,不好意思.
2中应该是标题栏右侧位置newX,不过看你的程序怎么样判定啦,无关紧要.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyFrame extends JFrame implements ActionListener {
Rectangle rect;
int left;// 窗体离屏幕左边的距离
int top;// 窗体离屏幕顶部的距离
int width; // 窗体的宽
int height;// 窗体的高
Point point;// 鼠标在窗体的位置
Timer timer = new Timer(10, this);
public static void main(String[] args) {
new MyFrame();
}
public MyFrame() {
timer.start();
this.setTitle("中国");
this.setSize(200, 600);
this.setLocation(700, 300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
left = getLocationOnScreen().x;
top = getLocationOnScreen().y;
width = getWidth();
height = getHeight();
// 获取窗体的轮廓
rect = new Rectangle(0, 0, width, height);
// 获取鼠标在窗体的位置
point = getMousePosition();
if ((top < 0) && isPtInRect(rect, point)) {
// 当鼠标在当前窗体内,并且窗体的Top属性小于0
// 设置窗体的Top属性为0,就是将窗口上边沿紧靠顶部
setLocation(left, 0);
} else if (top > -5 && top < 5 && !(isPtInRect(rect, point))) {
// 当窗体的上边框与屏幕的顶端的距离小于5时 ,
// 并且鼠标不再窗体上 将QQ窗体隐藏到屏幕的顶端
setLocation(left, 5 - height);
}
}
public boolean isPtInRect(Rectangle rect, Point point) {
if (rect != null && point != null) {
int x0 = rect.x;
int y0 = rect.y;
int x1 = rect.width;
int y1 = rect.height;
int x = point.x;
int y = point.y;
return x >= x0 && x < x1 && y >= y0 && y < y1;
}
return false;
}
}
第二个,用事件听
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class MyFrame extends JFrame implements ActionListener,MouseListener { int left;// 窗体离屏幕左边的距离 int top;// 窗体离屏幕顶部的距离 int width; // 窗体的宽 int height;// 窗体的高 Timer timer = new Timer(10, this); int state;//窗体状态 public static void main(String[] args) { new MyFrame(); } public MyFrame() { timer.start(); this.addMouseListener(this); this.setTitle("中国"); this.setSize(200, 600); this.setLocation(700, 300); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { System.out.println("ffdd"); left = getLocationOnScreen().x; top = getLocationOnScreen().y; width = getWidth(); height = getHeight(); if ((top < 0)) { this.state=1; } if(this.state==2){ setLocation(left, 5 - height); } }public void mouseEntered(MouseEvent e) {if(this.state==1){ setLocation(left, 0);}}public void mouseExited(MouseEvent e) {if(top > -5 && top < 5){this.state=2;}}public void mouseClicked(MouseEvent e) {}public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) {}}
- Java像QQ把窗口放到屏幕边缘并隐藏
- Java像QQ把窗口放到屏幕边缘并隐藏
- Qt窗口像QQ在屏幕顶端自动隐藏
- qt实现的窗口吸附屏幕边缘,动画隐藏显示的效果,类似于qq主窗口 吸附 隐藏的效果
- 窗体靠近屏幕边缘自动隐藏*学习(类似于QQ)
- QQ在屏幕边缘自动隐藏不能正常显示
- 像QQ一样当窗体在屏幕边缘时停靠(转载)
- C# 窗体靠近屏幕边缘自动隐藏*学习(类似于QQ)
- Java实现窗体隐藏,功能类似于QQ窗口自动隐藏
- 窗口在屏幕边缘自动隐藏的实现(动态收缩与动态弹出)
- 把批处理文件打包成可执行文件并隐藏黑窗口
- 把QQ好友放到桌面上,快捷聊天
- 把QQ好友放到桌面上,快捷聊天
- C++把窗口隐藏
- QQ桌面边缘停靠隐藏显示问题
- 把浮动窗口放到SELECT 元素上
- Ubuntu把关闭窗口按键放到右边
- Ubuntu把关闭窗口按键放到右边
- linux下清空iptables规则具体方法如下:
- memcached 运行状态监控
- POJ 2349 Arctic Network(Prim算法)
- 市场前瞻:$100平板电脑
- 用汇编的眼光看C++(之拷贝、赋值函数)
- Java像QQ把窗口放到屏幕边缘并隐藏
- Android获取外部和内部存储空间总大小
- poj1207(The 3n + 1 problem)
- IT 圈里那些经常被读错的词
- oracle 个人学习笔记
- firefox中隐藏flash导致 flash重新加载的问题
- int substr( char dst[], char src[], int start, int len )
- redhat+linux5.5下DNS配置文件获取方法+亲测
- ubuntu11.04网络设备未托管 解决方案