How to minimize a JFrame window from Java?

前端 未结 6 1740
谎友^
谎友^ 2020-12-03 10:10

In my Java app, I have a JFrame window, how can I minimize it from my Java program ?

相关标签:
6条回答
  • 2020-12-03 10:24

    Another approach

    frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_ICONIFIED));
    
    0 讨论(0)
  • 2020-12-03 10:30

    If you are trying to code for a event of a component then try code below. And make sure the class which this code is included is extended by Frame class

    private void closeMouseClicked(java.awt.event.MouseEvent evt){                        
        this.setState(1);
    }
    

    Or create an instance of a Frame class and call setState(1);

    0 讨论(0)
  • 2020-12-03 10:31

    minimize with frame.setState(Frame.ICONIFIED)

    restore with frame.setState(Frame.NORMAL)

    0 讨论(0)
  • 2020-12-03 10:34

    Minimize:

    frame.setState(Frame.ICONIFIED);
    

    Another way to minimize:

    frame.setExtendedState(JFrame.ICONIFIED);
    

    Normal size:

    frame.setState(Frame.NORMAL);
    

    Another way to normal size:

    frame.setExtendedState(JFrame.NORMAL);
    

    Maximize:

    frame.setState(Frame.MAXIMIZED_BOTH);
    

    Another way to maximize:

    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    

    Full Screen maximize:

    GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0];
    try { device.setFullScreenWindow((Window) frame); } finally { device.setFullScreenWindow(null); }
    

    Refer to the JFrame documentation for more information.

    0 讨论(0)
  • 2020-12-03 10:34

    You can use following code:

    this.setState(YourJFrame.ICONIFIED);
    

    And you can use this code to maximize it:

    this.setExtendedState(MAXIMIZED_BOTH);
    
    0 讨论(0)
  • 2020-12-03 10:35

    You can do this in two ways:

    JFrame frame = new JFrame("Test");
    
    frame.setExtendedState(JFrame.ICONIFIED); // One way
    frame.setState(JFrame.ICONIFIED); // Another way
    
    0 讨论(0)
提交回复
热议问题