JFrame在去掉标题栏的情况下怎么实现拉大收缩边框的功能? JFrame在去掉标题栏的情况下怎么实现拉大收缩边框的功能?谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 只能自己实现了,思路是把JFrame分成九个区域,八个boolean状态变量,当然,是虚拟的划分,注册mouse move事件,当鼠标进入不同区域的时候设置八个boolean状态变量的值,同时改变鼠标指针状态,然后注册mouse down事件,根据八个boolean状态变量的值确定应该如何Resize,还是比较复杂的。 import javax.swing.*;import java.awt.*;import java.awt.event.*;public class ResizeFrame extends JFrame { private boolean isTopLeft; private boolean isTop; private boolean isTopRight; private boolean isRight; private boolean isBottomRight; private boolean isBottom; private boolean isBottomLeft; private boolean isLeft; private final static int RESIZE_WIDTH = 5; private final static int MIN_WIDTH = 20; private final static int MIN_HEIGHT = 20; public ResizeFrame() { addMouseMotionListener(new ResizeAdapter(this)); } private class ResizeAdapter extends MouseAdapter { private Component c; public ResizeAdapter(Component c) { this.c = c; } @Override public void mouseMoved(MouseEvent event) { int x = event.getX(); int y = event.getY(); int width = c.getWidth(); int height = c.getHeight(); int cursorType = Cursor.DEFAULT_CURSOR; isTopLeft = isTop = isTopRight = isRight = isBottomRight = isBottom = isBottomLeft = isLeft = false; if (y <= RESIZE_WIDTH) { if (x <= RESIZE_WIDTH) { isTopLeft = true; cursorType = Cursor.NW_RESIZE_CURSOR; } else if (x >= width - RESIZE_WIDTH) { isTopRight = true; cursorType = Cursor.NE_RESIZE_CURSOR; } else { isTop = true; cursorType = Cursor.N_RESIZE_CURSOR; } } else if (y >= height - RESIZE_WIDTH) { if (x <= RESIZE_WIDTH) { isBottomLeft = true; cursorType = Cursor.SW_RESIZE_CURSOR; } else if (x >= width - RESIZE_WIDTH) { isBottomRight = true; cursorType = Cursor.SE_RESIZE_CURSOR; } else { isBottom = true; cursorType = Cursor.S_RESIZE_CURSOR; } } else if (x <= RESIZE_WIDTH) { isLeft = true; cursorType = Cursor.W_RESIZE_CURSOR; } else if (x >= width - RESIZE_WIDTH) { isRight = true; cursorType = Cursor.E_RESIZE_CURSOR; } c.setCursor(new Cursor(cursorType)); } @Override public void mouseDragged(MouseEvent event) { int x = event.getX(); int y = event.getY(); int width = c.getWidth(); int height = c.getHeight(); if (isTopLeft) { c.setLocation(c.getX() + x, c.getY() + y); c.setSize(width - x, height - y); } if (isTop) { c.setLocation(c.getX(), c.getY() + y); c.setSize(width, height - y); } if (isTopRight) { c.setLocation(c.getX(), c.getY() + y); c.setSize(x, height - y); } if (isRight) { c.setSize(x, height); } if (isBottomRight) { c.setSize(x, y); } if (isBottom) { c.setSize(width, y); } if (isBottomLeft) { c.setLocation(c.getX() + x, c.getY()); c.setSize(width - x, y); } if (isLeft) { c.setLocation(c.getX() + x, c.getY()); c.setSize(width - x, height); } } } public static void main(String[] args) { JFrame frame = new ResizeFrame(); frame.setLocation(200, 200); frame.setSize(400, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setUndecorated(true); frame.setVisible(true); }}先这么多,还有BUG,最小宽度和最小高度的处理还没弄,下班了,晚上回家再说。 [code=Java]import javax.swing.*;import java.awt.*;import java.awt.event.*;public class ResizeFrame extends JFrame { private boolean isTopLeft;// 是否处于左上角调整窗口状态 private boolean isTop;// 是否处于上边界调整窗口状态 private boolean isTopRight;// 是否处于右上角调整窗口状态 private boolean isRight;// 是否处于右边界调整窗口状态 private boolean isBottomRight;// 是否处于右下角调整窗口状态 private boolean isBottom;// 是否处于下边界调整窗口状态 private boolean isBottomLeft;// 是否处于左下角调整窗口状态 private boolean isLeft;// 是否处于左边界调整窗口状态 private final static int RESIZE_WIDTH = 5;// 判定是否为调整窗口状态的范围与边界距离 private final static int MIN_WIDTH = 20;// 窗口最小宽度 private final static int MIN_HEIGHT = 20;// 窗口最小高度 public ResizeFrame() { addMouseMotionListener(new ResizeAdapter(this)); } private class ResizeAdapter extends MouseAdapter { private Component c; public ResizeAdapter(Component c) { this.c = c; } @Override public void mouseMoved(MouseEvent event) { int x = event.getX(); int y = event.getY(); int width = c.getWidth(); int height = c.getHeight(); int cursorType = Cursor.DEFAULT_CURSOR;// 鼠标光标初始为默认类型,若未进入调整窗口状态,保持默认类型 // 先将所有调整窗口状态重置 isTopLeft = isTop = isTopRight = isRight = isBottomRight = isBottom = isBottomLeft = isLeft = false; if (y <= RESIZE_WIDTH) { if (x <= RESIZE_WIDTH) {// 左上角调整窗口状态 isTopLeft = true; cursorType = Cursor.NW_RESIZE_CURSOR; } else if (x >= width - RESIZE_WIDTH) {// 右上角调整窗口状态 isTopRight = true; cursorType = Cursor.NE_RESIZE_CURSOR; } else {// 上边界调整窗口状态 isTop = true; cursorType = Cursor.N_RESIZE_CURSOR; } } else if (y >= height - RESIZE_WIDTH) { if (x <= RESIZE_WIDTH) {// 左下角调整窗口状态 isBottomLeft = true; cursorType = Cursor.SW_RESIZE_CURSOR; } else if (x >= width - RESIZE_WIDTH) {// 右下角调整窗口状态 isBottomRight = true; cursorType = Cursor.SE_RESIZE_CURSOR; } else {// 下边界调整窗口状态 isBottom = true; cursorType = Cursor.S_RESIZE_CURSOR; } } else if (x <= RESIZE_WIDTH) {// 左边界调整窗口状态 isLeft = true; cursorType = Cursor.W_RESIZE_CURSOR; } else if (x >= width - RESIZE_WIDTH) {// 右边界调整窗口状态 isRight = true; cursorType = Cursor.E_RESIZE_CURSOR; } // 最后改变鼠标光标 c.setCursor(new Cursor(cursorType)); } @Override public void mouseDragged(MouseEvent event) { int x = event.getX(); int y = event.getY(); int width = c.getWidth(); int height = c.getHeight(); // 保存窗口改变后的x、y坐标和宽度、高度,用于预判是否会小于最小宽度、最小高度 int nextX = c.getX(); int nextY = c.getY(); int nextWidth = width; int nextHeight = height; if (isTopLeft || isLeft || isBottomLeft) {// 所有左边调整窗口状态 nextX += x; nextWidth -= x; } if (isTopLeft || isTop || isTopRight) {// 所有上边调整窗口状态 nextY += y; nextHeight -= y; } if (isTopRight || isRight || isBottomRight) {// 所有右边调整窗口状态 nextWidth = x; } if (isBottomLeft || isBottom || isBottomRight) {// 所有下边调整窗口状态 nextHeight = y; } if (nextWidth <= MIN_WIDTH) {// 如果窗口改变后的宽度小于最小宽度,则宽度调整到最小宽度 nextWidth = MIN_WIDTH; if (isTopLeft || isLeft || isBottomLeft) {// 如果是从左边缩小的窗口,x坐标也要调整 nextX = c.getX() + width - nextWidth; } } if (nextHeight <= MIN_HEIGHT) {// 如果窗口改变后的高度小于最小高度,则高度调整到最小高度 nextHeight = MIN_HEIGHT; if (isTopLeft || isTop || isTopRight) {// 如果是从上边缩小的窗口,y坐标也要调整 nextY = c.getY() + height - nextHeight; } } // 最后统一改变窗口的x、y坐标和宽度、高度,可以防止刷新频繁出现的屏闪情况 setBounds(nextX, nextY, nextWidth, nextHeight); } } public static void main(String[] args) { // 一个简单的演示小例子 JFrame frame = new ResizeFrame(); frame.setSize(400, 300); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation((screenSize.width - frame.getWidth()) / 2, (screenSize.height - frame.getHeight()) / 2); frame.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent event) { if (event.getClickCount() > 1) { System.exit(0); } } }); frame.setUndecorated(true); frame.setVisible(true); }}[code]这个是完整版,更新如下:1、优化了一下代码结构2、解决了限制最小宽度和最小高度的问题3、解决了左边和上边调整大小时的屏闪问题4、加入了注释5、解决了无法在窗体上直接结束程序的问题(双击窗体结束程序) import javax.swing.*;import java.awt.*;import java.awt.event.*;public class ResizeFrame extends JFrame { private boolean isTopLeft;// 是否处于左上角调整窗口状态 private boolean isTop;// 是否处于上边界调整窗口状态 private boolean isTopRight;// 是否处于右上角调整窗口状态 private boolean isRight;// 是否处于右边界调整窗口状态 private boolean isBottomRight;// 是否处于右下角调整窗口状态 private boolean isBottom;// 是否处于下边界调整窗口状态 private boolean isBottomLeft;// 是否处于左下角调整窗口状态 private boolean isLeft;// 是否处于左边界调整窗口状态 private final static int RESIZE_WIDTH = 5;// 判定是否为调整窗口状态的范围与边界距离 private final static int MIN_WIDTH = 20;// 窗口最小宽度 private final static int MIN_HEIGHT = 20;// 窗口最小高度 public ResizeFrame() { addMouseMotionListener(new ResizeAdapter(this)); } private class ResizeAdapter extends MouseAdapter { private Component c; public ResizeAdapter(Component c) { this.c = c; } @Override public void mouseMoved(MouseEvent event) { int x = event.getX(); int y = event.getY(); int width = c.getWidth(); int height = c.getHeight(); int cursorType = Cursor.DEFAULT_CURSOR;// 鼠标光标初始为默认类型,若未进入调整窗口状态,保持默认类型 // 先将所有调整窗口状态重置 isTopLeft = isTop = isTopRight = isRight = isBottomRight = isBottom = isBottomLeft = isLeft = false; if (y <= RESIZE_WIDTH) { if (x <= RESIZE_WIDTH) {// 左上角调整窗口状态 isTopLeft = true; cursorType = Cursor.NW_RESIZE_CURSOR; } else if (x >= width - RESIZE_WIDTH) {// 右上角调整窗口状态 isTopRight = true; cursorType = Cursor.NE_RESIZE_CURSOR; } else {// 上边界调整窗口状态 isTop = true; cursorType = Cursor.N_RESIZE_CURSOR; } } else if (y >= height - RESIZE_WIDTH) { if (x <= RESIZE_WIDTH) {// 左下角调整窗口状态 isBottomLeft = true; cursorType = Cursor.SW_RESIZE_CURSOR; } else if (x >= width - RESIZE_WIDTH) {// 右下角调整窗口状态 isBottomRight = true; cursorType = Cursor.SE_RESIZE_CURSOR; } else {// 下边界调整窗口状态 isBottom = true; cursorType = Cursor.S_RESIZE_CURSOR; } } else if (x <= RESIZE_WIDTH) {// 左边界调整窗口状态 isLeft = true; cursorType = Cursor.W_RESIZE_CURSOR; } else if (x >= width - RESIZE_WIDTH) {// 右边界调整窗口状态 isRight = true; cursorType = Cursor.E_RESIZE_CURSOR; } // 最后改变鼠标光标 c.setCursor(new Cursor(cursorType)); } @Override public void mouseDragged(MouseEvent event) { int x = event.getX(); int y = event.getY(); int width = c.getWidth(); int height = c.getHeight(); // 保存窗口改变后的x、y坐标和宽度、高度,用于预判是否会小于最小宽度、最小高度 int nextX = c.getX(); int nextY = c.getY(); int nextWidth = width; int nextHeight = height; if (isTopLeft || isLeft || isBottomLeft) {// 所有左边调整窗口状态 nextX += x; nextWidth -= x; } if (isTopLeft || isTop || isTopRight) {// 所有上边调整窗口状态 nextY += y; nextHeight -= y; } if (isTopRight || isRight || isBottomRight) {// 所有右边调整窗口状态 nextWidth = x; } if (isBottomLeft || isBottom || isBottomRight) {// 所有下边调整窗口状态 nextHeight = y; } if (nextWidth <= MIN_WIDTH) {// 如果窗口改变后的宽度小于最小宽度,则宽度调整到最小宽度 nextWidth = MIN_WIDTH; if (isTopLeft || isLeft || isBottomLeft) {// 如果是从左边缩小的窗口,x坐标也要调整 nextX = c.getX() + width - nextWidth; } } if (nextHeight <= MIN_HEIGHT) {// 如果窗口改变后的高度小于最小高度,则高度调整到最小高度 nextHeight = MIN_HEIGHT; if (isTopLeft || isTop || isTopRight) {// 如果是从上边缩小的窗口,y坐标也要调整 nextY = c.getY() + height - nextHeight; } } // 最后统一改变窗口的x、y坐标和宽度、高度,可以防止刷新频繁出现的屏闪情况 setBounds(nextX, nextY, nextWidth, nextHeight); } } public static void main(String[] args) { // 一个简单的演示小例子 JFrame frame = new ResizeFrame(); frame.setSize(400, 300); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation((screenSize.width - frame.getWidth()) / 2, (screenSize.height - frame.getHeight()) / 2); frame.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent event) { if (event.getClickCount() > 1) { System.exit(0); } } }); frame.setUndecorated(true); frame.setVisible(true); }}这个是完整版,更新如下:1、优化了一下代码结构2、解决了限制最小宽度和最小高度的问题3、解决了左边和上边调整大小时的屏闪问题4、加入了注释5、解决了无法在窗体上直接结束程序的问题(双击窗体结束程序)一个斜杠和CSDN帖子不能修改引发的杯具…… 最近刚看这些 刚好用到证发愁呢哈哈 谢谢Tassdars 谢谢楼主 问题解决 运算符 如何求数的精度 java 小菜遇到的找不到符号的问题(java.lang.string) 请教 图片加载类(GUI) 请教高手一个JAVA显示图形的问题!!!!!!!!!!!!!!!!!!!!! 初学者在mysql中的小问题 如何把一个JPG图片尺寸缩小而不压缩图片的质量 焦急!StringBuffer问题 [诚送60分]何用Java 的socket 来实现服务器和客户端的图象交互传输,急盼!!! 约瑟夫问题求解!! 类型转换问题,来帮忙解决下
import java.awt.*;
import java.awt.event.*;public class ResizeFrame extends JFrame {
private boolean isTopLeft;
private boolean isTop;
private boolean isTopRight;
private boolean isRight;
private boolean isBottomRight;
private boolean isBottom;
private boolean isBottomLeft;
private boolean isLeft;
private final static int RESIZE_WIDTH = 5;
private final static int MIN_WIDTH = 20;
private final static int MIN_HEIGHT = 20;
public ResizeFrame() {
addMouseMotionListener(new ResizeAdapter(this));
}
private class ResizeAdapter extends MouseAdapter {
private Component c;
public ResizeAdapter(Component c) {
this.c = c;
}
@Override
public void mouseMoved(MouseEvent event) {
int x = event.getX();
int y = event.getY();
int width = c.getWidth();
int height = c.getHeight();
int cursorType = Cursor.DEFAULT_CURSOR;
isTopLeft = isTop = isTopRight = isRight = isBottomRight = isBottom = isBottomLeft = isLeft = false;
if (y <= RESIZE_WIDTH) {
if (x <= RESIZE_WIDTH) {
isTopLeft = true;
cursorType = Cursor.NW_RESIZE_CURSOR;
} else if (x >= width - RESIZE_WIDTH) {
isTopRight = true;
cursorType = Cursor.NE_RESIZE_CURSOR;
} else {
isTop = true;
cursorType = Cursor.N_RESIZE_CURSOR;
}
} else if (y >= height - RESIZE_WIDTH) {
if (x <= RESIZE_WIDTH) {
isBottomLeft = true;
cursorType = Cursor.SW_RESIZE_CURSOR;
} else if (x >= width - RESIZE_WIDTH) {
isBottomRight = true;
cursorType = Cursor.SE_RESIZE_CURSOR;
} else {
isBottom = true;
cursorType = Cursor.S_RESIZE_CURSOR;
}
} else if (x <= RESIZE_WIDTH) {
isLeft = true;
cursorType = Cursor.W_RESIZE_CURSOR;
} else if (x >= width - RESIZE_WIDTH) {
isRight = true;
cursorType = Cursor.E_RESIZE_CURSOR;
}
c.setCursor(new Cursor(cursorType));
}
@Override
public void mouseDragged(MouseEvent event) {
int x = event.getX();
int y = event.getY();
int width = c.getWidth();
int height = c.getHeight();
if (isTopLeft) {
c.setLocation(c.getX() + x, c.getY() + y);
c.setSize(width - x, height - y);
}
if (isTop) {
c.setLocation(c.getX(), c.getY() + y);
c.setSize(width, height - y);
}
if (isTopRight) {
c.setLocation(c.getX(), c.getY() + y);
c.setSize(x, height - y);
}
if (isRight) {
c.setSize(x, height);
}
if (isBottomRight) {
c.setSize(x, y);
}
if (isBottom) {
c.setSize(width, y);
}
if (isBottomLeft) {
c.setLocation(c.getX() + x, c.getY());
c.setSize(width - x, y);
}
if (isLeft) {
c.setLocation(c.getX() + x, c.getY());
c.setSize(width - x, height);
}
}
}
public static void main(String[] args) {
JFrame frame = new ResizeFrame();
frame.setLocation(200, 200);
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.setVisible(true);
}
}先这么多,还有BUG,最小宽度和最小高度的处理还没弄,下班了,晚上回家再说。
import java.awt.*;
import java.awt.event.*;public class ResizeFrame extends JFrame {
private boolean isTopLeft;// 是否处于左上角调整窗口状态
private boolean isTop;// 是否处于上边界调整窗口状态
private boolean isTopRight;// 是否处于右上角调整窗口状态
private boolean isRight;// 是否处于右边界调整窗口状态
private boolean isBottomRight;// 是否处于右下角调整窗口状态
private boolean isBottom;// 是否处于下边界调整窗口状态
private boolean isBottomLeft;// 是否处于左下角调整窗口状态
private boolean isLeft;// 是否处于左边界调整窗口状态
private final static int RESIZE_WIDTH = 5;// 判定是否为调整窗口状态的范围与边界距离
private final static int MIN_WIDTH = 20;// 窗口最小宽度
private final static int MIN_HEIGHT = 20;// 窗口最小高度
public ResizeFrame() {
addMouseMotionListener(new ResizeAdapter(this));
}
private class ResizeAdapter extends MouseAdapter {
private Component c;
public ResizeAdapter(Component c) {
this.c = c;
}
@Override
public void mouseMoved(MouseEvent event) {
int x = event.getX();
int y = event.getY();
int width = c.getWidth();
int height = c.getHeight();
int cursorType = Cursor.DEFAULT_CURSOR;// 鼠标光标初始为默认类型,若未进入调整窗口状态,保持默认类型
// 先将所有调整窗口状态重置
isTopLeft = isTop = isTopRight = isRight = isBottomRight = isBottom = isBottomLeft = isLeft = false;
if (y <= RESIZE_WIDTH) {
if (x <= RESIZE_WIDTH) {// 左上角调整窗口状态
isTopLeft = true;
cursorType = Cursor.NW_RESIZE_CURSOR;
} else if (x >= width - RESIZE_WIDTH) {// 右上角调整窗口状态
isTopRight = true;
cursorType = Cursor.NE_RESIZE_CURSOR;
} else {// 上边界调整窗口状态
isTop = true;
cursorType = Cursor.N_RESIZE_CURSOR;
}
} else if (y >= height - RESIZE_WIDTH) {
if (x <= RESIZE_WIDTH) {// 左下角调整窗口状态
isBottomLeft = true;
cursorType = Cursor.SW_RESIZE_CURSOR;
} else if (x >= width - RESIZE_WIDTH) {// 右下角调整窗口状态
isBottomRight = true;
cursorType = Cursor.SE_RESIZE_CURSOR;
} else {// 下边界调整窗口状态
isBottom = true;
cursorType = Cursor.S_RESIZE_CURSOR;
}
} else if (x <= RESIZE_WIDTH) {// 左边界调整窗口状态
isLeft = true;
cursorType = Cursor.W_RESIZE_CURSOR;
} else if (x >= width - RESIZE_WIDTH) {// 右边界调整窗口状态
isRight = true;
cursorType = Cursor.E_RESIZE_CURSOR;
}
// 最后改变鼠标光标
c.setCursor(new Cursor(cursorType));
}
@Override
public void mouseDragged(MouseEvent event) {
int x = event.getX();
int y = event.getY();
int width = c.getWidth();
int height = c.getHeight();
// 保存窗口改变后的x、y坐标和宽度、高度,用于预判是否会小于最小宽度、最小高度
int nextX = c.getX();
int nextY = c.getY();
int nextWidth = width;
int nextHeight = height;
if (isTopLeft || isLeft || isBottomLeft) {// 所有左边调整窗口状态
nextX += x;
nextWidth -= x;
}
if (isTopLeft || isTop || isTopRight) {// 所有上边调整窗口状态
nextY += y;
nextHeight -= y;
}
if (isTopRight || isRight || isBottomRight) {// 所有右边调整窗口状态
nextWidth = x;
}
if (isBottomLeft || isBottom || isBottomRight) {// 所有下边调整窗口状态
nextHeight = y;
}
if (nextWidth <= MIN_WIDTH) {// 如果窗口改变后的宽度小于最小宽度,则宽度调整到最小宽度
nextWidth = MIN_WIDTH;
if (isTopLeft || isLeft || isBottomLeft) {// 如果是从左边缩小的窗口,x坐标也要调整
nextX = c.getX() + width - nextWidth;
}
}
if (nextHeight <= MIN_HEIGHT) {// 如果窗口改变后的高度小于最小高度,则高度调整到最小高度
nextHeight = MIN_HEIGHT;
if (isTopLeft || isTop || isTopRight) {// 如果是从上边缩小的窗口,y坐标也要调整
nextY = c.getY() + height - nextHeight;
}
}
// 最后统一改变窗口的x、y坐标和宽度、高度,可以防止刷新频繁出现的屏闪情况
setBounds(nextX, nextY, nextWidth, nextHeight);
}
}
public static void main(String[] args) {
// 一个简单的演示小例子
JFrame frame = new ResizeFrame();
frame.setSize(400, 300);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((screenSize.width - frame.getWidth()) / 2, (screenSize.height - frame.getHeight()) / 2);
frame.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent event) {
if (event.getClickCount() > 1) {
System.exit(0);
}
}
});
frame.setUndecorated(true);
frame.setVisible(true);
}
}[code]这个是完整版,更新如下:
1、优化了一下代码结构
2、解决了限制最小宽度和最小高度的问题
3、解决了左边和上边调整大小时的屏闪问题
4、加入了注释
5、解决了无法在窗体上直接结束程序的问题(双击窗体结束程序)
import java.awt.*;
import java.awt.event.*;public class ResizeFrame extends JFrame {
private boolean isTopLeft;// 是否处于左上角调整窗口状态
private boolean isTop;// 是否处于上边界调整窗口状态
private boolean isTopRight;// 是否处于右上角调整窗口状态
private boolean isRight;// 是否处于右边界调整窗口状态
private boolean isBottomRight;// 是否处于右下角调整窗口状态
private boolean isBottom;// 是否处于下边界调整窗口状态
private boolean isBottomLeft;// 是否处于左下角调整窗口状态
private boolean isLeft;// 是否处于左边界调整窗口状态
private final static int RESIZE_WIDTH = 5;// 判定是否为调整窗口状态的范围与边界距离
private final static int MIN_WIDTH = 20;// 窗口最小宽度
private final static int MIN_HEIGHT = 20;// 窗口最小高度
public ResizeFrame() {
addMouseMotionListener(new ResizeAdapter(this));
}
private class ResizeAdapter extends MouseAdapter {
private Component c;
public ResizeAdapter(Component c) {
this.c = c;
}
@Override
public void mouseMoved(MouseEvent event) {
int x = event.getX();
int y = event.getY();
int width = c.getWidth();
int height = c.getHeight();
int cursorType = Cursor.DEFAULT_CURSOR;// 鼠标光标初始为默认类型,若未进入调整窗口状态,保持默认类型
// 先将所有调整窗口状态重置
isTopLeft = isTop = isTopRight = isRight = isBottomRight = isBottom = isBottomLeft = isLeft = false;
if (y <= RESIZE_WIDTH) {
if (x <= RESIZE_WIDTH) {// 左上角调整窗口状态
isTopLeft = true;
cursorType = Cursor.NW_RESIZE_CURSOR;
} else if (x >= width - RESIZE_WIDTH) {// 右上角调整窗口状态
isTopRight = true;
cursorType = Cursor.NE_RESIZE_CURSOR;
} else {// 上边界调整窗口状态
isTop = true;
cursorType = Cursor.N_RESIZE_CURSOR;
}
} else if (y >= height - RESIZE_WIDTH) {
if (x <= RESIZE_WIDTH) {// 左下角调整窗口状态
isBottomLeft = true;
cursorType = Cursor.SW_RESIZE_CURSOR;
} else if (x >= width - RESIZE_WIDTH) {// 右下角调整窗口状态
isBottomRight = true;
cursorType = Cursor.SE_RESIZE_CURSOR;
} else {// 下边界调整窗口状态
isBottom = true;
cursorType = Cursor.S_RESIZE_CURSOR;
}
} else if (x <= RESIZE_WIDTH) {// 左边界调整窗口状态
isLeft = true;
cursorType = Cursor.W_RESIZE_CURSOR;
} else if (x >= width - RESIZE_WIDTH) {// 右边界调整窗口状态
isRight = true;
cursorType = Cursor.E_RESIZE_CURSOR;
}
// 最后改变鼠标光标
c.setCursor(new Cursor(cursorType));
}
@Override
public void mouseDragged(MouseEvent event) {
int x = event.getX();
int y = event.getY();
int width = c.getWidth();
int height = c.getHeight();
// 保存窗口改变后的x、y坐标和宽度、高度,用于预判是否会小于最小宽度、最小高度
int nextX = c.getX();
int nextY = c.getY();
int nextWidth = width;
int nextHeight = height;
if (isTopLeft || isLeft || isBottomLeft) {// 所有左边调整窗口状态
nextX += x;
nextWidth -= x;
}
if (isTopLeft || isTop || isTopRight) {// 所有上边调整窗口状态
nextY += y;
nextHeight -= y;
}
if (isTopRight || isRight || isBottomRight) {// 所有右边调整窗口状态
nextWidth = x;
}
if (isBottomLeft || isBottom || isBottomRight) {// 所有下边调整窗口状态
nextHeight = y;
}
if (nextWidth <= MIN_WIDTH) {// 如果窗口改变后的宽度小于最小宽度,则宽度调整到最小宽度
nextWidth = MIN_WIDTH;
if (isTopLeft || isLeft || isBottomLeft) {// 如果是从左边缩小的窗口,x坐标也要调整
nextX = c.getX() + width - nextWidth;
}
}
if (nextHeight <= MIN_HEIGHT) {// 如果窗口改变后的高度小于最小高度,则高度调整到最小高度
nextHeight = MIN_HEIGHT;
if (isTopLeft || isTop || isTopRight) {// 如果是从上边缩小的窗口,y坐标也要调整
nextY = c.getY() + height - nextHeight;
}
}
// 最后统一改变窗口的x、y坐标和宽度、高度,可以防止刷新频繁出现的屏闪情况
setBounds(nextX, nextY, nextWidth, nextHeight);
}
}
public static void main(String[] args) {
// 一个简单的演示小例子
JFrame frame = new ResizeFrame();
frame.setSize(400, 300);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((screenSize.width - frame.getWidth()) / 2, (screenSize.height - frame.getHeight()) / 2);
frame.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent event) {
if (event.getClickCount() > 1) {
System.exit(0);
}
}
});
frame.setUndecorated(true);
frame.setVisible(true);
}
}这个是完整版,更新如下:
1、优化了一下代码结构
2、解决了限制最小宽度和最小高度的问题
3、解决了左边和上边调整大小时的屏闪问题
4、加入了注释
5、解决了无法在窗体上直接结束程序的问题(双击窗体结束程序)一个斜杠和CSDN帖子不能修改引发的杯具……
问题解决