如何手动调整JFrame的大小



我试图通过拖动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;

相关内容

  • 没有找到相关文章

最新更新