2012-06-05 79 views
0

我有一個寬度80000的非常長的JLabel。它坐落在寬度爲80000的面板中。但是,標籤以朝向面板中間的「...」結束,意味着它被切斷。我將JLabel的最大尺寸,最小尺寸,尺寸和首選尺寸設置爲80000.它所在的面板位於JScrollPane內。JLabel寬度看起來不正確

任何想法爲什麼JLabel似乎沒有它實際上認爲它的寬度?

+4

爲了更好地幫助更快,張貼...哦,內夫呃介意。 –

+0

@Andrew湯普森請whaaaaat'發佈......哦,沒關係', – mKorbel

+2

@mKorbel已經在OP的其中一個問題上提到過它。已被忽略。已經進入更有成效的問題。 –

回答

1

這裏有一個辦法:

可以repaintJLabel與自己的自定義圖形或文字:

protected void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    g.drawImage(foregroundImage, x, y, this); 
    g.drawString("YOUR BIG STRING"); 
    jlabel.setBounds(0,0,100,100); //set it to your JLabel bounds... 
    jlabel.paint(g); 
} 
1

根據Andrew Thompson的評論 - 發佈代碼示例,演示您的問題以獲得更好的答案。

如果您遇到讓你的標籤,以顯示字符串內容的問題......

1)使用的FontMetrics弄清楚什麼大小您的字符串實際上必須這樣做。

Graphics g; //initialize this with your component's Graphics, as passed in to paintComponent 
Rectangle2D bounds = g.getFontMetrics().getStringBounds(reallyLongString, g) 

2)請記住,標籤的邊距會有一些可變緩衝。這應該等於其插頁,儘管可能有其他項目會將標籤所需的大小增加到字符串本身的大小以外。

Insets i = getInsets(); 

3)1和2之後,你應該能夠找出您的標籤必須大小:

minWidth = bounds.getWidth() + i.left + i.right; 

4)如果這還不夠,再有就是我的一些其他因素錯過了。您總是可以通過實驗測試來確定閾值 - 逐漸增加標籤的大小,直到它停止向您提供橢圓。

2

例如Font and String

enter image description here

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 

public class JLabelIntoJScrollPane { 

    private JFrame frame = new JFrame(); 

    public JLabelIntoJScrollPane() { 
     AlphaChars aChars = new AlphaChars(); 
     String str = aChars.getNext(80000); 
     JTextArea textarea = new JTextArea(str); 
     textarea.setLineWrap(true); 
     JScrollPane jScrollPane = new JScrollPane(textarea); 

     JLabel label = new JLabel(str); 
     label.setPreferredSize(new Dimension(550000, 60)); 
     // replace new Dimension(int, int) with proper way 
     // I'd be use TextLayout or one of three methods 
     // how to get size from Font and String 

     frame.pack(); 

     final JPanel panel = new JPanel(); 
     panel.setLayout(new BorderLayout()); 
     panel.add(label); 

     JScrollPane jScrollPane1 = new JScrollPane(panel); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(jScrollPane, BorderLayout.CENTER); 
     frame.add(jScrollPane1, BorderLayout.SOUTH); 

     frame.setSize(400, 300); 
     frame.setVisible(true); 

     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       System.out.println(panel.getPreferredSize()); 
      } 
     }); 
    } 

    private class AlphaChars { 

     public static final int MIN_LENGTH = 80000; 
     private java.util.Random rand = new java.util.Random(); 
     private char[] AlphaChars = { 
      'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 
      'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 
      'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 
      'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 
      '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '+', '-', '*', '/', '<', '>', '&', 
      '#', '@', '{', '}', '?', ':', '_', '"', '!', ')', '('}; 

     public String getNext() { 
      StringBuilder strbuf = new StringBuilder(); 
      for (int i = 0; i < MIN_LENGTH; i++) { 
       strbuf.append(getAlphaChars()[getRand().nextInt(getAlphaChars().length)]); 
      } 
      return strbuf.toString(); 
     } 

     public String getNext(int reqLenght) { 
      StringBuilder strbuf = new StringBuilder(); 
      for (int i = 0; i < reqLenght; i++) { 
       strbuf.append(getAlphaChars()[getRand().nextInt(getAlphaChars().length)]); 
      } 
      return strbuf.toString(); 
     } 

     public java.util.Random getRand() { 
      return rand; 
     } 

     public void setRand(java.util.Random aRand) { 
      rand = aRand; 
     } 

     public char[] getAlphaChars() { 
      return AlphaChars; 
     } 

     public void setAlphaChars(char[] aAlphaChars) { 
      AlphaChars = aAlphaChars; 
     } 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new JLabelIntoJScrollPane(); 
      } 
     }); 
    } 
}