如何消除大尺寸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进行下采样并控制几何. 附录:如here所述,根本问题是字体的前导,在 优点: >绝对控制放置. 缺点: >没有Icon支持. 请注意,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(); } }); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- java – HttpProtocolParams.setUseExpectContinue(params,
- 如何使用Java计算Selenium WebDriver中选择下拉框中的选项数
- Java创建ZIP压缩文件的方法
- Largest Rectangle in a Histogram(单调栈)
- Java中==运算符与equals方法的区别及intern方法详解
- java – 如何打印“Hello World”? – 完整的生命周期
- 多线程 – 我不懂多线程编程
- java – 如何初始化一个对象数组?
- transactionAttributes各属性意义及配置
- 在Java中比较java.sql.Time的正确方法