JFrame.setExtendedState doesn't actually maxim

2019-06-08 07:38发布

public static void main(String args[]){
    JFrame frame = new JFrame();
    frame.setExtendedState(JFrame.MAXIMISED_BOTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

I've used this code to maximise a JFrame, but instead of actually maximising the frame, it just sets the window size to that of the screen, without actually changing the state, so clicking the maximize button doesn't actually downscale it again.

Am I using the wrong command or something?

7条回答
\"骚年 ilove
2楼-- · 2019-06-08 08:10

It works for me running Java 7 on a WinXP machine.

For the record, this is what an SSCCE should look like:

import javax.swing.*;

public class JFrameExtendedDemo
{
  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(300, 200);
        f.setLocationRelativeTo(null);
        f.setVisible(true);

        // Then:
        f.setExtendedState(JFrame.MAXIMIZED_BOTH);
      }
    });
  }
}
查看更多
在下西门庆
3楼-- · 2019-06-08 08:15

You must want it maximized by default. Because the maximize button works out-of-the-box.

frame.setExtendedState(JFrame.MAXIMIZED_BOTH) works on Linux x64. Here's the program I tested with:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test implements ActionListener {

  public static void main(String... args) {
    new Test();
  }

  private JFrame frame;

  public Test() {
    frame = new JFrame();
    frame.add(new JLabel("Hi!"), BorderLayout.CENTER);
    JButton button = new JButton("maximize");
    button.addActionListener(this);
    frame.add(button, BorderLayout.SOUTH);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }


  @Override
  public void actionPerformed(ActionEvent e) {
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
  }
}
查看更多
狗以群分
4楼-- · 2019-06-08 08:21

Based on your provided example and run on Windows 7...

"Maximised" state (this is cropped version of window as the original is quite large)

enter image description here

"Normal" state

enter image description here

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ExtendedFrame {

    public static void main(String[] args) {
        new ExtendedFrame();
    }

    public ExtendedFrame() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
//                frame.setExtendedState(JFrame.MAXIMISED_BOTH);
                frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}
查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-06-08 08:26

This worked for me:

We need to combine the setSize () and setExtendedState together JFrame frame=new JFrame();

frame.setExtendedState(JFrame.MAXIMIZED_BOTH); // aligns itself with windows task bar
// set maximum screen   
frame.setSize((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(), (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight());
查看更多
对你真心纯属浪费
6楼-- · 2019-06-08 08:27

You should use this when applying changes

frame.setResizable(true);
查看更多
啃猪蹄的小仙女
7楼-- · 2019-06-08 08:30

You have an error in frame.setExtendedState(JFrame.MAXIMISED_BOTH);

You should write frame.setExtendedState(JFrame.MAXIMIZED_BOTH); instead

查看更多
登录 后发表回答