栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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.

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
.”

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;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(); }        });    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/370330.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号