这是教Java Frame关于Windows的Aero Snap功能的唯一方法吗?使用本机代码的解决方案

2022-09-01 14:48:30

如果我通过单击 Windows WindowDecoration 的最小化按钮来最小化一个 Aero-snaped 到屏幕左侧的,并通过 Alt-Tabbing 或单击它在 Windows TaskBar 中取消最小化它,则框架将正确还原到左侧。好!JFrame

但是,如果我最小化框架

setExtendedState( getExtendedState() | Frame.ICONIFIED );

并通过将鼠标悬停在Windows任务栏上来查看预览,它会将帧显示为错误的位置。通过 Alt-Tab 键或在 Windows 任务栏中单击它来取消其最小化后,帧将在此错误的位置和大小还原。帧边界是“未贴靠”的值,如果将帧从“屏幕边框”中拖离,Windows 通常会记住还原这些值。

错误的屏幕录制:

enter image description here

我的结论是,Java不了解AeroSnap,并且向Windows提供了错误的界限。(例如,返回 false。Toolkit.getDefaultToolkit().isFrameStateSupported( Frame.MAXIMIZED_VERT ) );

这是我对这个错误的修复:

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Point;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
 * Fix for the "Frame does not know the AeroSnap feature of Windows"-Bug.
 *
 * @author bobndrew 20160106
 */
public class SwingFrameStateWindowsAeroSnapBug extends JFrame
{
  Point     location = null;
  Dimension size     = null;


  public SwingFrameStateWindowsAeroSnapBug( final String title )
  {
    super( title );
    initUI();
  }

  private void initUI()
  {
    setDefaultCloseOperation( EXIT_ON_CLOSE );
    setLayout( new FlowLayout() );
    final JButton minimize = new JButton( "Minimize" );
    final JButton maximize = new JButton( "Maximize" );
    final JButton normal = new JButton( "Normal" );
    add( normal );
    add( minimize );
    add( maximize );
    pack();
    setSize( 200, 200 );


    final ActionListener listener = actionEvent ->
    {
      if ( actionEvent.getSource() == normal )
      {
        setExtendedState( Frame.NORMAL );
      }
      else if ( actionEvent.getSource() == minimize )
      {
        //Size and Location have to be saved here, before the minimizing of an AeroSnapped WindowsWindow leads to wrong values:
        location = getLocation();
        size = getSize();
        System.out.println( "saving location (before iconify) " + size + " and " + location );

        setExtendedState( getExtendedState() | Frame.ICONIFIED );//used "getExtendedState() |" to preserve the MAXIMIZED_BOTH state

        //does not fix the bug; needs a Window-Drag after DeMinimzing before the size is applied:
        //          setSize( size );
        //          setLocation( location );
      }
      else if ( actionEvent.getSource() == maximize )
      {
        setExtendedState( getExtendedState() | Frame.MAXIMIZED_BOTH );
      }
    };

    minimize.addActionListener( listener );
    maximize.addActionListener( listener );
    normal.addActionListener( listener );

    addWindowStateListener( windowEvent ->
    {
      System.out.println( "oldState=" + windowEvent.getOldState() + "  newState=" + windowEvent.getNewState() );

      if ( size != null && location != null )
      {
        if ( windowEvent.getOldState() == Frame.ICONIFIED )
        {
          System.out.println( "Fixing (possibly) wrong size and location on de-iconifying to " + size + " and " + location + "\n" );
          setSize( size );
          setLocation( location );

          //Size and Location should only be applied once. Set NULL to avoid a wrong DeMinimizing of a following Windows-Decoration-Button-Minimize!
          size = null;
          location = null;
        }
        else if ( windowEvent.getOldState() == (Frame.ICONIFIED | Frame.MAXIMIZED_BOTH) )
        {
          System.out.println( "Set size and location to NULL (old values: " + size + " and " + location + ")" );
          //Size and Location does not have to be applied, Java can handle the MAXIMIZED_BOTH state. Set NULL to avoid a wrong DeMinimizing of a following Windows-Decoration-Button-Minimize!
          size = null;
          location = null;
        }
      }

    } );
  }


  public static void main( final String[] args )
  {
    SwingUtilities.invokeLater( new Runnable()
    {
      @Override
      public void run()
      {
        new SwingFrameStateWindowsAeroSnapBug( "AeroSnap and the Frame State" ).setVisible( true );
      }
    } );
  }
}

这似乎适用于Windows7下的所有情况,但感觉太多的窗口管理混乱。出于某种原因,我避免在Linux或MacOS下测试它;-)

有没有更好的方法让AeroSnap和Java Frames一起工作?


编辑:

我在甲骨文上提交了一个错误:http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8147840


答案 1

有没有更好的方法让AeroSnap和Java Frames一起工作?

好不了多少。直接设置扩展状态会绕过操作系统设置它的处理方式。

如果你看一下源代码,你会发现它调用了 's 方法。JDK 的接口实现是类,它将其方法声明为 。所以,在Oracle对此做些什么或者你使用原生代码之前,你就不走运了(见下文)。JFrame#setExtendedStateFramePeersetStateJFrameFramePeerWFramePeersetStatenative

幸运的是,您不必对事件侦听器和缓存边界感到疯狂。隐藏并显示帧足以将大小“重置”到最小化之前的大小:

public class AeroResize extends JFrame {

    public AeroResize(final String title) {

        super(title);
        initUI();
    }

    private void initUI() {

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        final JButton minimize = new JButton("Minimize");
        final JButton maximize = new JButton("Maximize");
        final JButton normal = new JButton("Normal");
        add(normal);
        add(minimize);
        add(maximize);
        pack();

        minimize.addActionListener(e -> {
            setVisible(false);
            setExtendedState(getExtendedState() | JFrame.ICONIFIED);
            setVisible(true);
//          setLocation(getLocationOnScreen()); // Needed only for the preview. See comments section below.
        });
    }

    public static void main(final String[] args) {

        SwingUtilities.invokeLater(() -> new AeroResize("AeroSnap and the Frame State").setVisible(true));
    }
}

虽然这确实有一个副作用,即没有详细预览框架的内容:

enter image description here

使用本机代码的解决方案

如果你愿意使用JNA,那么你可以完全模仿原生平台的最小化。您需要在构建路径中包含 和。jna.jarjna-platform.jar

import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import com.sun.jna.Native;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HWND;

public class AeroResize extends JFrame {

    public AeroResize(final String title) {

        super(title);
        initUI();
    }

    private void initUI() {

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        final JButton minimize = new JButton("Minimize");
        final JButton maximize = new JButton("Maximize");
        final JButton normal = new JButton("Normal");
        add(normal);
        add(minimize);
        add(maximize);
        pack();

        minimize.addActionListener(e -> {
            HWND windowHandle = new HWND(Native.getComponentPointer(AeroResize.this));
            User32.INSTANCE.CloseWindow(windowHandle);
        });
    }

    public static void main(final String[] args) {

        SwingUtilities.invokeLater(() -> new AeroResize("AeroSnap and the Frame State").setVisible(true));
    }
}

这是非常不言自明的。您将获得一个指向窗口的指针,并在其上使用本机(实际上最小化,去图)。请注意,我编写它的简约方式将导致第一次按下按钮时的小延迟,因为实例已加载。您可以在启动时加载它,以避免这种首次延迟。CloseWindowUser32

功劳归于此处接受的答案


答案 2

这似乎是一个Swing错误。错误数据库上的错误报告:

JDK-7029238:组件对齐时未调用组件大小

在这个报告中,bug无法重现,现在你遇到了同样的bug(我认为它是一样的,或者至少是相关的),也许现在是重新打开这个报告的好时机。(我没有找到任何其他参考,所以我假设它没有被修复)


推荐