如何手动调整JFrame的大小?

时间:2015-05-21 06:15:23

标签: java swing user-interface

我正在尝试通过拖动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是实际的高度和宽度。

1 个答案:

答案 0 :(得分:0)

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

    newwidth = this.getWidth() + xMoved;

    newheight =  this.getHeight() + yMoved;

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

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