代码之家  ›  专栏  ›  技术社区  ›  user153498

JFrame的Java锁长宽比

  •  2
  • user153498  · 技术社区  · 16 年前

    我知道 ComponentListener componentResized ,但我很难不让它进入一个无限循环的调整大小,因为我在调整大小侦听器中调整窗口的大小。

    1 回复  |  直到 16 年前
        1
  •  2
  •   David Sykes    16 年前

    我对你的问题没有具体的答案,但我经常发现,要避免你所说的“无限循环的调整大小”的方法是推迟你的电话来调整窗口大小:

    boolean correctAspectRatio = ...; // check to see if new size meets aspect ratio requirements
    
    if (!correctAspectRatio) {
      final Frame _frame = this;
      final int _width = ...; // calculated width
      final int _height = ...; // calculated height
    
      SwingUtilities.invokeLater(new Runnable(){
        public void run() {
          _frame.setSize(_width, _height);
        }
      });
    }