如果我将一个被 Aero 吸附到屏幕左侧的 JFrame
最小化,方法是通过点击 Windows WindowDecoration 的最小化按钮,并通过 Alt-Tab 或在 Windows 任务栏中单击它来取消最小化,那么该框架会被正确还原并吸附到左侧。非常好!
但是,如果我通过
setExtendedState( getExtendedState() | Frame.ICONIFIED );
当你将鼠标悬停在Windows任务栏上查看预览时,它显示的位置是错误的。 通过Alt-Tab或单击Windows任务栏将其最小化后,该框架会以错误的位置和大小恢复。框架边界是“未捕捉”的值,如果您将框架从屏幕边缘拖动,则Windows通常会记住这些值以便恢复。
Bug的屏幕录制: 我的结论是,Java不知道AeroSnap并向Windows提供了错误的边界。(例如,
Toolkit.getDefaultToolkit().isFrameStateSupported(Frame.MAXIMIZED_VERT));
返回false。)这是我修复此错误的方法:
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框架一起工作?
编辑:
我已在Oracle提交了一个错误报告:http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8147840
JFrame.ICONIFIED
而不是Frame.ICONIFIED
等,您可以删除Frame
导入。 - user1803551