2012-12-05 54 views
5

我想增加/自動降低JButton的文本的字體大小(如果JButton的增加/延伸它的文本也將增加,如果一個JButton降低其文本也將減少)。 JButtons的默認字體是Sans-Serif size 20,它永遠不會減少到20以下(它可以是21,30,40或者任何大於或等於20但是不低於20的任何東西)。我有一個名爲MenuJPanel的JPanel,它使用GridLayout添加5個JButtons,隨着JPanel增加/減少,大小會增加/減少。我選擇了GridLayout,因爲它似乎是用於此目的的最佳佈局,我錯了嗎?我還爲MenuJPanel添加了一個componentResized。下面你可以看到我的代碼部分工作。如何根據JButton的大小自動增加或減少JButton文本的大小?

enter image description here

public class MenuJPanel extends JPanel { 

    private JButton resizeBtn1; 
    private JButton resizeBtn2; 
    private JButton resizeBtn3; 
    private JButton resizeBtn4; 
    private JButton resizeBtn5; 

    public MenuJPanel() { 
     initComponents(); 
     this.addComponentListener(new ComponentAdapter() { 
      @Override 
      public void componentResized(ComponentEvent e) { 

       Font btnFont = resizeBtn1.getFont(); 
       String btnText = resizeBtn1.getText(); 

       int stringWidth = resizeBtn1.getFontMetrics(btnFont).stringWidth(btnText); 
       int componentWidth = resizeBtn1.getWidth(); 

       // Find out how much the font can grow in width. 
       double widthRatio = (double) componentWidth/(double) stringWidth; 

       int newFontSize = (int) (btnFont.getSize() * widthRatio); 
       int componentHeight = resizeBtn1.getHeight(); 

       // Pick a new font size so it will not be larger than the height of label. 
       int fontSizeToUse = Math.min(newFontSize, componentHeight); 

       // Set the label's font size to the newly determined size. 
       resizeBtn1.setFont(new Font(btnFont.getName(), Font.BOLD, fontSizeToUse)); 
      } 
     }); 
    } 

    private void initComponents() { 

     resizeBtn1 = new javax.swing.JButton(); 
     resizeBtn2 = new javax.swing.JButton(); 
     resizeBtn3 = new javax.swing.JButton(); 
     resizeBtn4 = new javax.swing.JButton(); 
     resizeBtn5 = new javax.swing.JButton(); 

     setLayout(new java.awt.GridLayout(5, 0)); 

     resizeBtn1.setText("Text to resize 1"); 
     add(resizeBtn1); 

     resizeBtn2.setText("Text to resize 2"); 
     add(resizeBtn2); 

     resizeBtn3.setText("Text to resize 3"); 
     add(resizeBtn3); 

     resizeBtn4.setText("Text to resize 4"); 
     add(resizeBtn4); 

     resizeBtn5.setText("Text to resize 5"); 
     add(resizeBtn5); 
    } 
} 
+3

你的問題是你沒有發佈SSCCE,whats btnOperatorLogout,在主類的地方,對不起懶惰打我也是,btw這個簡單的問題在這個論壇上被解決了很多次,我可以看到DeriveFont的幾個代碼&JLabel的 – mKorbel

+0

謝謝,我只是固定的含義,從JButton的GET插圖和減去1像素的邊框(請參閱有關的JButton行,如果被選中)的示例代碼 – jadrijan

+1

你能夠計算內部區域(見行約的JButton如果被選中),然後導致是可用維SwingUtilities#computeStringWidht :-) – mKorbel

回答

5

通常,ButtonUI委託一個JButton計算基於設計者的選擇的字體和平臺的美學按鈕的首選大小。隨着您的方法擊敗了這一點,您需要決定文本如何使用按鈕進行縮放並適應按鈕裝飾。

缺席創建自己的ButtonUI,你可以簡單地縮放文本。在這種exampleTextLayout用於呈現在BufferedImage文本在根據需要,可以按比例縮小任意大的尺寸。在這種example,單個數字的字形是在BufferedImage渲染和縮放以適應按鈕的當前大小。如果你需要處理的Icon的相對位置,看到SwingUtilities.layoutCompoundLabel(),檢查here

+0

非常感謝你這個答案。我有一個問題,如果我的JButton的文本使用HTML,例如:「 1號線2號線
」,將上面的例子仍然有效? – jadrijan

+0

謝謝我會試試看,並讓你知道 – jadrijan

+0

我不確定; 'layoutCompoundLabel()'可以處理它。對於'JPanel' [編輯],如[顯示](http://stackoverflow.com/a/7028497/230513)所示,可能更容易呈現'JLabel'。 – trashgod