【问题标题】:How to resize a JFrame manually?如何手动调整 JFrame 的大小?
【发布时间】:2015-05-21 06:15:22
【问题描述】:

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

【问题讨论】:

  • 考虑提供一个runnable example 来证明您的问题。这不是代码转储,而是您正在做的事情的一个例子,它突出了您遇到的问题。这将导致更少的混乱和更好的响应

标签: java swing user-interface


【解决方案1】:

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

    newwidth = this.getWidth() + xMoved;

    newheight =  this.getHeight() + yMoved;

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

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

【讨论】:

  • 我将它设置为 this.get 最初的高度和宽度。但每次我拖动鼠标时,它似乎都在循环。所以我的意思是尺寸增加得非常快,并且在我停止拖动时没有停止。会不会是计算拖拽距离有问题?
  • 你通常如何计算鼠标移动的距离以增加屏幕的大小?
猜你喜欢
  • 1970-01-01
  • 2012-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-10
  • 1970-01-01
  • 2012-05-16
相关资源
最近更新 更多