简体   繁体   中英

JFrame size, resize, fullscreen

My question have 3 parts.

  1. I would like to know, how can I set JFrame window to be resizable, but can not go under a minim size so the user cant make it smaller then the minimum size.
  2. I would like to know, how can I set JFrame window to be resizable, but still keep the aspect ratio (16:9) even if the user is resizing it.
  3. I would like to know, how can I make the program become really full screen after he clicks a button so there are no borders and runs like a game or something (and if there is any specific issues how can I revert it back safely).

Thanks for your help and patient for my not perfect English and not perfect Java knowledge.

The provided answer is useful and answers part of my question, but still most of it still blank for me.

The part of the code:

private void createDisplay() {
    GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();

    frame = new JFrame(title);                                              //setting the title of the window
    frame.setSize(width, height);                                           //setting the width and height of the window
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                   //make sure program closes down properly
    frame.setResizable(true);
    frame.setLocationRelativeTo(null);                                      //the window will apparel on the center
    frame.setVisible(true);
    frame.setIconImage(Assets.ImageMap.get("icon"));

    canvas = new Canvas();
    canvas.setPreferredSize(new Dimension(width, height));
    canvas.setMaximumSize(new Dimension(width, height));
    canvas.setMinimumSize(new Dimension(gd.getDisplayMode().getWidth() / 2, gd.getDisplayMode().getHeight() / 2));
    canvas.setFocusable(false);

    frame.add(canvas);
    frame.pack();                                                           //resize the window a little bit so able to see all of canvas
}
  1. Set the frame's minimum size (setMinimumSize)
  2. Is more difficult, as you are notified of the size change after the fact, so you'll always be fighting the system as it changes (and then you run into problems with who generated the size change event). It might be better to focus on keeping the ratio of the contents of the frame based on the available space within the frame (VLC is a good example of this)
  3. Full screen exclusive mode

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM