问题描述
我想知道是否有可能以这种方式实现JFrame的大小调整,它已被调整大小,例如linux中的标准窗口。更确切地说:如果用户开始拖动,则只有未来大小(如果将预览窗口),而原始内容不会调整大小。一旦用户释放鼠标,Frame就会调整为该大小。在图片中:
(1)调整大小前的状态
(2)用户开始drage(在红色圆圈处)
(3)用户释放鼠标,框架调整大小
是否有可能在Java Swing中意识到这一点?
编辑:
As这个程序有一天也应该在较低的Java RE中作为7运行,我试图将mKorbel建议与translucend Frame的注释中的建议结合起来。结果接近目标,除了
- 在我停止移动鼠标后,contentPane的内容调整大小,而不是在鼠标移动时已发布
- 框架标题会立即调整大小,而不仅仅是我停止拖动框架边框。
- 仅当从右侧或底部调整大小时,它才有效否则内容随着拖动而移动。
我认为第一点可以通过代码和MouseListener的组合来解析,类似于如果mouseReleased(),则调整大小。这是代码,随意尝试。对于进一步的建议,我仍然对任何建议感到满意。
代码是。我希望它可以在这里发布,否则请告诉我任何侵犯版权的行为。黑色JPanel应该是应用程序的内容,因为contentPane保持不可见。
import java.awt。* ;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.beans.PropertyChangeListener;
import javax.swing。*;
import static java.awt.GraphicsDevice.WindowTranslucency。*;
公共类GroundFrame扩展JFrame {
定时器计时器;
JPanel panel2;
public GroundFrame(){
super(GradientTranslucentWindow);
setBackground(new Color(0,0,0,0));
setSize(new Dimension(300,200));
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(){
panel.setBackground(new Color(0,0,0,0));
setContentPane(panel);
setLayout(null);
panel2 = new JPanel();
panel2.setBackground(Color.black);
panel2.setBounds(0,0,getContentPane()。getWidth(),getContentPane()。getHeight());
getContentPane()。add(panel2);
addComponentListener(new ComponentListener(){
@Override
public void componentShown(ComponentEvent e){}
@Override
public void componentResized(ComponentEvent e){
timer = new Timer(50,new Action(){
@Override
public void actionPerformed(ActionEvent e){
if(timer.isRunning()){
} else {
resizePanel(getContentPane()。getSize());
}
}
@Override
public void setEnabled(boolean b){}
@Override
public void removePropertyChangeListener(PropertyChangeListener listener){}
@Override
public void putValue(String ke y,对象值){}
@Override
public boolean isEnabled(){return false;}
@Override
public Object getValue(String key){return null;}
@Override
public void addPropertyChangeListener(PropertyChangeListener listener){}
});
timer.setRepeats(false);
timer.start();
}
@Override
public void componentMoved(ComponentEvent e){}
@Override
public void componentHidden( ComponentEvent e){}
});
}
public void resizePanel(Dimension dim){
panel2.setBounds(0,0,dim.width,dim.height);
repaint();
}
public static void main(String [] args){
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
boolean isPerPixelTranslucencySupported =
gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT);
if(!isPerPixelTranslucencySupported){
System.out.println(
不支持每像素半透明度);
System.exit(0);
}
JFrame.setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
GroundFrame gtw = new GroundFrame();
gtw .setVisible(true);
}
});
}
}
+ 1到mKorbel和Denis Tulskiy的答案。
我做了一些可能有帮助的抽象解决方案。它支持从 JFrame
(NORTH,EAST)的所有四个边调整 JFrame
的大小(增加和减少高度和宽度)当鼠标移动到其中一个角落时,它也可以同时按宽度和高度重新调整大小。
基本上我所做的是:
- 添加
MouseMotionListener
和MouseListener
到JFrame
- 覆盖
mouseDragged(..)
,mouseMoved(..)
,mousePressed(..)
和mouseReleased(..)
监听器
s。 - 设置
JFrame
不可调整大小。 - 在
mouseMoved(..)
从底部或右边听鼠标 10px 或更少侧。JFrame
,然后设置调整大小的适当方向(高度或宽度),更改鼠标光标
(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR)
用于最右边/宽度调整,Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR)
用于底部/ hieght调整大小,Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR)
用于调整帧高度大小或Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR)
对于左侧宽度调整大小)并调用canResize(true)
。 - 在
mouseDragged(.. )
在拖动方向时更新新尺寸的宽度或高度 - 在
mouseReleased(..)
将JFrame
设置为新大小。 - 在
mousePressed(..)
我们检查用户按下的坐标(这允许我们查看帧大小是否正在减少/增加)。
退房这个例子我做了:
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
公共类JFrameSizeAfterDrag扩展JFrame {
//方向保持拖动位置
private int w = 0,h = 0,direction,startX = 0, startY = 0;
public JFrameSizeAfterDrag(){
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
addMouseListener(new MouseAdapter(){
//所以我们可以看到用户是否从哪里开始增加或减少大小
@Override
public void mousePressed (MouseEvent me){
super.mouseClicked(me);
startX = me.getX();
startY = me.getY();
System.out.println( 点击:+ startX +,+ startY;
}
//当鼠标重新设定时设置大小
@Override
public void mouseReleased( MouseEvent me){
super.mouseReleased(me);
System.out.println(Mouse released);
if(direction == 1 || direction == 2 || direction == 5 || direction == 6){
setSize(w,h);
} else {//这应该移动x和y与鼠标移动一样多,然后使用setBounds(x, y,w,h);
se tBounds(getX() - (startX - me.getX()),getY() - (startY - me.getY()),w,h);
}
validate();
}
});
addMouseMotionListener(new MouseAdapter(){
private boolean canResize;
//拖动检查方向拖动
@Override
public void mouseDragged(MouseEvent me){
super.mouseDragged(me);
System.out.println(Dragging:+ me.getX()+,+ me.getY());
if(canResize&& direction == 1){//帧高变化
if(startY> me.getY()){//减少高度
h - = 4 ;
}否则{//增加高度
h + = 4;
}
}否则if(canResize&& direction == 2){//帧宽变化
if(startX> me.getX()){//减小宽度
w - = 4;
}否则{//增加宽度
w + = 4;
}
}否则if(canResize&&方向== 3){//框架高度变化
if(startX> me.getX()){//减小宽度
w + = 4;
} else {//增加宽度
w - = 4;
}
}否则if(canResize&& direction == 4){//帧宽更改
if(startY> me.getY()){//降低高度
h + = 4;
} else {//增加高度
h - = 4;
}
}否则如果(canResize&& direction == 5){//框架宽度和高度更改在右下方
if(startY> me.getY()&& ; startX> me.getX()){//增加高度和宽度
h - = 4;
w - = 4;
} else {//身高减少,
h + = 4;
w + = 4;
}
} / * Windows通常不支持从顶部进行reszing但是如果你想要:)在mouseMoved(..)中取消注释代码
else if(canResize&& direction == 6 ){//框架宽度和高度更改左上方
if(startY> me.getY()&& startX> me.getX()){//减少高度并使用
h + = 4;
w + = 4;
} else {//增加高度和宽度
h - = 4;
w - = 4;
}
}否则if(canResize&& direction == 8){//框架宽度和高度更改右上角
if(startY> me.getY()&& ; startX> me.getX()){//增加高度和宽度
h - = 4;
w - = 4;
} else {//身高减少,
h + = 4;
w + = 4;
}
}
* / else if(canResize&& direction == 7){//框架宽度和高度更改左下角
if(startY> me。 getY()&& startX> me.getX()){//增加高度和宽度
h - = 4;
w - = 4;
} else {//身高减少,
h + = 4;
w + = 4;
}
}
}
@Override
public void mouseMoved(MouseEvent me){
super.mouseMoved(me);
if(me.getY()> = getHeight() - 10&& me.getX()> = getWidth() - 10){//靠近框架的底部和右侧显示南东方光标并允许高度随着增加/减少
//System.out.println(允许大小允许......);
canResize = true;
setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
方向= 5;
} / * Windows通常不支持从顶部进行reszing但是如果你想要:)在mouseDragged(..)中取消注释代码
else if(me.getY()< = 28&& me .getX()< = 28){//靠近框架的顶部和左侧显示西北光标,只允许同时增加/减少宽度和高度
//System.out.println(\"resize允许..);
canResize = true;
setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
方向= 6;
}否则if(me.getY()< = 28&& me.getX()> = getWidth() - 10){//靠近框架的顶部和右侧显示东北光标并且只允许同时增加/减少宽度和高度
//System.out.println(\"resize允许..);
canResize = true;
setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
方向= 8;
}
* / else if(me.getY()> = getHeight() - 10&& me.getX()< = 10){//靠近底部和左侧框架的一侧显示西北光标,只允许同时增加/减少宽度和高度
//System.out.println(允许大小允许......);
canResize = true;
setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
方向= 7;
}否则如果(me.getY()> = getHeight() - 10){//接近框架底部显示南大小光标并且只允许增加高度
//System.out.println (允许调整大小);
canResize = true;
setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
direction = 1;
}否则if(me.getX()> = getWidth() - 10){//靠近框架的右侧显示东方光标,只允许增加宽度
//System.out.println (允许调整大小);
canResize = true;
setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
方向= 2;
}否则if(me.getX()< = 10){//靠近框架的左侧显示东方光标并且只允许增加宽度
//System.out.println(允许大小) );
canResize = true;
setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
方向= 3;
}否则if(me.getY()< = 28){//靠近框架的顶部显示东方光标并且只允许增加高度
// System.out.println(允许调整大小..);
canResize = true;
setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
方向= 4;
} else {
canResize = false;
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
// System.out.println(不允许调整大小);
}
}
});
//只是因为GUI是可见的而且不小
add(新JPanel(){
@Override
public Dimension getPreferredSize(){
return new Dimension(200,200);
}
});
pack();
setVisible(true);
}
@Override
public void setVisible(boolean bln){
super.setVisible(bln);
w = getWidth();
h = getHeight();
}
public static void main(String [] args){
/ **
*在Event-Dispatch-Thread上创建GUI和组件
* /
javax.swing.SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
new JFrameSizeAfterDrag();
}
});
}
}
更新:
你做的很好的例子我通过添加 MouseAdapter
修改了代码,它覆盖了 mouseReleased(..)
当 mouseReleased(..)
在此处查看固定代码(还修复了一些小问题,如添加 ComponentAdapter
而不是 ComponentListener
和 AbstractAction
而不是行动
):
import java.awt。*;
import static java.awt.GraphicsDevice.WindowTranslucency。*;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing。*;
公共类JFrameSizeAfterDrag2扩展JFrame {
私有定时器计时器;
私人JPanel panel2;
boolean canResize = true,firstTime = true;
public JFrameSizeAfterDrag2(){
super(GradientTranslucentWindow);
setBackground(new Color(0,0,0,0));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(new JPanel(null){// contentpane layout is null only
@Override
protected void paintComponent(Graphics g){
Paint p = new GradientPaint (0.0f,0.0f,new Color(0,0,0,0),0.0f,getHeight(),new Color(0,0,0,0),true);
Graphics2D g2d =(Graphics2D )g;
g2d.setPaint(p);
g2d.fillRect(0,0,getWidth(),getHeight());
}
@Override
public Dimension getPreferredSize(){
return new Dimension(300,300);
}
});
panel2 = new JPanel();
panel2.setBackground(Color.black);
getContentPane()。add(panel2);
addMouseListener(new MouseAdapter(){
@Override
public void mouseReleased(MouseEvent me){
super.mouseReleased(me);
if(b) canResize){
resizePanel(getContentPane()。getSize());
}
}
});
addComponentListener(new ComponentAdapter(){
@Override
public void componentResized(ComponentEvent e){
timer = new Timer(50,new AbstractAction) (){
@Override
public void actionPerformed(ActionEvent e){
if(timer.isRunning()){
canResize = false;
} else {
canResize = true;
if(firstTime == true){
firstTime = false;
resizePanel(getContentPane()。getSize());
}
}
}
});
timer.setRepeats(false);
timer.start();
}
}) ;
pack();
}
public void resizePanel(Dimension dim){
panel2.setBounds(0,0,dim.width,dim.height);
revalidate();
}
public static void main(String [] args){
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
boolean isPerPixelTranslucencySupported = gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT);
if(!isPerPixelTranslucencySupported){
System.out.println(不支持每像素半透明度);
System.exit(0);
}
JFrame.setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
JFrameSizeAfterDrag2 gtw = new JFrameSizeAfterDrag2();
gtw .setVisible(true);
}
});
}
}
I would like to know if theres a possibility to implement the resizing of a JFrame in that manner, that it's been resized like for example the standart windows in linux. To be more precise: If the user starts do drag, only the future size if the window will be previewed, while the original content is not resized. As soon as the user releases the mouse, the Frame resizes to that size. In images:
(1) state before resizing
(2) user starts to drage (at the red circle)
(3) user releases the mouse, the frame gets resized
Is it possible to realize that in Java Swing?
EDIT:
As this program one day should run also in lower Java RE as the 7, I tried to combine mKorbel suggestions with and the suggestion in the comment with the translucend Frame. The result is close to the goal, except that
- The content of the contentPane resizes after I stop moving the mouse, not when the mouse is released
- The frame title is resized immediately, not just whend I stop dragging the frame border.
- It works only if resized from right or bottom, otherwise the content moves with the dragging.
I think the first point is resolvable by a combination of the code and a MouseListener, something like if mouseReleased(), then resize . Here is the code, feel free to try it. For further suggestions I'm still happy about any suggestions.
The code is a slightly modification of the GradientTranslucentWindowDemo.java from the Java Tutorials. I hope it's permitted to post it here, otherwise please indicate me any violation against copyright causas. The black JPanel is supposed to be the content of the application, where as the contentPane stays invisible.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.beans.PropertyChangeListener;
import javax.swing.*;
import static java.awt.GraphicsDevice.WindowTranslucency.*;
public class GroundFrame extends JFrame {
Timer timer;
JPanel panel2;
public GroundFrame() {
super("GradientTranslucentWindow");
setBackground(new Color(0,0,0,0));
setSize(new Dimension(300,200));
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel() {
panel.setBackground(new Color(0,0,0,0));
setContentPane(panel);
setLayout(null);
panel2 = new JPanel();
panel2.setBackground(Color.black);
panel2.setBounds(0,0,getContentPane().getWidth(), getContentPane().getHeight());
getContentPane().add(panel2);
addComponentListener(new ComponentListener() {
@Override
public void componentShown(ComponentEvent e) {}
@Override
public void componentResized(ComponentEvent e) {
timer = new Timer(50, new Action() {
@Override
public void actionPerformed(ActionEvent e) {
if(timer.isRunning()){
}else{
resizePanel(getContentPane().getSize());
}
}
@Override
public void setEnabled(boolean b) {}
@Override
public void removePropertyChangeListener(PropertyChangeListener listener) {}
@Override
public void putValue(String key, Object value) {}
@Override
public boolean isEnabled() {return false;}
@Override
public Object getValue(String key) {return null;}
@Override
public void addPropertyChangeListener(PropertyChangeListener listener) {}
});
timer.setRepeats(false);
timer.start();
}
@Override
public void componentMoved(ComponentEvent e) {}
@Override
public void componentHidden(ComponentEvent e) {}
});
}
public void resizePanel(Dimension dim){
panel2.setBounds(0,0,dim.width, dim.height);
repaint();
}
public static void main(String[] args) {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
boolean isPerPixelTranslucencySupported =
gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT);
if (!isPerPixelTranslucencySupported) {
System.out.println(
"Per-pixel translucency is not supported");
System.exit(0);
}
JFrame.setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
GroundFrame gtw = new GroundFrame();
gtw.setVisible(true);
}
});
}
}
+1 to mKorbel and Denis Tulskiy's answers.
I did a sort of abstract solution which might be of help. It supports resizing (increasing and decreasing height and width) of JFrame
from all four sides of the JFrame
(NORTH, EAST, SOUTH and WEST) it also can be re-sized by width and height simultaneously when mouse is moved to one of the for corners.
Basically what I did was:
- Add
MouseMotionListener
andMouseListener
toJFrame
- Override
mouseDragged(..)
,mouseMoved(..)
,mousePressed(..)
andmouseReleased(..)
ofListener
s. - set
JFrame
to be non resizable. - In
mouseMoved(..)
listen for when mouse is 10px or less from bottom or right side. ofJFrame
, it then sets the appropriate direction of resize (height or width), changes mouseCursor
(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR)
for far right/width resizing,Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR)
for bottom/hieght resizing,Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR)
for top of frame height resizing orCursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR)
for left side width resizing) accordingly and callcanResize(true)
. - In
mouseDragged(..)
update the width or height for new size as its dragged in direction - In
mouseReleased(..)
set theJFrame
to the new size. - In
mousePressed(..)
we check for user pressed co-ordinates (this allows us to see if frame size is decreasing/increasing).
Check out this example I made:
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JFrameSizeAfterDrag extends JFrame {
//direction holds the position of drag
private int w = 0, h = 0, direction, startX = 0, startY = 0;
public JFrameSizeAfterDrag() {
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
addMouseListener(new MouseAdapter() {
//so we can see if from where the user clikced is he increasing or decraesing size
@Override
public void mousePressed(MouseEvent me) {
super.mouseClicked(me);
startX = me.getX();
startY = me.getY();
System.out.println("Clicked: " + startX + "," + startY);
}
//when the mouse is relaeased set size
@Override
public void mouseReleased(MouseEvent me) {
super.mouseReleased(me);
System.out.println("Mouse released");
if (direction == 1 || direction == 2 || direction == 5 || direction == 6) {
setSize(w, h);
} else {//this should move x and y by as much as the mouse moved then use setBounds(x,y,w,h);
setBounds(getX() - (startX - me.getX()), getY() - (startY - me.getY()), w, h);
}
validate();
}
});
addMouseMotionListener(new MouseAdapter() {
private boolean canResize;
//while dragging check direction of drag
@Override
public void mouseDragged(MouseEvent me) {
super.mouseDragged(me);
System.out.println("Dragging:" + me.getX() + "," + me.getY());
if (canResize && direction == 1) {//frame height change
if (startY > me.getY()) {//decrease in height
h -= 4;
} else {//increase in height
h += 4;
}
} else if (canResize && direction == 2) {//frame width change
if (startX > me.getX()) {//decrease in width
w -= 4;
} else {//increase in width
w += 4;
}
} else if (canResize && direction == 3) {//frame height change
if (startX > me.getX()) {//decrease in width
w += 4;
} else {//increase in width
w -= 4;
}
} else if (canResize && direction == 4) {//frame width change
if (startY > me.getY()) {//decrease in height
h += 4;
} else {//increase in height
h -= 4;
}
} else if (canResize && direction == 5) {//frame width and height change bottom right
if (startY > me.getY() && startX > me.getX()) {//increase in height and width
h -= 4;
w -= 4;
} else {//decrease in height and with
h += 4;
w += 4;
}
} /* Windows dont usually support reszing from top but if you want :) uncomment code in mouseMoved(..) also
else if (canResize && direction == 6) {//frame width and height change top left
if (startY > me.getY() && startX > me.getX()) {//decrease in height and with
h += 4;
w += 4;
} else {//increase in height and width
h -= 4;
w -= 4;
}
} else if (canResize && direction == 8) {//frame width and height change top right
if (startY > me.getY() && startX > me.getX()) {//increase in height and width
h -= 4;
w -= 4;
} else {//decrease in height and with
h += 4;
w += 4;
}
}
*/ else if (canResize && direction == 7) {//frame width and height change bottom left
if (startY > me.getY() && startX > me.getX()) {//increase in height and width
h -= 4;
w -= 4;
} else {//decrease in height and with
h += 4;
w += 4;
}
}
}
@Override
public void mouseMoved(MouseEvent me) {
super.mouseMoved(me);
if (me.getY() >= getHeight() - 10 && me.getX() >= getWidth() - 10) {//close to bottom and right side of frame show south east cursor and allow height witdh simaltneous increase/decrease
//System.out.println("resize allowed..");
canResize = true;
setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
direction = 5;
} /*Windows dont usually support reszing from top but if you want :) uncomment code in mouseDragged(..) too
else if (me.getY() <= 28 && me.getX() <= 28) {//close to top side and left side of frame show north west cursor and only allow increase/decrease in width and height simultaneosly
//System.out.println("resize allowed..");
canResize = true;
setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
direction = 6;
} else if (me.getY() <= 28 && me.getX() >= getWidth() - 10) {//close to top and right side of frame show north east cursor and only allow increase/decrease in width and height simultaneosly
//System.out.println("resize allowed..");
canResize = true;
setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
direction = 8;
}
*/ else if (me.getY() >= getHeight() - 10 && me.getX() <= 10) {//close to bottom side and left side of frame show north west cursor and only allow increase/decrease in width and height simultaneosly
//System.out.println("resize allowed..");
canResize = true;
setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
direction = 7;
} else if (me.getY() >= getHeight() - 10) {//close to bottom of frame show south resize cursor and only allow increase height
//System.out.println("resize allowed");
canResize = true;
setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
direction = 1;
} else if (me.getX() >= getWidth() - 10) {//close to right side of frame show east cursor and only allow increase width
//System.out.println("resize allowed");
canResize = true;
setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
direction = 2;
} else if (me.getX() <= 10) {//close to left side of frame show east cursor and only allow increase width
//System.out.println("resize allowed");
canResize = true;
setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
direction = 3;
} else if (me.getY() <= 28) {//close to top side of frame show east cursor and only allow increase height
// System.out.println("resize allowed..");
canResize = true;
setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
direction = 4;
} else {
canResize = false;
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
// System.out.println("resize not allowed");
}
}
});
//just so GUI is visible and not small
add(new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
});
pack();
setVisible(true);
}
@Override
public void setVisible(boolean bln) {
super.setVisible(bln);
w = getWidth();
h = getHeight();
}
public static void main(String[] args) {
/**
* Create GUI and components on Event-Dispatch-Thread
*/
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JFrameSizeAfterDrag();
}
});
}
}
UPDATE:
Great example you made I fixed the code by adding MouseAdapter
which overrides mouseReleased(..)
which calls resizePanel(...)
when mouseReleased(..)
see here for the fixed code (also fixed a few minor things like added ComponentAdapter
instead of ComponentListener
and AbstractAction
instead of Action
):
import java.awt.*;
import static java.awt.GraphicsDevice.WindowTranslucency.*;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class JFrameSizeAfterDrag2 extends JFrame {
private Timer timer;
private JPanel panel2;
boolean canResize = true,firstTime = true;
public JFrameSizeAfterDrag2() {
super("GradientTranslucentWindow");
setBackground(new Color(0, 0, 0, 0));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(new JPanel(null) {//contentpane layout is null only
@Override
protected void paintComponent(Graphics g) {
Paint p = new GradientPaint(0.0f, 0.0f, new Color(0, 0, 0, 0), 0.0f, getHeight(), new Color(0, 0, 0, 0), true);
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(p);
g2d.fillRect(0, 0, getWidth(), getHeight());
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
});
panel2 = new JPanel();
panel2.setBackground(Color.black);
getContentPane().add(panel2);
addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent me) {
super.mouseReleased(me);
if (canResize) {
resizePanel(getContentPane().getSize());
}
}
});
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
timer = new Timer(50, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if (timer.isRunning()) {
canResize = false;
} else {
canResize = true;
if (firstTime == true) {
firstTime = false;
resizePanel(getContentPane().getSize());
}
}
}
});
timer.setRepeats(false);
timer.start();
}
});
pack();
}
public void resizePanel(Dimension dim) {
panel2.setBounds(0, 0, dim.width, dim.height);
revalidate();
}
public static void main(String[] args) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
boolean isPerPixelTranslucencySupported = gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT);
if (!isPerPixelTranslucencySupported) {
System.out.println("Per-pixel translucency is not supported");
System.exit(0);
}
JFrame.setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrameSizeAfterDrag2 gtw = new JFrameSizeAfterDrag2();
gtw.setVisible(true);
}
});
}
}
这篇关于Swing:在Linux中调整像Frames这样的JFrame的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!