开发者

How to make a swing component's bounds fixed

开发者 https://www.devze.com 2023-04-11 18:16 出处:网络
I have a JPanel to which I have added a few components (checkbox, combo etc.). I have noticed that, when the frame is maximized, the bounds of components also move or shift to the right. Then, on rest

I have a JPanel to which I have added a few components (checkbox, combo etc.). I have noticed that, when the frame is maximized, the bounds of components also move or shift to the right. Then, on restore the components are shifted back to the original position.

On 21 and above inch monitor, the components shift really makes a difference as you can see the components move far right.

We are using customized layout manager which implements java.awt.LayoutManager2. The class content is quiet huge, so will point to the areas which determine the bounds for the components.

protected int hMargin = 0;
..
Insets insets = target.getInsets();
Dimension size = target.getSize();

int  x = (size.width - insets.left - insets.right - 15 * hMargin);

And from the frame which calls the layout and add the components as shown below:

JPanel  pl = new JPanel(new OurLayout(this))
//add the component to panel
pl.add(checkBox);
..

At the point where we decide x, I want to add a line to prevent the components from shifting when the frame on which the calling panel is added is maximized.

Can anyone开发者_StackOverflow suggest any ideas on how to achieve this? example code will be well appreciated.


If you have a custom layout manager, then your layout manager should respect the preferred size of the panel. The problem is with your layout management code.

Or for a simple solution add your custom panel to another panel that uses a FlowLayout. Then the preferred size of your custom panel will be respected.


Thanks to Thomas and camickr for their recommendations, I have achieved a solution using the line of syntaxes below.

      size=c.getMinimumSize();
      int dist =(size.width - insets.left - insets.right);
      int move = ((dist > 32) ? 35 : 38);
      x = (dist + move * hMargin);

Again thanks to all for the recommendations.

0

精彩评论

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

关注公众号