如何消除大尺寸java swing标签的差距

前端之家收集整理的这篇文章主要介绍了如何消除大尺寸java swing标签的差距前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的应用程序中,我有一个字体大小超过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));

        // 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并尝试使用insets,但这在linux和windows中看起来不同

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

解决方法

JDigit可能会给你一些想法:

>它覆盖paintComponent()以对高分辨率BufferedImage进行下采样并控制几何.
>它使用setBorderPainted(false)来设置borderPainted属性.
>它使用FocusHandler进行自定义突出显示.

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

优点:

>绝对控制放置.
>基于FontMetrics的TexteLayout边界的几何.

缺点:

>没有Icon支持.
>没有HTML支持.

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

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

    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();
            }
        });
    }
}
原文链接:https://www.f2er.com/java/125260.html

猜你在找的Java相关文章