Java如何使JFrames最大化但不能调整大小 [英] Java how to make JFrames maximised but not resizable

查看:119
本文介绍了Java如何使JFrames最大化但不能调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初(请参阅我之前的问题" Java如何使JFrames作为一个最大化的窗口开始),我想制作一个最大化的窗口. 这段代码完成了这一点:

Originally (See my previous question "Java how to make JFrames start off as a maximised window") I wanted to make a window which starts out maximised. This code accomplishes this:

public static void main(String[] args)  {

JFrame frame = new JFrame();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);

}

但是,如果将该窗口向下还原,它将变成几乎不存在的栏.为了解决这个问题,我使用setSize()为窗口设置了一个大小.可以,但是又出现了一个问题,仍然可以调整窗口的大小.

However, if this window is restored down it becomes a practically non-existent bar. To solve this I set a size for the window using setSize(). This works but presents another problem, the window can still be resized.

为解决此问题,我设置了setResizable(false);.这是到目前为止的代码:

To solve this problem I set setResizable(false); and this is my code so far:

public static void main(String[] args) {

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    JFrame frame = new JFrame("Jedia");
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setSize(screenSize);
    frame.setResizable(false);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

但是,现在窗口以指定的大小(而不是最大化)开始,并且无法恢复.

However, now the window starts out at its specified size (rather than maximised) and cannot be restored up.

所以,我的问题是,如何使窗口最大程度地启动,为窗口还原时提供一个大小,并使其无法调整大小?还是让一个窗口最大化而无法还原?

So, my question is, how can I either make the window start out maximised, give it a size for when it is restored down and make resizing it impossible? Or make a window that starts out maximised and cannot be restored down?

推荐答案

有一个几乎始终有效的简单修复程序:在设置为可见后, 不能调整框架的大小.因此,只能以这种方式修改您的代码:

There is a simple fix that works almost all the time: make your frame not resizable after having set visible. So only modifies your code this way:

public static void main(String[] args) {

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    JFrame frame = new JFrame("Jedia");
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setSize(screenSize);
    frame.setVisible(true);    // FIRST visible = true
    frame.setResizable(false); // THEN  resizable = false
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

这样,框架将开始最大化,并且最大化按钮将变灰,从而阻止用户使用它. (我真的不知道为什么要这样做.我认为最大化状态仅在窗口可见时才真正应用,并且如果您之前使它不可调整大小,那么它将不适用.)

This way, the frame will start maximized and the maximize button will be greyed out, preventing user to use it. (I don't really know why you have to do this. I suppose the maximized state is really applied only when the window becomes visible, and if you make it unresizable before, it will not apply.)

它几乎一直都能正常工作,因为至少在Windows 7上,您可以通过单击标题栏并将其拖动来使窗口脱离最大化状态.但这将是您之前设置的大小.问题在于您的用户将无法再次最大化它,并且我还没有找到使用侦听器将窗口恢复到最大化状态的方法. (编辑:@David Kroukamp在回答的最后部分显示,可以通过使用ComponentListener强制最大化状态.因此,您不必使用setResizable(false),这样您仍然Windows 7出现问题,因为无论出于何种原因该事件都无法捕获拖动动作,但是用户将能够使用最大化按钮将其放回原处.)

It works almost all the time because on Windows 7 at least you can make the window goes out of the maximized state by clicking the title bar and dragging it. But it will be at the size you have set it earlier. Problem is that your user will not be able to maximize it again, and I haven't found the way with listeners to make the window back to maximized state. ( Edit: @David Kroukamp shows in the last part of his answer that it is possible to force the maximized state by using a ComponentListener. Therefore you don't have to use setResizable(false) This way you still have a problem with Windows 7 because the dragging action is not catched by this event for whatever reason but users will be able to use the maximized button to put it back where it should be.)

现在,几乎从来没有理由做这种事情.用户真的不喜欢您阻止他们操作其窗口(例如,无法移动最大化的窗口,而当您有多个屏幕时,这可能会很烦人).一个例外是如果您要制作游戏,通常是全屏游戏.但是然后您将不需要JFrame,因为您不需要所有装饰,而是需要Window.

Now, there is almost never a reason to do this kind of things. Users don't really like when you prevent them to manipulate their windows (maximized windows can not be moved, for example, and that can be annoying when you have multiple screens). An exception is if you are making a game, which is typically full-screen. But then you wouldn't want a JFrame because you don't want all the decoration, but a Window.

如果您的问题是默认窗口大小非常小,那是正常的.您必须先使用布局(这很重要)在框架中放置一些内容(一些控件,按钮,您在应用程序中需要的内容),然后在框架上调用方法pack().它将为您的窗口选择一个不错的默认大小.

If your problem is that the default window size is very small, it's normal. You have to put something in your frame first (some controls, buttons, what you want in your application), using layouts (that's important) then call the method pack() on your frame. It will chose a nice default size for your window.

最后,是最后一个字.我已经将示例代码作为快捷方式放在了main方法中,但是您应该始终使用SwingUtils.invokeLater()在Swing EDT中执行Swing的工作.

Finally, a last word. I've put my example code in a main method as a shortcut, but you should always do Swing stuff in the Swing EDT by using SwingUtils.invokeLater().

这篇关于Java如何使JFrames最大化但不能调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆