java swing rectangle,java swing resize

问题

I am writing a library where i am passed in a Container (usually JPanel)

and have an xml specification for different controls, their locations, size and other attributes. i have to create those controls at runtime and add it to the component. What's a good way to handle resizing of the parent Container?

回答1:

If the XML specifies the absolute locations then there is probably no way to elegantly resize the components. You could create a custom LayoutManager that would scale them linearly but that would probably look bad for most components.

EDIT: Here is a version that might be helpful:

public class LinearScaleLayout {

public static void main(String[] args) {

JFrame frame = new JFrame("Linear Scale Layout Test");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setBounds(100, 100, 200, 200);

JPanel scalePanel = new JPanel(new LinearScaleLayoutManager());

scalePanel.setBorder(new EmptyBorder(5, 5, 5, 5));

scalePanel.add(new JTextField(), new Rectangle2D.Double(0, 0, 1, 0.2));

scalePanel.add(new JScrollPane(new JEditorPane()), new Rectangle2D.Double(0, 0.25, 0.45, 0.75));

scalePanel.add(new JSlider(), new Rectangle2D.Double(0.55, 0.25, 0.45, 0.75));

frame.getContentPane().add(scalePanel);

frame.setVisible(true);

}

private static class LinearScaleLayoutManager implements LayoutManager2 {

private final HashMap rectMap = new HashMap();

@Override

public void layoutContainer(Container parent) {

synchronized (parent.getTreeLock()) {

Insets insets = parent.getInsets();

int clientWidth = parent.getWidth() - insets.left - insets.right;

int clientHeight = parent.getHeight() - insets.top - insets.bottom;

if (clientWidth > 0 && clientHeight > 0) {

for (Component component : parent.getComponents()) {

Rectangle2D rect = rectMap.get(component);

if (rect != null) {

component.setBounds(new Rectangle(insets.left + (int)(rect.getX() * clientWidth),

insets.top + (int)(rect.getY() * clientHeight), (int)(rect.getWidth() * clientWidth),

(int)(rect.getHeight() * clientHeight)));

}

}

}

}

}

@Override

public void addLayoutComponent(Component comp, Object constraints) {

rectMap.put(comp, (Rectangle2D)constraints);

}

@Override

public void removeLayoutComponent(Component comp) {

rectMap.remove(comp);

}

@Override

public Dimension minimumLayoutSize(Container parent) {

return new Dimension(0, 0);

}

@Override

public Dimension preferredLayoutSize(Container parent) {

return new Dimension(100, 100);

}

@Override

public Dimension maximumLayoutSize(Container target) {

return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);

}

@Override

public void addLayoutComponent(String name, Component comp) {

}

@Override

public float getLayoutAlignmentX(Container target) {

return 0;

}

@Override

public float getLayoutAlignmentY(Container target) {

return 0;

}

@Override

public void invalidateLayout(Container target) {

}

}

}

来源:https://stackoverflow.com/questions/2302648/java-swing-resize

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值