gpt4 book ai didi

java - 如何平滑地根据容器大小自动调整多个 JLabel 的字体大小?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:03:24 24 4
gpt4 key购买 nike

我需要根据用于调整容器大小的比例因子调整多个 JLabel 的字体大小。为此,我将每个 JLabel 的字体设置为 null,以便它们采用容器的字体。它有效,但它也会产生奇怪的结果。

具体来说,文本似乎“滞后于”容器,有时甚至被截断。我想避免这种行为。知道怎么做吗?

模拟行为的示例代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.geom.AffineTransform;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TextResize implements Runnable {

public static void main(String[] args) {
TextResize example = new TextResize();
SwingUtilities.invokeLater(example);
}

public void run() {
JFrame frame = new JFrame("JLabel Text Resize");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(800, 400));

Container container = frame.getContentPane();
container.setLayout(new BorderLayout());

final JPanel labelContainer = new JPanel(new GridBagLayout());
labelContainer.setBorder(BorderFactory.createLineBorder(Color.black));

//initial font
final Font textFont = new Font("Lucida Console", Font.PLAIN, 10).deriveFont(AffineTransform.getScaleInstance(1, 1));
labelContainer.setFont(textFont);

GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(0, 10, 0, 10);
c.weightx = 1;
for (int i = 0; i < 5; i++) {
JLabel f = new JLabel("Text here with possibly looooooooong words");
f.setBorder(BorderFactory.createLineBorder(Color.green));
f.setFont(null);//take the font from parent
c.gridy = i;
labelContainer.add(f, c);
}

JSlider slider = new JSlider(0,50000,10000);
slider.addChangeListener(new ChangeListener() {
double containerWidth = labelContainer.getPreferredSize().getWidth();
double containerHeight = labelContainer.getPreferredSize().getHeight();

@Override
public void stateChanged(ChangeEvent ev) {
JSlider source = (JSlider) ev.getSource();
double scale = (double) (source.getValue() / 10000d);

//scaling the container
labelContainer.setSize((int) (containerWidth * scale), (int) (containerHeight * scale));

//adjusting the font: why does it 'lag' ? why the truncation at times?
Font newFont = textFont.deriveFont(AffineTransform.getScaleInstance(scale, scale));
labelContainer.setFont(newFont);

//print (font.getSize() does not change?)
System.out.println(scale + " " + newFont.getTransform() + newFont.getSize2D());
}
});

container.add(slider, BorderLayout.NORTH);
JPanel test = new JPanel();
test.setLayout(null);
labelContainer.setBounds(5, 5, labelContainer.getPreferredSize().width, labelContainer.getPreferredSize().height);
test.add(labelContainer);
container.add(test, BorderLayout.CENTER);

frame.pack();
frame.setVisible(true);
}

}

图片: http://i.stack.imgur.com/tZLOO.png

谢谢,

-s

最佳答案

您可以使用以下任何一种方法:

关于java - 如何平滑地根据容器大小自动调整多个 JLabel 的字体大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9814616/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com