java把窗口放右边,Java像QQ把窗口放到屏幕边缘并隐藏

其实可以这么来做:

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;

}

}0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值