2012-02-16 102 views
5

還有一個類似的問題在這裏: How to change the size of the font of a JLabel to take the maximum size如何更改JLabel字體大小以在調整大小時填充JPanel空閒空間?

但它不起作用倒退。

所以,如果你讓JPanel更大,字體越來越大,但如果你把它縮小JLabel大小和字體仍然像以前一樣

Check this Image

如何使Jlabel字體根據JPanel尺寸,同時調整增長?

+0

你嘗試modyifying代碼時幀的大小縮小到解決這一問題? – camickr 2012-02-16 16:01:38

回答

0

確定,這裏就是答案。

讓我們的代碼從這裏:How to change the size of the font of a JLabel to take the maximum size,從答案coobird

int componentWidth = label.getWidth(); 

我們需要從JLabelJFrame組件獲得寬度,不是,因爲代碼會沒有讓機會爲改變大小JLabel。

這將解決它:

int componentWidth = this.getWidth()-20; // '20' - according width of Jlabel to JFrame 
+2

爲容器。getSize/getBounds你必須添加擺動計時器,以避免引起閃爍,只需要重新啓動,直到調整大小不會結束, – mKorbel 2012-02-16 14:35:34

+0

爲什麼'20';這不應該基於'FontMetrics'嗎? – trashgod 2012-05-15 14:02:11

4

使用FontMetricsTextLayout你可以得到這個輸出(請閱讀代碼中的註釋)

SwingUtilities能做到正確太

我sugget到另外加上兩個方向幾個像素

ComponentListener添加到容器和組件上Resize事件再次重新計算FontMetrics

enter image description here

import java.awt.*; 
import java.awt.font.TextLayout; 
import java.awt.geom.Rectangle2D; 
import javax.swing.*; 

public class ShowFontMetrics extends JFrame { 

    private static final long serialVersionUID = 1L; 
    private JLabel lTime; 

    public ShowFontMetrics() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JPanel pane = new JPanel(); 
     pane.setLayout(new FlowLayout()); 
     lTime = new JLabel("88:88"); 
     lTime.setFont(new Font("Helvetica", Font.PLAIN, 88)); 
     FontMetrics fm = lTime.getFontMetrics(lTime.getFont()); 
     TextLayout layout = new TextLayout(lTime.getText(), lTime.getFont(), fm.getFontRenderContext()); 
     Rectangle2D bounds = layout.getBounds(); 
     Dimension d = lTime.getPreferredSize(); 
     d.height = (int) (bounds.getHeight() + 100);// add huge amount of pixels just for for fun 
     d.width = (int) (bounds.getWidth() + 150);// add huge amount of pixels just for for fun 
     lTime.setPreferredSize(d); 
     lTime.setVerticalAlignment(SwingConstants.CENTER); 
     lTime.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); 
     pane.add(lTime); 
     setContentPane(pane); 
    } 

    public static void main(String[] arguments) { 
     ShowFontMetrics frame = new ShowFontMetrics(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 
+0

謝謝。但它並沒有達到我的意思..在JFrame調整大小後,必須更改JLable的字體。所以,如果你讓JFrame更大/更小 - JLabel字體也必須更改。 – VextoR 2012-02-16 13:24:35

+1

有一個相關示例可以執行此操作[此處](http://stackoverflow.com/a/8282330/230513)。 – trashgod 2012-02-16 13:26:57

+0

我更新了我的答案(可能我沒有讀你的問題......)CompomentListener, – mKorbel 2012-02-16 13:27:25

4

是這樣的? http://java-sl.com/tip_adapt_label_font_size.html

UPDATE:

代碼的要求

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ComponentAdapter; 
import java.awt.event.ComponentEvent; 

public class ResizeLabelFont extends JLabel { 
    public static final int MIN_FONT_SIZE=3; 
    public static final int MAX_FONT_SIZE=240; 
    Graphics g; 

    public ResizeLabelFont(String text) { 
     super(text); 
     init(); 
    } 

    protected void init() { 
     addComponentListener(new ComponentAdapter() { 
      public void componentResized(ComponentEvent e) { 
       adaptLabelFont(ResizeLabelFont.this); 
      } 
     }); 
    } 

    protected void adaptLabelFont(JLabel l) { 
     if (g==null) { 
      return; 
     } 
     Rectangle r=l.getBounds(); 
     int fontSize=MIN_FONT_SIZE; 
     Font f=l.getFont(); 

     Rectangle r1=new Rectangle(); 
     Rectangle r2=new Rectangle(); 
     while (fontSize<MAX_FONT_SIZE) { 
      r1.setSize(getTextSize(l, f.deriveFont(f.getStyle(), fontSize))); 
      r2.setSize(getTextSize(l, f.deriveFont(f.getStyle(),fontSize+1))); 
      if (r.contains(r1) && ! r.contains(r2)) { 
       break; 
      } 
      fontSize++; 
     } 

     setFont(f.deriveFont(f.getStyle(),fontSize)); 
     repaint(); 
    } 

    private Dimension getTextSize(JLabel l, Font f) { 
     Dimension size=new Dimension(); 
     g.setFont(f); 
     FontMetrics fm=g.getFontMetrics(f); 
     size.width=fm.stringWidth(l.getText()); 
     size.height=fm.getHeight(); 

     return size; 
    } 

    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     this.g=g; 
    } 

    public static void main(String[] args) throws Exception { 
     ResizeLabelFont label=new ResizeLabelFont("Some text"); 
     JFrame frame=new JFrame("Resize label font"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     frame.getContentPane().add(label); 

     frame.setSize(300,300); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

} 
+0

對於'deriveFont()'+1。 – trashgod 2012-02-16 19:15:08

+0

我知道這個答案是舊的,但它也是一個只有鏈接的答案。你能從鏈接中複製所需的信息嗎? – 2015-04-28 18:05:19

相關問題