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

前端 未结 3 1566
别跟我提以往
别跟我提以往 2021-01-26 17:21

I have a simple Atari breakout program, and long story short, one of my powerups is to allow the user to resize the window for a few seconds, then make the window non-resizable

相关标签:
3条回答
  • 2021-01-26 18:03

    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(...);
            }
        }
    });
    
    0 讨论(0)
  • 2021-01-26 18:09

    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.

    0 讨论(0)
  • 2021-01-26 18:14

    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();
    }
    
    0 讨论(0)
提交回复
热议问题