java中 实现无标题无边框的窗体能够用鼠标拖动改变窗口大小
来源:互联网 发布:网络检测插件 编辑:程序博客网 时间:2024/05/01 12:22
出现的问题:
1、setUndecorated(true)设置窗体无边框,但是setResizable(true)设置
可以通过鼠标调整大小,不起作用,这是怎么回事?
2、有没有事件,可以监听到,鼠标移动到边缘?如果java设置窗体无边框后,无
法鼠标拖动调整大小,我想自己做监听事件。
需要的效果:
目标:
下面是我做,很简单,但是费了老大劲了!
1、我所提的问题,出现的这种情况;
2、无标题栏,自定义边框,可以根据需要修改,做的简单;
3、像QQ面板一样,需要时,可以根据需要向上加组件;
4、可以拖动修改大小,拖动移动位置;
5、有点小问题,就是左、上拉伸时,右、下有点小小的抖动;
着重:
frame.setUndecorated(true);
Border b = new CompoundBorder(new EtchedBorder(),new LineBorder(Color.RED));
源码:http://download.csdn.net/detail/littlealamo/4845515
上传源码 2种效果对比
package test;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
public class WindowDragSize {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final JFrame frame = new JFrame();
frame.setUndecorated(true);
Border b = new CompoundBorder(new EtchedBorder(),new LineBorder(Color.RED));
final JPanel panel = new JPanel();
panel.setSize(200,100);
panel.setBorder(b);
panel.addMouseMotionListener(new MouseAdapter() {
private boolean top = false;
private boolean down = false;
private boolean left = false;
private boolean right = false;
private boolean drag = false;
private Point lastPoint = null;
private Point draggingAnchor = null;
@Override
public void mouseMoved(MouseEvent e) {
if( e.getPoint().getY() == 0 ){
frame.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
top = true;
}else if(Math.abs(e.getPoint().getY()- frame.getSize().getHeight()) <= 1 ){
frame.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
down = true;
}else if(e.getPoint().getX() == 0 ){
frame.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
left = true;
}else if(Math.abs(e.getPoint().getX()- frame.getSize().getWidth()) <=1 ){
frame.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
right = true;
}else{
frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
draggingAnchor = new Point(e.getX() + panel.getX(), e.getY() + panel.getY());
top = false;
down = false;
left = false;
right = false;
drag = true;
}
}
@Override
public void mouseDragged(MouseEvent e) {
Dimension dimension = frame.getSize();
if(top){
dimension.setSize(dimension.getWidth() ,dimension.getHeight()-e.getY());
frame.setSize(dimension);
frame.setLocation(frame.getLocationOnScreen().x, frame.getLocationOnScreen().y + e.getY());
}else if(down){
dimension.setSize(dimension.getWidth() , e.getY());
frame.setSize(dimension);
}else if(left){
dimension.setSize(dimension.getWidth() - e.getX() ,dimension.getHeight() );
frame.setSize(dimension);
frame.setLocation(frame.getLocationOnScreen().x + e.getX(),frame.getLocationOnScreen().y );
}else if(right){
dimension.setSize(e.getX(),dimension.getHeight());
frame.setSize(dimension);
}else {
frame.setLocation(e.getLocationOnScreen().x - draggingAnchor.x, e.getLocationOnScreen().y - draggingAnchor.y);
}
}
});
frame.setLocation(200,100);
frame.getContentPane().add(panel);
frame.setSize(200,100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
- java中 实现无标题无边框的窗体能够用鼠标拖动改变窗口大小
- Qt 无标题无边框程序的拖动和改变大小
- Qt 无标题无边框程序的拖动和改变大小
- Qt 无标题无边框程序的拖动和改变大小
- MFC无边框的窗体实现用鼠标拖动窗体边缘实现窗体大小变化
- MFC无边框的窗体实现用鼠标拖动窗体边缘实现窗体大小变化
- MFC无边框的窗体实现用鼠标拖动窗体边缘实现窗体大小变化
- MFC无边框的窗体实现用鼠标拖动窗体边缘实现窗体大小变化
- MFC无边框的窗体实现用鼠标拖动窗体边缘实现窗体大小变化
- c++builder如何通过鼠标拖动改变无边框窗体的大小(bsnone)
- Pyqt实现无边框窗口拖动及改变窗口大小
- java swing设置窗体无边框后鼠标还能拖动窗体的方法
- 无边框窗体拖动和改变大小
- Qt无边框窗口实现拖动和改变大小(修改)
- 实现无边框窗体的拖动及窗体大小变化
- 无边框窗体的拖动以及大小改变的实现方式
- 无边框对话框拖动改变大小的实现总结
- 实现无边框可拖动调整大小的阴影窗口
- POJ 1521 哈夫曼树
- Spring Mail邮件发送
- IBM MQ Queue Monitor
- ecshop 订单状态改变后 前后台功能菜单的显示
- 调试小记2
- java中 实现无标题无边框的窗体能够用鼠标拖动改变窗口大小
- SAP发送邮件
- C# 浏览文件按钮选项 浏览文件所在路径
- jquery的ajax请求struts action返回json时XmlHttpRequest.status500错误
- OpenCV源码中Haar训练及提取特征的代码
- jsp乱码解决大全
- zencart初始安装后台目录admin必须修改才能登录问题 .
- 如何实现asp.net中FileUpload文件类型过滤功能 .
- Android: NDK编程入门笔记