Java JFrame.setExtendedState 实际上并没有最大化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18969085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JFrame.setExtendedState doesn't actually maximise
提问by Shaun Wild
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.
我已经使用此代码来最大化 JFrame,但实际上并没有最大化框架,它只是将窗口大小设置为屏幕的大小,而不实际更改状态,因此单击最大化按钮实际上不会再次缩小它。
Am I using the wrong command or something?
我使用了错误的命令还是什么?
回答by WeaponsGrade
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:
frame.setExtendedState(JFrame.MAXIMIZED_BOTH)
适用于 Linux x64。这是我测试的程序:
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);
}
}
回答by Martijn Courteaux
Have you tried this?
你试过这个吗?
f.setExtendedState(f.getExtendedState() | JFrame.MAXIMIZED_BOTH);
回答by splungebob
It works for me running Java 7 on a WinXP machine.
它适用于我在 WinXP 机器上运行 Java 7。
For the record, this is what an SSCCE should look like:
作为记录,这就是 SSCCE 的样子:
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);
}
});
}
}
回答by MadProgrammer
Based on your provided example and run on Windows 7...
根据您提供的示例并在 Windows 7 上运行...
"Maximised" state (this is cropped version of window as the original is quite large)
“最大化”状态(这是窗口的裁剪版本,因为原始窗口很大)
"Normal" state
“正常”状态
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);
}
});
}
}
回答by Ajith Moni
This worked for me:
这对我有用:
We need to combine the setSize () and setExtendedState together JFrame frame=new JFrame();
我们需要将 setSize() 和 setExtendedState 组合在一起 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());
回答by Jesus Flores
You have an error in frame.setExtendedState(JFrame.MAXIMISED_BOTH);
你有一个错误 frame.setExtendedState(JFrame.MAXIMISED_BOTH);
You should write frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
instead
你应该写frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
,而不是
回答by Dharmendrasinh Chudasama
You should use this when applying changes
您应该在应用更改时使用它
frame.setResizable(true);