Home >Java >javaTutorial >How to Correctly Position Non-Resizable JFrames with Visible Text in Windows Aero?

How to Correctly Position Non-Resizable JFrames with Visible Text in Windows Aero?

Linda Hamilton
Linda HamiltonOriginal
2024-12-09 05:35:13827browse

How to Correctly Position Non-Resizable JFrames with Visible Text in Windows Aero?

Non-Resizable Window Border and Positioning

Issue

When creating non-resizable JFrames, the setLocation method does not appear to properly consider the window border if Windows Aero is enabled. As a result, the borders overlap. The behavior is as expected if Aero is disabled or if the setResizable calls are removed.

Cause

The issue arises when setting the bounds of a non-resizable container. Changes to the bounds may render text invisible depending on the user's platform-specific font settings.

Solution

The correct approach to setting the bounds of a non-resizable container is to ensure that any text remains visible regardless of the host platform's default font. This can be achieved by adjusting the bounds based on the string's pixel width and the height of the container.

For example, the following code calculates the string's pixel width using computeStringWidth and sets the frame size accordingly:

JLabel label = new JLabel(s + "3, 1, 4, 1, 5, 9", JLabel.LEFT);

int w = SwingUtilities.computeStringWidth(
    label.getFontMetrics(label.getFont()), s);
int h = f.getHeight();
f.setSize(w, h);
f.setResizable(false);

This ensures that the text is visible regardless of the font settings on the user's platform.

The above is the detailed content of How to Correctly Position Non-Resizable JFrames with Visible Text in Windows Aero?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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