2012-02-16 71 views
5

Có một câu hỏi tương tự ở đây: How to change the size of the font of a JLabel to take the maximum sizeLàm thế nào để thay đổi kích thước phông chữ JLabel để điền vào không gian trống JPanel trong khi thay đổi kích thước?

nhưng không hoạt động ngược.

Vì vậy, nếu bạn thực hiện JPanel lớn, phông chữ đang phát triển, nhưng nếu bạn thực hiện nó nhỏ hơn JLabel kích thước và phông chữ vẫn như trước

Check this Image

Làm thế nào để làm cho Jlabel phông chữ ngày càng tăng theo kích thước JPanel trong khi thay đổi kích thước?

+0

Bạn có thử modyifying mã để khắc phục vấn đề khi kích thước khung co lại? – camickr

Trả lời

0

ok, đây là câu trả lời.

Hãy lấy mã từ đây: How to change the size of the font of a JLabel to take the maximum size, câu trả lời từ coobird

int componentWidth = label.getWidth(); 

Chúng ta cần để có được chiều rộng từ các thành phần JFrame, KHÔNG từ JLabel, vì mã sẽ không có cơ hội let để thay đổi kích thước cho JLabel.

này sẽ sửa chữa nó:

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

cho vùng chứa.getSize/getBounds bạn phải thêm Swing Timer, để tránh gây ra nhấp nháy, chỉ gọi cho khởi động lại cho đến khi thay đổi kích thước không kết thúc, – mKorbel

+0

Tại sao '20'; không nên dựa trên 'FontMetrics'? – trashgod

4

bằng cách sử dụng FontMetricsTextLayout bạn có thể nhận được kết quả này (xin đọc một bình luận trong các mã)

SwingUtilities thể làm điều đó một cách chính xác quá

tôi sugget để thêm một vài điểm ảnh hơn nữa trên cả hai hướng

thêm ComponentListener vào vùng chứa và thành phầnĐối với sự kiện được đánh giá lại tính toán lại 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

cảm ơn bạn. Nhưng nó không làm những gì tôi có nghĩa là .. Phông chữ của JLable phải được thay đổi sau khi JFrame thay đổi kích cỡ. Vì vậy, nếu bạn làm cho JFrame lớn hơn/nhỏ hơn - phông chữ JLabel cũng phải được thay đổi .. – VextoR

+1

Có một ví dụ liên quan thực hiện điều này [ở đây] (http://stackoverflow.com/a/8282330/230513). – trashgod

+0

Tôi đã cập nhật câu trả lời của mình (có lẽ tôi không đọc câu hỏi của bạn ...) cho CompomentListener, – mKorbel

4

Như thế này? http://java-sl.com/tip_adapt_label_font_size.html

UPDATE:

mã theo yêu cầu

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

+1 cho 'deriveFont()'. – trashgod

+0

Tôi biết câu trả lời này là cũ nhưng nó cũng là một câu trả lời chỉ có một. Bạn có thể sao chép thông tin cần thiết từ liên kết không. –

Các vấn đề liên quan