gpt4 book ai didi

java - 如何手动调整 JFrame 的大小?

转载 作者:太空宇宙 更新时间:2023-11-04 13:50:21 25 4
gpt4 key购买 nike

我正在尝试通过拖动 JPanel 来手动调整 JFrame 的大小。这是我所做的。目前可以了,但是屏幕仍然闪烁,并且调整大小不正确

我希望它发生的是,当我单击 Jpanel 并拖动它时,JFrame 应自动调整大小到 X 坐标和 Y 坐标所在的位置。

     public void mouseDragged(MouseEvent e) {

// Sets width, height and moved X and Y coordinates
int currentwidth = this.getWidth();
int currentheight = this.getHeight();
int newwidth = 0;
int newheight = 0 ;

int thisX = this.getX();
int thisY = this.getY();

// Calculates the moving distance
int xMoved = (thisX + e.getX()) - (thisX + initialClick.x);
int yMoved = (thisY + e.getY()) - (thisY + initialClick.y);



// Checks which component the mouse is clicked on
if(e.getComponent() == reszingbit){

// Calculates the new height and width
newwidth = 200 + xMoved;

newheight = 200 + yMoved;

// Making sure that the new height and width is not less than actual size
if(newwidth >= 200 && newheight >= 200){
this.setSize(newwidth,newheight);
}
}

200是实际的高度和宽度。

最佳答案

信息很少,但也许您需要将大小计算更改为:

    newwidth = this.getWidth() + xMoved;

newheight = this.getHeight() + yMoved;

移动大小的计算应该如下所示:

    int xMoved =  e.getX() - initialClick.x;
int yMoved = e.getY() - initialClick.y;

关于java - 如何手动调整 JFrame 的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30365877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com