【问题标题】:Text resize according to button/label size根据按钮/标签大小调整文本大小
【发布时间】:2013-12-28 19:30:27
【问题描述】:

如果我没有使用正确的单词/名称/术语,请提前道歉。

我在 NetBeans 上使用 Swing 来创建我的 gui。由于布局,我能够根据窗口大小使所有按钮和标签拉伸/收缩。但是按钮/标签的文本大小根本不会改变。 如何根据按钮/标签大小调整文本大小?

PS:到目前为止,我还没有编写任何代码。这一切都是用JFrame 设计thingy 来自NetBeans

【问题讨论】:

    标签: java swing text netbeans resize


    【解决方案1】:

    没有简单的方法可以做到这一点,我想问你为什么认为它是必需的,但是......

    虽然我确信可能有一种简单的方法可以实现这一点,但我研究的大多数解决方案都需要某种 Graphics 上下文,当我想到可变宽度字体时,它很快就变得一团糟。 .

    你可以使用类似...

    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);
                }
            });
        }
    
    }
    

    我确实考虑过使用Font#deriveFont(AffineTransform),但这需要我了解组件/文本的原始大小

    【讨论】:

    • 恭喜获得 100K。 :) 顺便说一句 - 您是否打算错误地拼写您个人资料的第一个单词?!?
    • @AndrewThompson 仍然比大个子差 21 分,但正在努力争取。是的,我做每件事都是故意的,只是不正确;)
    【解决方案2】:

    由于这是一个简单的程序,它不需要尽可能高效,所以我最终使用了此处发布的答案,由于某种原因,该答案已被删除。

    像这样:

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

    在 formComponentResized.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-08
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多