gpt4 book ai didi

java - 标签错误中的文本对齐?

转载 作者:行者123 更新时间:2023-11-29 03:43:15 25 4
gpt4 key购买 nike

现在我正在尝试在框架旁边添加标签。我有一个标签文本,想将它对齐到中心,但它失败了,总是左对齐。请帮助我!

Simulation sm = new Simulation(dm);
JFrame simulation = new JFrame();
simulation.setTitle("Traffic light and Car park Siumulation");

simulation.setSize(800,600);
simulation.setResizable(false);
simulation.setVisible(true);
simulation.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
simulation.add(sm, BorderLayout.CENTER);

//Carpark label
JLabel carparklb = new JLabel("abcd");
carparklb.setPreferredSize(new Dimension(200,600));

carparklb.setVerticalTextPosition(JLabel.CENTER);
carparklb.setHorizontalTextPosition(SwingConstants.CENTER);

simulation.add(carparklb, BorderLayout.EAST);

最佳答案

来自 Java 文档

setVerticalTextPosition(int textPosition)

Sets the vertical position of the label's text, relative to its image.

尝试 JLabel.setVerticalAlignmentJLabel.setHorizontalAlignment相反

关于java - 标签错误中的文本对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12171164/

25 4 0