2013-10-22 32 views
3

當JLabel中的文本太長時,在文本的末尾會出現可見的3個點。是否有可能將它們放在開頭?在JLabel的開頭放置3個點

+0

不直接認識,而不是需要的,但問了幾個時間XxxRenderer較長說明(默認情況下有一個JLabel作爲渲染器組件) – mKorbel

+0

@LittleChild這是一種規範的要求,不是我的任性 – tobi

+0

@tobi我有想出一個瘋狂的答案。看看是否有幫助。 –

回答

4

您可以考慮使用FontMetrics。類來查看當前字體下文本的長度。

_________________________________ 
| 
| This is some really long text that I want to fit in the small label 
|________________________________ 

     ^^^ YOUR LABEL ^^^ 

假設您想將長文本放入該標籤中。
這裏是你可以做什麼(這只是胡亂猜測,我使這個上飛)

  1. 開始與三個點...String
  2. 開始向其中逐一添加附加字符。
  3. 獲取您的JLabel的寬度。
  4. 使用FontMetrics來衡量你的文本的長度,以像素爲單位,當你添加更多字符
  5. 保持只要文本的像素長度小於你的JLabel
  6. 一旦它的寬度增加更多的字符變得大於JLabel的寬度,跳出循環。
  7. 設置這種新形成的文本作爲您的JLabel

文本您應該結束了這樣的:

_________________________________ 
| 
| ...This is some really long tex 
|________________________________ 

     ^^^ YOUR LABEL ^^^ 

這裏上手FontMetrics一個簡單的方法。避免在那裏發生爭吵。只要做到接受的答案說的話:Java: Friendlier way to get an instance of FontMetrics

SSCCE是按照什麼樣的OP真正想要的,而不是我解釋

package stack; 

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.FontMetrics; 
import java.awt.Toolkit; 
import java.awt.event.ComponentEvent; 
import java.awt.event.ComponentListener; 

import javax.swing.BorderFactory; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.SwingUtilities; 

public class BackwardsDots extends JFrame{ 

    JLabel label = new JLabel(){ 
         @Override 
         public Dimension getPreferredSize(){ 
          return new Dimension(200,100); 
         } 
        }; 
    String text = "This is a design requirement and not my whim"; 
    FontMetrics fm; 
    Font theFontBeingUsed; 
//-------------------------------------------------------------------------------- 
    public BackwardsDots(){ 
     getContentPane().add(label); 
     pack(); 

     theFontBeingUsed = new Font("Ubuntu",Font.BOLD,14); 
     fm = Toolkit.getDefaultToolkit().getFontMetrics(theFontBeingUsed); 



     label.setText(trimmedStringCalculator(text)); 
     label.setToolTipText(text); 
     label.setBorder(BorderFactory.createDashedBorder(Color.RED)); 
     label.addComponentListener(new ComponentListener(){ 

      @Override 
      public void componentHidden(ComponentEvent arg0) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void componentMoved(ComponentEvent arg0) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void componentResized(ComponentEvent arg0) { 
       label.setText(trimmedStringCalculator(text)); 
      } 

      @Override 
      public void componentShown(ComponentEvent arg0) { 
       // TODO Auto-generated method stub 

      } 

     }); 

     setVisible(true); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
//-------------------------------------------------------------------------------- 
    private String trimmedStringCalculator(String inputText){ 
     String ellipses = "..."; 
     String textToBeDisplayed = ""; 

     int widthOfJLabel = label.getWidth(); 

     for(int i = text.length()-1; i >= 0; i--){ 
      if(fm.stringWidth(ellipses + textToBeDisplayed) <= widthOfJLabel){ 
       textToBeDisplayed = text.charAt(i) + textToBeDisplayed; 
      } 
     } 

     String finalText; 
     if(textToBeDisplayed.equals(inputText)){ 
      finalText = inputText; 
     }else{ 
      finalText = ellipses.concat(textToBeDisplayed); 
     } 

     return finalText; 
    } 
//-------------------------------------------------------------------------------- 
    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable(){ 
      @Override 
      public void run(){ 
       new BackwardsDots(); 
      } 
     }); 
    } 
} 

輸出
enter image description here

+0

從我從OP中瞭解到的情況來看,我認爲他想要像'......適合小標籤'那樣的東西。這會更有意義。 – Flawyte

+0

這不是我想要的方式。在我的情況下,它應該打印如下內容:「...想要適應小標籤」 – tobi

+0

以'...'開頭。創建一個名爲'textTobeDisplayed'的新字符串,並向後迭代,即從文本的末尾開始向前移動。將字符追加到'textToBeDisplayed'的開頭,然後測量總長度,即......的長度加上'textToBeDisplayed' =) –

0

我認爲這是一個系統行爲,超過JLabel的,所以你不能這樣做。

1

我有另一種解決方案,它依賴於LabelUI。首先代碼:

LabelUI labelUI = new MetalLabelUI() { 
    @Override 
    protected String layoutCL(JLabel label, FontMetrics fontMetrics, String text, Icon icon, Rectangle viewR, Rectangle iconR, Rectangle textR) { 
     String clipString = "..."; 
     // Use reversed text, because first characters may be larger or thinner than last ones. 
     String reversedText = new StringBuilder(text).reverse().toString(); 
     // Let Swing do its magic. 
     String result = super.layoutCL(label, fontMetrics, reversedText, icon, viewR, iconR, textR); 

     // Not using .equals is intentional. Previous method will return a different instance 
     // if and only if a clip operation occurred. 
     if (result != text) { 
      // Use the same character count, but starting with the end. 
      result = clipString 
        + text.substring(text.length() - result.length() + clipString.length()); 
     } else { 
      // Restore the original 
      result = text; 
     } 

     return result; 
    } 

}; 

的目標是讓鞦韆計算的一切,包括它的剪切字符串,並以此作爲線索來執行我們自己的左裁剪。 訣竅是我們必須向super方法提供反轉字符串,因爲我們的結果會剪裁前導字符,所以我們需要確保計算是正確的。字符具有不同的寬度。

對於我來說,主要優勢在於,與目前在UI之前計算新大小的解決方案相比,開銷非常小,並且UI將開始執行相同的操作。

編輯:更改代碼以在未剪裁時恢復原始字符串。

+1

很好的答案,謝謝;)。雖然實際上這可以做得更簡單:LabelUI本身會附加點,所以只需將反轉的'String'移交給'super.layoutCL(...)',然後再次反轉就可以實現這一點,而不需要任何條件語句或字符串操作。 – Paul