Home > Java > javaTutorial > Why Do Non-Resizable JFrames Overlap When Using Windows Aero?

Why Do Non-Resizable JFrames Overlap When Using Windows Aero?

Linda Hamilton
Release: 2024-12-06 05:42:14
Original
870 people have browsed it

Why Do Non-Resizable JFrames Overlap When Using Windows Aero?

Non-Resizable Window Borders and Positioning

In situations where non-resizable JFrames are used and Windows Aero is enabled, the setLocation method's behavior may appear inconsistent when considering window borders.

To illustrate this behavior, consider the following code snippet:

import java.awt.Rectangle;
import javax.swing.JFrame;
public class FrameBorders {
    public static void main(String[] args) {
        JFrame frame1 = new JFrame("frame 1");
        JFrame frame2 = new JFrame("frame 2");

        frame1.setResizable(false);
        frame2.setResizable(false);

        frame1.setVisible(true);       
        Rectangle bounds = frame1.getBounds();      
        frame2.setLocation(bounds.x + bounds.width, bounds.y);
        frame2.setVisible(true);
    }
}
Copy after login

With this code, you might expect frame2 to be positioned to the right of frame1. However, when Windows Aero is enabled, the borders of the two frames overlap.

Explanation and Solution

Windows Aero applies different styling to non-resizable windows, resulting in a thicker border. Because the setLocation method considers the window's raw dimensions without accounting for the border thickness, it incorrectly positions the windows, leading to the overlapping issue.

To achieve the desired behavior of two non-resizable frames positioned side by side without overlapping borders, you can:

  • Use JDialogs Instead: JDialogs typically display a thinner border than JFrames. In the example code, replacing frame2 with a JDialog resolves the overlapping borders issue.
  • Disable Aero: If using JDialogs is not feasible, you can disable Aero's effects by setting the system property "sun.java2d.noddraw" to "true". This can improve border positioning accuracy.
  • Adjust Bounds Manually: Determine the appropriate border thickness based on your platform and adjust the window bounds manually to compensate for it. This requires careful measurement and testing to ensure proper positioning on different systems.
  • Consider Native Windowing: Native windowing frameworks, such as AWT or Swing, may provide more precise control over window borders and positioning than Java's standard API.

The above is the detailed content of Why Do Non-Resizable JFrames Overlap When Using Windows Aero?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template