开发者

Java Swing: component that resizes itself but doesn't influence the layout

开发者 https://www.devze.com 2023-01-20 09:01 出处:网络
I\'ll try to explain my problem as simply as possible but it\'s a tricky topic and people who haven\'t encountered the issue probably won\'t know what I\'m talking about.

I'll try to explain my problem as simply as possible but it's a tricky topic and people who haven't encountered the issue probably won't know what I'm talking about.

I want to use a BorderLayout using west, east, north, south, etc. components that are my "normal" components (JLabels, JButtons, etc.) then I want the center component to be an "image" (that is: pixels). To this end I'm using a BufferedImage and using setIcon on a JLabel that is inside a panel that is part of the "center".

However I want my image/pixels to be "fluid": whenever the user resizes the app, I want to compute the exact size of the JLabel (icon/text gap is set to 0) and then create a new image (pixels that I manipulate directly in a BufferedImage but whatever) that has exactly that size.

Now it does work fine. But only when I resize the main window ("window" as in "one of the window of the operating system) by making it bigger.

It doesn't work when I downsize my main window.

The reason, after a lot of testing, is obviously because the size of my JLabel (in which I did a setIcon( img ) is influencing the computation of the layout manager.

So here comes the billion dollar question: how should I us开发者_StackOverflow社区e a BorderLayout (or any other layout) so that I can create a "fluid" rectangle of pixels in the center of my app?


Answering my own question with an answer that I will not accept even tough it does work...

The problem can be "worked around" by creating a picture a few pixels smaller than the getVisibleRect of the center area.

So in my case I create an ImageIcon from a BufferedImage that is 20 pixels smaller (both in width and height) than the area that will hold it.

What happens then is that because the picture is smaller it doesn't "block"/prevent the layout manager from putting everything at their correct place when downsizing the main window.

So by using such an hack I get the "fluid" behavior I want.

This is however an hack whose level of hackyness cannot be understated and I'm sure there's a very clean way to solve this.


The reason, after a lot of testing, is obviously because the size of my JLabel (in which I did a setIcon( img ) is influencing the computation of the layout manager.

The preferred size of the JLabel is used in the preferred size of the panel, but this size is ignored when you resize the frame, since the CENTER only gets whatever space is left over after the preferred size of the other 4 components is considered.

To this end I'm using a BufferedImage and using setIcon on a JLabel that is inside a panel that is part of the "center".

Sounds to me like it should work.

Create the panel with a BorderLayout. Add the JLabel to the Center of your main panel. Then add a ComponentListener to the panel. Now when the frame is resized the center panel size will be adjusted to take the space available to it. Now that you know the size of the center panel you can recreate the Icon and add it to your JLabel,

This is how you write a SSCCE:

import java.awt.*;
import javax.swing.*;

public class LabelTest2 extends JFrame
{
    public LabelTest2()
    {
        JLabel picture = new JLabel(new ImageIcon("???.jpg"));
        add(picture);
    }

    public static void main(String[] args)
    {
        LabelTest2 frame = new LabelTest2();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消