Forcing JFrame to not resize after setResizable(false). Command wont work

江枫思渺然 提交于 2019-12-02 07:47:54

Use Java's Robot class to force a mouse release. I've modified your example code below:

public static void main(String[] args) {        
  JFrame testFrame = new JFrame();
  testFrame.setResizable(true);
  testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  Timer testTimer = new Timer(6000, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
            testFrame.setResizable(false);
      Robot r;
      try {
        r = new Robot();
        r.mouseRelease( InputEvent.BUTTON1_DOWN_MASK);
      } catch (AWTException ex) {
        ex.printStackTrace();
      }
    }

  });
  testFrame.setVisible(true);
  testTimer.start();
}

Well one way I could think of is setting the size back after a resizing event if the frame is not resizable.

Not sure how well it would work though.

frame.addComponentListener(new ComponentAdapter() {
    @Override
    public void componentResized(ComponentEvent e) {
        if (!frame.isResizable()) {
            frame.setSize(...);
        }
    }
});

Have you tried revalidating the JFrame immediately after the power up ends? This will most likely solve your issue (though I wouldn't know given that you have neither an SSCCE, or an MVCE). I hope this helps, and best of luck.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!