MigLayout-componentResized()被调用太多次

时间:2019-05-21 14:41:28

标签: java swing miglayout

我有以下情况-我有一个面板,该面板在另一个面板中包含一个组件(在这种情况下为标签)-全部用MigLayout定义。

调整面板大小时,我要调整JLabel的字体大小以适合整个区域。

我将代码剥离到最低限度

public class TestClass {

    private JPanel panel;
    private JLabel displayLabel;

    private TestClass() {
        panel = new JPanel(new MigLayout(new LC().fill().gridGap("0", "0").insetsAll("0").hideMode(3)));

        displayLabel = new JLabel("some text goes here");
        displayLabel.setFont(new Font("Dialog", Font.BOLD, 9));
        panel.add(displayLabel, new CC().grow().push());

        panel.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                scale(displayLabel);
            }
        });
    }

    private void scale(JLabel component) {
        Integer lastWidth = (Integer) component.getClientProperty("width");
        Integer lastHeight = (Integer) component.getClientProperty("height");
        if (lastWidth != null && lastHeight != null && lastWidth == component.getWidth() && lastHeight == component.getHeight()) {
            return;
        }

        component.putClientProperty("width", component.getWidth());
        component.putClientProperty("height", component.getHeight());

        int minFontSize = 9;
        Font oldFont = component.getFont();
        float size = oldFont.getSize();
        Font newFont = oldFont.deriveFont(size);
        FontMetrics fm = component.getFontMetrics(newFont);

        int availWidth = component.getWidth() - 2;
        int availHeight = component.getHeight() - 2;

        boolean found = false;
        boolean increased = false;
        boolean decreased = false;
        while (!found) {
            int stringWidth = fm.stringWidth(component.getText());
            int stringHeight = fm.getHeight();
            if (!decreased && stringWidth < availWidth && stringHeight < availHeight) {
                size++;
                increased = true;
            } else if (stringWidth > availWidth || stringHeight > availHeight) {
                size--;
                decreased = true;
            } else {
                found = true;
            }
            if (increased && decreased) {
                found = true;
            }
            newFont = oldFont.deriveFont(size);
            fm = component.getFontMetrics(newFont);
        }

        if (size < minFontSize) {
            size = minFontSize;
            newFont = oldFont.deriveFont(size);
        }

        component.setFont(newFont);
    }


    public JPanel getPanel() {
        return panel;
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("");
        JPanel pnl = new JPanel(new MigLayout(new LC().fill()));
        pnl.add(new TestClass().getPanel(), new CC().grow().push());
        frame.add(pnl, BorderLayout.CENTER);
        frame.setSize(1024, 700);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

当我通过拖动右边缘以放大它来调整框架大小时-它工作得很好,但是,当我尝试通过拖动右边缘的左边来减小它的大小时,应用程序速度很慢并且结果没有得到反映立即在GUI中。请运行它以了解我的意思。

您是否知道我如何才能改进此代码以使其工作顺利?

enter image description here

更新:我添加了一些日志记录,并且观察到当增加宽度时它可以正常工作,但是当减小面板的宽度时,我看到componentResize()方法被称为比放大时要多。

问题的原因似乎是MigLayout与grow()结合使用的。我已经用BorderLayout替换了所有布局,并且看起来工作正常。

但是,由于我需要面板在布局中增长,因此我无法真正做到这一点。

2 个答案:

答案 0 :(得分:0)

由于该方法被多次调用,因此尝试只计算一次该值。您可以将最后计算的宽度和高度存储在组件中,并在这些值之一更改时计算大小:

private void scale(JLabel component) {
    int minFontSize = 9;
    float size = Float.MAX_VALUE;
    size = Math.min(size, component.getWidth());
    size = Math.min(size, component.getHeight());

    Integer lastWidth = (Integer) component.getClientProperty("width");
    Integer lastHeight = (Integer) component.getClientProperty("height");
    if (lastWidth != null && lastHeight != null && lastWidth.intValue() == component.getWidth() && lastHeight.intValue() == component.getHeight()) {
        return;
    }

    component.putClientProperty("width", component.getWidth());
    component.putClientProperty("height", component.getHeight());

    component.putClientProperty("lastSize", new Integer((int) size));

    Font oldFont = component.getFont();
    Font newFont = oldFont.deriveFont(size);
    FontMetrics fm = component.getFontMetrics(newFont);

    int availWidth = component.getWidth() - 2;
    int availHeight = component.getHeight() - 2;

    while ((size > minFontSize) && ((fm.stringWidth(component.getText()) >= availWidth) || (fm.getHeight() >= availHeight))) {
        newFont = oldFont.deriveFont(--size);
        fm = component.getFontMetrics(newFont);
    }

    component.setFont(newFont);
}

此外,您可以尝试像二进制搜索那样找到size变量,而不是逐个递减。对于范围[9 - size=min(width, height)],如果适合搜索范围(size - 9)/2,请首先尝试[(size - 9)/2 - size]。如果不合适,请在[9 - (size - 9)/2]之间进行搜索。

这是一个不断增长的GridBagLayout替代方案:

private TestClass() {
    panel = new JPanel(new GridBagLayout());

    displayLabel = new JLabel("some text goes here");
    displayLabel.setFont(new Font("Dialog", Font.BOLD, 9));
    GridBagConstraints gc = new GridBagConstraints();
    gc.fill = GridBagConstraints.BOTH;
    gc.weightx = 1;
    gc.weighty = 1;
    panel.add(displayLabel, gc);

    panel.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            scale(displayLabel);
        }
    });
}

答案 1 :(得分:0)

我不确定为什么,但是为JLabel设置最小大小似乎可以解决问题。

private TestClass() {
    panel = new JPanel(new MigLayout(new LC().fill().gridGap("0", "0").insetsAll("0").hideMode(3)));

    displayLabel = new JLabel("some text goes here");
    displayLabel.setFont(new Font("Dialog", Font.BOLD, 9));
    displayLabel.setMinimumSize(new Dimension(30, 10));
    panel.add(displayLabel, new CC().grow().push());

    panel.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            scale(displayLabel);
        }
    });
}