当前位置: 首页 > 面试题库 >

如何消除大尺寸的Java Swing标签中的间隙

寿卜鹰
2023-03-14
问题内容

在我的应用程序中,我有一个字体大小超过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();
            }
        });
    }
}

有什么更好的方法可以消除这种差距吗?


问题答案:

JDigit may give you some
ideas:

  • It override’s paintComponent() to down-sample a high-resolution BufferedImage and control the geometry.

  • It uses setBorderPainted(false) to set the borderPainted property.

  • It uses a FocusHandler for custom highlighting.

image

Addendum: As noted here, the
underlying problem is the font’s leading , defined in
FontMetrics
as being included in the font’s height. As suggested in a comment by
@Guillaume Polet, you can render the text wherever you want in your own
JComponent. TextLayout, discussed here can be used to calculate
the bounds, as shown below.

Pros:

  • Absolute control over placement.

  • Geometry of TexteLayout bounds based on FontMetrics.

Cons:

  • No Icon support.

  • No HTML support.

Note that the JComponent authors “recommend that you put the component in a
JPanel and set the border on the 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();
            }
        });
    }
}


 类似资料:
  • 有人知道JavaFX中画布的最大尺寸吗?从一些测试中,它接缝为8192(与IE相同),在我看来,这很奇怪。也许,可以修改吗?

  • 但是我尝试重写、、等似乎都不起作用,或者可能是我实现错误。我需要调整它的内部子组件吗?或者仅仅调整

  • 我很想知道相互独立的最大位图宽度和高度是多少。我确实发现最大尺寸是32768x32768,但这只是一个完美的正方形吗?32768x32768=1073741824是我可以玩的像素总数吗?我可以在宽度和高度之间重新排列这些像素,只要总数不超过? 如果我这样做,我不会有任何错误: 将位图变暗为位图=新位图(450100000) 即使我无法在保存后打开图像(我不需要这样做),我仍然能够使用位图,但我相信

  • 问题内容: 我试图用一层构建CNN,但是我有一些问题。确实,编译器告诉我 ValueError:检查模型输入时出错:预期conv1d_1_input具有3维,但数组的形状为(569,30) 这是代码 问题答案: td; LR你需要重塑你的数据有一个 空间 维度是有道理的: 本质上重塑如下所示的数据集: 至: 解释和例子 通常,卷积在空间维度上起作用。内核在产生张量的维度上“卷积”。对于Conv1D

  • 添加以下代码后: 它正在流离失所

  • 我读到过,太多的小分区会因为开销而损害性能,例如,向执行器发送大量任务。 使用最大的分区的缺点是什么?例如,为什么我会看到100s的MB范围内的建议? 如果丢失了一个分区,则需要进行大量的重新计算。对于许多较小的分区,您可能会更经常地丢失分区,但在运行时中的差异会更小。 如果在大分区上执行的少数任务中有一个任务的计算时间比其他任务长,这将使其他核心未被利用,但使用较小的分区,可以更好地在集群中分配