gpt4 book ai didi

java - 根据按钮/标签大小调整文本大小

转载 作者:行者123 更新时间:2023-12-02 06:23:11 24 4
gpt4 key购买 nike

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

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

PS:到目前为止我还没有编写任何代码。这一切都是用 NetBeansJFrame 设计 thingy 制作的。

最佳答案

没有简单的方法可以实现这一点,但是我会质疑为什么您认为这是必需的......

虽然我确信可能有一种简单的方法可以实现这一点,但我研究的大多数解决方案都需要某种图形上下文,当我考虑可变宽度字体时,它很快就变成了真是一团糟...

你可以使用类似...

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),但这需要我对组件/文本的原始大小有一些背景

关于java - 根据按钮/标签大小调整文本大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20818489/

24 4 0