java Swing 调整大小 [英] java swing resize

查看:75
本文介绍了java Swing 调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个库,我在其中传递了一个容器(通常是 JPanel)

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

并为不同的控件、它们的位置、大小和其他属性提供一个 xml 规范.我必须在运行时创建这些控件并将其添加到组件中.处理父容器大小调整的好方法是什么?

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?

推荐答案

如果 XML 指定了绝对位置,那么可能无法优雅地调整组件的大小.您可以创建一个自定义 LayoutManager 来线性缩放它们,但对于大多数组件来说这可能看起来很糟糕.

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.

这是一个可能有用的版本:

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<Component, Rectangle2D> rectMap = new HashMap<Component, Rectangle2D>();

        @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) {
        }
    }
}

这篇关于java Swing 调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆