Java JFrame 最大化窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/479523/
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 Maximize window
提问by Nope
I'm putting together a quick and dirty animation using swing. I would like the window to be maximized. How can I do that?
我正在使用 Swing 制作一个快速而肮脏的动画。我希望窗口最大化。我怎样才能做到这一点?
采纳答案by kgiannakakis
Provided that you are extending JFrame:
假设您要扩展 JFrame:
public void run() {
MyFrame myFrame = new MyFrame();
myFrame.setVisible(true);
myFrame.setExtendedState(myFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
}
回答by user54579
If you're using a JFrame, try this
如果你使用的是 JFrame,试试这个
JFrame frame = new JFrame();
//...
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
回答by Zach Scrivena
回答by VonC
Something like this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);
就像是 this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame
{
public Test()
{
GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
this.setMaximizedBounds(env.getMaximumWindowBounds());
this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);
}
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
Test t = new Test();
t.setVisible(true);
}
}
回答by Greg Reynolds
The way to set JFrame to full-screen, is to set MAXIMIZED_BOTH
option which stands for MAXIMIZED_VERT | MAXIMIZED_HORIZ
, which respectively set the frame to maximize vertically and horizontally
设置JFrame全屏的方法,是设置MAXIMIZED_BOTH
option代表MAXIMIZED_VERT | MAXIMIZED_HORIZ
,分别设置frame垂直和水平最大化
package Example;
import java.awt.GraphicsConfiguration;
import javax.swing.JFrame;
import javax.swing.JButton;
public class JFrameExample
{
static JFrame frame;
static GraphicsConfiguration gc;
public static void main(String[] args)
{
frame = new JFrame(gc);
frame.setTitle("Full Screen Example");
frame.setExtendedState(MAXIMIZED_BOTH);
JButton button = new JButton("exit");
b.addActionListener(new ActionListener(){@Override
public void actionPerformed(ActionEvent arg0){
JFrameExample.frame.dispose();
System.exit(0);
}});
frame.add(button);
frame.setVisible(true);
}
}
回答by frame_max_show
i like this version:
我喜欢这个版本:
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class Test
{
public static void main(String [] args)
{
final JFrame frame = new JFrame();
final GraphicsConfiguration config = frame.getGraphicsConfiguration();
final int left = Toolkit.getDefaultToolkit().getScreenInsets(config).left;
final int right = Toolkit.getDefaultToolkit().getScreenInsets(config).right;
final int top = Toolkit.getDefaultToolkit().getScreenInsets(config).top;
final int bottom = Toolkit.getDefaultToolkit().getScreenInsets(config).bottom;
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
final int width = screenSize.width - left - right;
final int height = screenSize.height - top - bottom;
frame.setResizable(false);
frame.setSize(width,height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
回答by Wouter
I ended up using this code:
我最终使用了这段代码:
public void setMaximized(boolean maximized){
if(maximized){
DisplayMode mode = this.getGraphicsConfiguration().getDevice().getDisplayMode();
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(this.getGraphicsConfiguration());
this.setMaximizedBounds(new Rectangle(
mode.getWidth() - insets.right - insets.left,
mode.getHeight() - insets.top - insets.bottom
));
this.setExtendedState(this.getExtendedState() | JFrame.MAXIMIZED_BOTH);
}else{
this.setExtendedState(JFrame.NORMAL);
}
}
This options worked the best of all the options, including multiple monitor support. The only flaw this has is that the taskbar offset is used on all monitors is some configurations.
此选项在所有选项中效果最好,包括多显示器支持。唯一的缺陷是在所有显示器上使用任务栏偏移量是一些配置。
回答by TT.
I've tried the solutions in this thread and the ones here, but simply calling setExtendedState(getExtendedState()|Frame.MAXIMIZED_BOTH);
right after calling setVisible(true);
apparently does not work for my environment (Windows 10, JDK 1.8, my taskbar is on the right side of my screen). Doing it this way still leaves a tiny space on the left, right and bottom .
我已经尝试了这个线程和这里的解决方案,但只是在调用setExtendedState(getExtendedState()|Frame.MAXIMIZED_BOTH);
后立即调用setVisible(true);
显然不适用于我的环境(Windows 10,JDK 1.8,我的任务栏在我的屏幕右侧)。这样做仍然会在左侧、右侧和底部留下一个很小的空间。
What did work for me however, is calling setExtendedState(...
when the window is activated, like so:
然而,对我有用的是setExtendedState(...
在窗口激活时调用,如下所示:
public class SomeFrame extends JFrame {
public SomeFrame() {
// ...
setVisible(true);
setResizable(true);
// if you are calling setSize() for fallback size, do that here
addWindowListener (
new WindowAdapter() {
private boolean shown = false;
@Override
public void windowActivated(WindowEvent we) {
if(shown) return;
shown = true;
setExtendedState(getExtendedState()|JFrame.MAXIMIZED_BOTH);
}
}
);
}
}