概述
packagecom.rosedata;
importjava.awt.Point;
importjava.awt.Rectangle;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.MouseAdapter;
importjava.awt.event.MouseEvent;
importjava.awt.event.MouseMotionAdapter;
importjavax.swing.JFrame;
importjavax.swing.Timer;
publicclassHideFrameextendsJFrameimplementsActionListener{
privateRectangle rect;
privateintframeLeft;// 窗体离屏幕左边的距离
privateintframeRight;// 窗体离屏幕右边的距离;
privateintframeTop;// 窗体离屏幕顶部的距离
privateintframeWidth;// 窗体的宽
privateintframeHeight;// 窗体的高
privateintscreenXX;// 屏幕的宽度;
privatePoint point;// 鼠标在窗体的位置
privateTimer timer =newTimer(10,this);
privateintxx, yy;
privatebooleanisDraging =false;
publicHideFrame(){
timer.start();
setTitle("窗体在屏幕边缘隐藏演示");
setSize(400,300);
setUndecorated(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setAlwaysOnTop(true);
addMouseListener(newMouseAdapter() {
publicvoidmousePressed(MouseEvent e) {
isDraging = true;
xx = e.getX();
yy = e.getY();
}
publicvoidmouseReleased(MouseEvent e) {
isDraging = false;
}
});
addMouseMotionListener(newMouseMotionAdapter() {
publicvoidmouseDragged(MouseEvent e) {
if(isDraging) {
intleft = getLocation().x;
inttop = getLocation().y;
setLocation(left + e.getX() - xx, top + e.getY() - yy);
repaint();
}
}
});
setVisible(true);
}
@Override
publicvoidactionPerformed(ActionEvent arg0) {
frameLeft = getLocationOnScreen().x;
frameTop = getLocationOnScreen().y;
frameWidth = getWidth();
frameHeight = getHeight();
screenXX = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
frameRight =screenXX- frameLeft - frameWidth;
// 获取窗体的轮廓
rect = newRectangle(0,0, frameWidth, frameHeight);
// 获取鼠标在窗体的位置
point =getMousePosition();
if(frameLeft <0&& isPtInRect(rect, point)) {
setLocation(0, frameTop);// 隐藏在左边,鼠标指到后显示窗体;
} elseif(frameLeft > -5&& frameLeft <5&& !(isPtInRect(rect, point))) {
setLocation(frameLeft - frameWidth + 1, frameTop);// 窗体移到左边边缘隐藏到左边;
} elseif((frameTop <0&& frameLeft <0) && isPtInRect(rect, point)) {// 窗体在左上角;
setLocation(0,0);// 窗口隐藏了,鼠标指到他,就显示出来;
} elseif((frameTop > -5&& frameTop <5) && (frameLeft > -5&& frameLeft <5) && !(isPtInRect(rect, point))) {
// 当窗体的上边框与屏幕的顶端的距离小于5时 ,
// 并且鼠标不再窗体上将窗体隐藏到屏幕的顶端
setLocation(frameLeft - frameWidth + 1,1);
} elseif((frameTop <0) && isPtInRect(rect, point)) {
setLocation(frameLeft, 0);// 窗口隐藏了,鼠标指到他,就显示出来;
} elseif(frameTop > -5&& frameTop <5&& !(isPtInRect(rect, point))) {
// 当窗体的上边框与屏幕的顶端的距离小于5时 ,
// 并且鼠标不再窗体上将窗体隐藏到屏幕的顶端
setLocation(frameLeft, 1- frameHeight);
} elseif(frameRight <0&& isPtInRect(rect, point)) {
setLocation(screenXX - frameWidth + 1, frameTop);// 隐藏在右边,鼠标指到后显示;
} elseif(frameRight > -5&& frameRight <5&& !(isPtInRect(rect, point))) {
setLocation(screenXX - 1, frameTop);// 窗体移到屏幕右边边缘隐藏到右边;
} elseif(frameRight <0&& frameTop <0&& isPtInRect(rect, point)) {// 窗体在右上角;
setLocation(screenXX - frameWidth + 1,0);// 隐藏在右边,鼠标指到后显示;
} elseif((frameRight > -5&& frameRight <5) && (frameTop > -5&& frameTop <5) && !(isPtInRect(rect, point))) {
setLocation(screenXX - 1,1);// 窗体移到屏幕右边边缘隐藏到右边;
}
}
/**
* 检测是否在矩形框内
* @param rect
* @param point
*/
publicbooleanisPtInRect(Rectangle rect, Point point) {
if(rect !=null&& point !=null) {
intx0 = rect.x;
inty0 = rect.y;
intx1 = rect.width;
inty1 = rect.height;
intx = point.x;
inty = point.y;
returnx >= x0 && x = y0 && y
}
returnfalse;
}
publicstaticvoidmain(String[] args){
HideFrame frame = newHideFrame();
}
}
最后
以上就是默默鱼为你收集整理的java怎么实现窗体隐藏,Java实现窗体边缘隐藏的全部内容,希望文章能够帮你解决java怎么实现窗体隐藏,Java实现窗体边缘隐藏所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复