简体   繁体   中英

Text resize according to button/label size

Apologies in advance if I'm not using the correct words/names/terminologies.

I'm using Swing on NetBeans to create my gui. I was able to make all the buttons and labels stretch/shrink according to the window size thanks to layouts. However the text size of the buttons/labels don't change at all. How do I make the text size scale according to the buttons/labels size?

PS: I haven't written any code so far. It was all made with that JFrame design thingy from NetBeans .

There's no easy way to accomplish this and I would question why you think it's required, however...

While I'm sure there is probably an easy way to achieve this, most of the solutions I looked into required some kind of Graphics context and when I thought about variable width fonts, it quickly became a real mess...

You could use something like...

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TextResize {

    public static void main(String[] args) {
        new TextResize();
    }

    public TextResize() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                final JLabel label = new JLabel("Hello");
                label.addComponentListener(new ComponentAdapter() {

                    protected void decreaseFontSize(JLabel comp) {

                        Font font = comp.getFont();
                        FontMetrics fm = comp.getFontMetrics(font);
                        int width = comp.getWidth();
                        int height = comp.getHeight();
                        int textWidth = fm.stringWidth(comp.getText());
                        int textHeight = fm.getHeight();

                        int size = font.getSize();
                        while (size > 0 && (textHeight > height || textWidth > width)) {
                            size -= 2;
                            font = font.deriveFont(font.getStyle(), size);
                            fm = comp.getFontMetrics(font);
                            textWidth = fm.stringWidth(comp.getText());
                            textHeight = fm.getHeight();
                        }

                        comp.setFont(font);

                    }

                    protected void increaseFontSize(JLabel comp) {

                        Font font = comp.getFont();
                        FontMetrics fm = comp.getFontMetrics(font);
                        int width = comp.getWidth();
                        int height = comp.getHeight();
                        int textWidth = fm.stringWidth(comp.getText());
                        int textHeight = fm.getHeight();

                        int size = font.getSize();
                        while (textHeight < height && textWidth < width) {
                            size += 2;
                            font = font.deriveFont(font.getStyle(), size);
                            fm = comp.getFontMetrics(font);
                            textWidth = fm.stringWidth(comp.getText());
                            textHeight = fm.getHeight();
                        }

                        comp.setFont(font);
                        decreaseFontSize(comp);

                    }

                    @Override
                    public void componentResized(ComponentEvent e) {
                        JLabel comp = (JLabel) e.getComponent();
                        Font font = comp.getFont();
                        FontMetrics fm = comp.getFontMetrics(font);
                        int width = comp.getWidth();
                        int height = comp.getHeight();
                        int textWidth = fm.stringWidth(comp.getText());
                        int textHeight = fm.getHeight();

                        if (textHeight > height || textWidth > width) {

                            decreaseFontSize(comp);

                        } else {

                            increaseFontSize(comp);

                        }
                    }

                });

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(label);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

I did consider using Font#deriveFont(AffineTransform) , but this would require me to have some context as to the original size of the component/text

Since this is a simple program it doesn't need to be as most efficient possible so I end up using an answer posted here that, for some reason, was deleted.

Like this:

button.setFont(button.getFont().deriveFont((float)(button.getWidth()/2)));

in formComponentResized.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM