gpt4 book ai didi

java - 如何消除大尺寸java swing标签中的间隙

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

在我的应用程序中,我有一个字体大小超过 200 的标签。该标签包含较大的上下(不规则)间隙。我怎样才能删除它?

这是我的代码:

package Core;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class LabelDemo extends JPanel {
public LabelDemo() {
super(new GridBagLayout());
JLabel label2;
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
// Create the other labels.
label2 = new JLabel("Text-Only Label");
label2.setBorder(BorderFactory.createTitledBorder("aaaaaaaa"));
label2.setFont(new Font("Verdana", Font.PLAIN, (int) 220));
// label2.setBorder(new EmptyBorder(-50, 0, 0, 0));

// Add the labels.
add(label2, c);
}

/**
* Create the GUI and show it. For thread safety, this method should be invoked from the event dispatch thread.
*/
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("LabelDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Add content to the window.
frame.add(new LabelDemo());

// Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
// Schedule a job for the event dispatch thread:
// creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);

createAndShowGUI();
}
});
}
}

我也试试我的最后一篇文章:How to change gap in swing label并尝试使用插图,但这在 linux 和 windows 中看起来不同

有没有更好的方法来消除这个差距?

最佳答案

JDigit可能会给你一些想法:

  • 它重写了 paintComponent() 以对高分辨率 BufferedImage 进行下采样并控制几何形状。

  • 它使用 setBorderPainted(false) 来设置 borderPainted 属性。

  • 它使用 FocusHandler 进行自定义突出显示。

image

附录:如前所述here , 潜在的问题是字体的 leading,在 FontMetrics 中定义作为包含在字体的高度。正如@Guillaume Polet 在评论中所建议的那样,您可以在自己的 JComponent 中的任意位置呈现文本。 TextLayout,已讨论 here , 可用于计算边界,如下所示。

优点:

  • 对展示位置的绝对控制。

  • TexteLayout 的几何边界基于 FontMetrics

缺点:

  • 不支持 Icon

  • 不支持 HTML。

请注意,JComponent 作者“建议您将组件放在 JPanel 中,并在 JPanel 上设置边框。”

Unleaded image

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
* @see https://stackoverflow.com/a/16014525/230513
*/
public class UnleadedTest {

private static class Unleaded extends JComponent {

private Font font = new Font("Verdana", Font.PLAIN, 144);
private FontRenderContext frc = new FontRenderContext(null, true, true);
private String text;
private TextLayout layout;
private Rectangle r;

public Unleaded(String text) {
this.text = text;
calcBounds();
}

@Override
public Dimension getPreferredSize() {
return new Dimension(r.width, r.height);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
calcBounds();
layout.draw(g2d, -r.x, -r.y);
}

private void calcBounds() {
layout = new TextLayout(text, font, frc);
r = layout.getPixelBounds(null, 0, 0);
}
}

private void display() {
JFrame f = new JFrame("Unleaded");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Unleaded label = new Unleaded("Unleaded");

JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder("Title"));
panel.add(label);
f.add(panel);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new UnleadedTest().display();
}
});
}
}

关于java - 如何消除大尺寸java swing标签中的间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16013184/

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