如何确定 JFrame 被移动的原因(Windows、JDK6)

发布于 2024-12-10 23:46:57 字数 1638 浏览 0 评论 0原文

import java.awt.Dimension;
import java.awt.event.ComponentListener;
import java.awt.event.ComponentEvent;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;

public class FrmMain extends JFrame implements ComponentListener
{

FrmMain()
{
    super("Moves & Resize");
    setMinimumSize(new Dimension(512,384));
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    addComponentListener(this);
    setVisible(true);
}

public void componentMoved( ComponentEvent ev )
{
    int state = getExtendedState();
    if( (state&JFrame.MAXIMIZED_BOTH)==JFrame.MAXIMIZED_BOTH )
    {
        System.out.println("Moved, maximized");
    } else {
        System.out.println("Moved, not maximized");
    }
}
public void componentResized( ComponentEvent ev )
{
    int state = getExtendedState();
    if( (state&JFrame.MAXIMIZED_BOTH)==JFrame.MAXIMIZED_BOTH )
    {
        System.out.println("Resized, maximized");
    } else {
        System.out.println("Resized, not maximized");
    }
}
public void componentShown( ComponentEvent ev )
{
}
public void componentHidden( ComponentEvent ev )
{
}

public static void main( String[] args )
{
    SwingUtilities.invokeLater(
        new Runnable() {
            public void run()
            {
                FrmMain mainwin = new FrmMain();
            }
        }
    );
}

}

我想确定框架是否由于最大化而被调整大小/移动,或者只是被用户拖动。令人惊讶的是,上面的代码输出(在我按下框架上的“最大化”按钮后):

Moved, not maximized
Resized, maximized

这意味着,窗口状态的更新发生在调用 componentMoved 之后和调用 componentResized 之前。那么,我如何知道在 componentMoved 事件中窗口是否被最大化?

import java.awt.Dimension;
import java.awt.event.ComponentListener;
import java.awt.event.ComponentEvent;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;

public class FrmMain extends JFrame implements ComponentListener
{

FrmMain()
{
    super("Moves & Resize");
    setMinimumSize(new Dimension(512,384));
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    addComponentListener(this);
    setVisible(true);
}

public void componentMoved( ComponentEvent ev )
{
    int state = getExtendedState();
    if( (state&JFrame.MAXIMIZED_BOTH)==JFrame.MAXIMIZED_BOTH )
    {
        System.out.println("Moved, maximized");
    } else {
        System.out.println("Moved, not maximized");
    }
}
public void componentResized( ComponentEvent ev )
{
    int state = getExtendedState();
    if( (state&JFrame.MAXIMIZED_BOTH)==JFrame.MAXIMIZED_BOTH )
    {
        System.out.println("Resized, maximized");
    } else {
        System.out.println("Resized, not maximized");
    }
}
public void componentShown( ComponentEvent ev )
{
}
public void componentHidden( ComponentEvent ev )
{
}

public static void main( String[] args )
{
    SwingUtilities.invokeLater(
        new Runnable() {
            public void run()
            {
                FrmMain mainwin = new FrmMain();
            }
        }
    );
}

}

I want to determine whether the frame was resized/moved due to being maximized, or simply dragged around by user. Surprisingly, the code above outputs (after I press "maximize" button on the frame):

Moved, not maximized
Resized, maximized

That means, the update of window state happens after calling componentMoved and before calling componentResized. So, how do I find out, if the window is being maximized, inside componentMoved event?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独自←快乐 2024-12-17 23:46:57

使用:

(state&JFrame.MAXIMIZED_BOTH) != 0

如MAXIMIZED_BOTH是水平和垂直最大化的两位。
在 JSE 7 下,我确实得到了“感动、最大化”。

我不会篡改移动和调整大小事件的顺序。尝试重新实现关闭框也是一个坏主意。

您可以在“移动”上发布您自己的事件,该事件将在调整大小的事件之后发布。

Use:

(state&JFrame.MAXIMIZED_BOTH) != 0

as MAXIMIZED_BOTH is two bits of horizontally and vertically maximized.
Under JSE 7 I do get "moved, maximized."

I would not tamper with the order of moved and resized event. Trying to reimplement the close box is a bad idea too.

You could post your own event on moved, which then would come after the resized event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文