【问题标题】:Get Normal JFrame Bounds while Maximized最大化时获取正常的 JFrame 边界
【发布时间】:2013-01-14 18:51:09
【问题描述】:

我正在尝试创建一个 JFrame 来记住它的窗口位置、大小以及它是否被最大化。为 windowClosing 事件使用 WindowListener 并将其边界保存到 Preferences 应该足够简单。

为了使其正常工作(如下所示),我需要在 JFrame 最大化时(MAXIMIZED_BOTH ExtendedState)提取 JFrame 的正常(NORMAL ExtendedState)边界。这是可能吗?考虑到正常边界存储在某处以便它恢复。

//...
addWindowListener(new WindowListener() {
    ///...
    @Override
    public void windowClosing(WindowEvent e) {
        prefs.putBoolean("win_max",win_max);
        if(winmax)
        {
            //win_x=?
            //win_y=?
            //win_w=?
            //win_h=?
        }
        else
        {
            win_x=getX();
            win_y=getY();
            win_w=getWidth();
            win_h=getHeight();
        }
        prefs.putInt("win_x",win_x);
        prefs.putInt("win_y",win_y);
        prefs.putInt("win_w",win_w);
        prefs.putInt("win_h",win_h);
    }
});
//...

如果这是一个非常简单的问题,我深表歉意。任何帮助表示赞赏。

【问题讨论】:

  • 只需保存和恢复extendedStatebounds 属性(使用它们各自的getter/setter)

标签: java swing window persistence jframe


【解决方案1】:

这就是我所做的。模型实例保存了框架原点、框架边界和框架状态。

protected int           state;

protected Point         frameOrigin;

protected Rectangle     frameBounds;

这是我创建的一个组件侦听器,用于捕获帧数据更改。

    frame.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentMoved(ComponentEvent event) {
            if (isMaximized()) {
                model.setFrameOrigin(frame.getLocation());
            } else {
                model.setFrameBounds(frame.getBounds());
            }
            model.setFrameState(frame.getExtendedState());
        }
        @Override
        public void componentResized(ComponentEvent event) {
            model.setFrameState(frame.getExtendedState());
        }
    });

这是我在打包框架以将框架设置为上次使用状态后必须执行的代码。

    frame.pack();
    if (options.getFrameBounds().getWidth() > 0) {
        frame.setExtendedState(options.getState());
        if (isMaximized()) {
            frame.setLocation(options.getFrameOrigin());
        } else {
            frame.setBounds(options.getFrameBounds());
        }
    }
    model.setFrameState(frame.getExtendedState());
    model.setFrameBounds(frame.getBounds());
    model.setFrameOrigin(frame.getLocation());

这是isMaximized 方法。

public boolean isMaximized() {
    return (frame.getExtendedState() & JFrame.MAXIMIZED_BOTH) 
            == JFrame.MAXIMIZED_BOTH;
}

我将帧数据保存在属性文件中,但您可以按需要保存和恢复帧数据。

【讨论】:

    【解决方案2】:

    在派生自JFrame 的类中,添加以下代码...

    private Rectangle m_normalBounds;
    
    @Override
    public void setExtendedState(int state)
    {
       if (getExtendedState() == 0)
          m_bounds = getBounds();        // Capture the normal bounds since the state is changing
    
       super.setExtendedState(state);
    }
    
    private Rectangle getNormalBounds()  // Always returns the normal bounds
    {
       Rectangle bounds;
    
       if (getExtendedState() == 0)
          return(getBounds());
    
       return(m_normalBounds);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多