2011-09-07 36 views

回答

4

我是使用方法 - createFont())嵌入字体并使用JLabel.setFont()来应用字体。

请尝试在HTML中设置它,如here所示。

+0

从我viewpoitn,使用HTML的主要问题是基础,大多数布局管理使用时下其中的损失(和这是一个很好的点恕我直言) – jfpoilpret

3

JLabel最初不是用于多行文字,从我记得的东西。您需要重写各种呈现方法来手动进行文本行分割。

也许你应该使用不可编辑的JTextArea,如果你想要多行标签。

1

,如果你想多行JComponents不使用的JLabel 1),那么你就必须寻找TextComponent因为是JTextArea,使其的JTextPane,JEditorPane中,如果should't可编辑然后myTextComponent#setEditable(false);

2)我从来没有看到问题HTML &字体&颜色在摇摆,例如:

enter image description here

import java.awt.Color; 
import java.awt.Font; 
import javax.swing.*; 

public class ButtonFg extends JFrame { 

    private static final long serialVersionUID = 1L; 

    public ButtonFg() { 
     JButton button = new JButton("<html> - myText <br>" 
       + " - myText <br>" 
       + " - myText <br>" 
       + " - myText </html>"); 
     button.setForeground(Color.blue); 
     button.setFont(new Font("Serif", Font.BOLD, 28)); 
     button.setFocusPainted(false); 
     add(button); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLocation(150, 150); 
     pack(); 
    } 

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

      @Override 
      public void run() { 
       new ButtonFg().setVisible(true); 
      } 
     }); 
    } 
} 
7


我不认为有直接(并且容易)做多行JLabel的方式没有反复到HTML。您可以改用JTextArea。

JTextArea textArea = new JTextArea(); 
textArea.setEditable(false); 
textArea.setLineWrap(true); 
textArea.setOpaque(false); 
textArea.setBorder(BorderFactory.createEmptyBorder()); 
add(textArea, BorderLayout.CENTER); 

它应该看起来差不多。如果您有不同的组件不同的字体,你可以添加以下行,以确保JTextArea中的字体是一个JLabel

textArea.setFont(UIManager.getFont("Label.font")); 

希望这有助于相同。

+0

我可以对齐JTextArea的文本? – Nauphal

+0

检查这篇文章http:// stackoverflow。COM /问题/ 3213045 /居中文本-IN-A-的JTextArea或 - JTextPane的水平文本对齐 – Serhiy

9

SwingX支持多标签:

JXLabel label = new JXLabel(); 
    label.setLineWrap(true); 
+0

当使用多行文本时,JXLabel是否具有“正确的”基准?请注意,“正确”可能对不同的人意味着不同的事情;-) – jfpoilpret

+0

@jfpoilpret - 不知道。什么是正确的基准? – kleopatra

+0

对于我来说,正确的基线将是标签第一行第一个字符的基线。但我相信你可以找到其他人更喜欢以标签的整个高度为中心的基线;其他人可能也希望基线成为标签中最后一行的第一个字符的基线。它实际上取决于人们希望标签在布局中对齐的方式。我认为我的第一个建议最有意义。 – jfpoilpret

1

大多数下面的代码是从BasicLabelUI和/或WindowsLabelUI拍摄,但我添加代码,使其与多行工作。这是我可以开始工作的最小代码量。您可以使用setSeparator在行之间设置分隔符,也可以通过更改LinesAndIndex实例的默认值来设置分隔符。我还没有做过这方面的广泛测试,但迄今为止它对我很有用。当使用HTML助记符不起作用,所以我创建了这个。如果您有更好的方法来完成此操作,请更正代码。

import com.sun.java.swing.plaf.windows.WindowsLabelUI; 
    import com.sun.java.swing.plaf.windows.WindowsLookAndFeel; 
    import java.awt.Color; 
    import java.awt.FontMetrics; 
    import java.awt.Graphics; 
    import java.awt.Insets; 
    import java.awt.Rectangle; 
    import java.util.ArrayList; 
    import java.util.List; 
    import javax.swing.Icon; 
    import javax.swing.JComponent; 
    import javax.swing.JLabel; 
    import javax.swing.UIManager; 
    import javax.swing.plaf.LabelUI; 
    import javax.swing.plaf.basic.BasicGraphicsUtils; 
    import javax.swing.plaf.basic.BasicHTML; 
    import javax.swing.text.View; 

    public class MultiLineLabelUI extends WindowsLabelUI { 
     private static MultiLineLabelUI multiLineLabelUI; 
     private LinesAndIndex lai = new LinesAndIndex(','); 
     private Rectangle paintIconR = new Rectangle(); 
     private Rectangle paintTextR = new Rectangle(); 
     public static LabelUI createUI(JComponent c) { 
      if (multiLineLabelUI == null) { 
       multiLineLabelUI = new MultiLineLabelUI(); 
      } 
      return multiLineLabelUI; 
     } 
     private int getBaseline(JComponent c, int y, int ascent, int w, int h) { 
      View view = (View) c.getClientProperty(BasicHTML.propertyKey); 
      if (view != null) { 
       int baseline = BasicHTML.getHTMLBaseline(view, w, h); 
       if (baseline < 0) { 
        return baseline; 
       } 
       return y + baseline; 
      } 
      return y + ascent; 
     } 
     public char getSeparator() { 
      return lai.getSeparator(); 
     } 
     public void setSeparator(char ch) { 
      lai.setSeparator(ch); 
     } 
     private String layout(JLabel label, FontMetrics fm, 
       int width, int height, int lineCnt, int curLine, String text) { 
      Insets insets = label.getInsets(null); 
      Icon icon = (label.isEnabled()) ? label.getIcon() 
        : label.getDisabledIcon(); 
      Rectangle paintViewR = new Rectangle(); 
      paintViewR.width = width - (insets.left + insets.right); 
      paintViewR.height = (height - (insets.top + insets.bottom))/lineCnt; 
      paintViewR.x = insets.left; 
      paintViewR.y = insets.top + (paintViewR.height * curLine); 
      paintIconR.x = 0; 
      paintIconR.y = 0; 
      paintIconR.width = 0; 
      paintIconR.height = 0; 
      paintTextR.x = 0; 
      paintTextR.y = 0; 
      paintTextR.width = 0; 
      paintTextR.height = 0; 
      return layoutCL(label, fm, text, icon, paintViewR, paintIconR, 
        paintTextR); 
     } 
     protected void paintEnabledText(JLabel l, Graphics g, 
       String s, int textX, int textY, int curLine) { 
      int mnemonicIndex = lai.getMnemonicIndex(); 
      // W2K Feature: Check to see if the Underscore should be rendered. 
      if (WindowsLookAndFeel.isMnemonicHidden() == true) { 
       mnemonicIndex = -1; 
      } 
      if (curLine != lai.getMnemonicLineIndex()) { 
       mnemonicIndex = -1; 
      } 

      g.setColor(l.getForeground()); 
      BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemonicIndex, 
        textX, textY); 
     } 
     protected void paintDisabledText(JLabel l, Graphics g, 
       String s, int textX, int textY, int curLine) { 
      int mnemonicIndex = lai.getMnemonicIndex(); 
      // W2K Feature: Check to see if the Underscore should be rendered. 
      if (WindowsLookAndFeel.isMnemonicHidden() == true) { 
       mnemonicIndex = -1; 
      } 
      if (curLine != lai.getMnemonicLineIndex()) { 
       mnemonicIndex = -1; 
      } 
      if (UIManager.getColor("Label.disabledForeground") instanceof Color 
        && UIManager.getColor("Label.disabledShadow") instanceof Color) { 
       g.setColor(UIManager.getColor("Label.disabledShadow")); 
       BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemonicIndex, 
         textX + 1, textY + 1); 
       g.setColor(UIManager.getColor("Label.disabledForeground")); 
       BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemonicIndex, 
         textX, textY); 
      } else { 
       Color background = l.getBackground(); 
       g.setColor(background.brighter()); 
       BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemonicIndex, 
         textX + 1, textY + 1); 
       g.setColor(background.darker()); 
       BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemonicIndex, 
         textX, textY); 
      } 
     } 
     @Override 
     public void paint(Graphics g, JComponent c) { 
      JLabel label = (JLabel) c; 
      String text = label.getText(); 
      Icon icon = (label.isEnabled()) 
        ? label.getIcon() 
        : label.getDisabledIcon(); 

      if ((icon == null) && (text == null)) { 
       return; 
      } 
      char mnemonic = (char) label.getDisplayedMnemonic(); 
      lai.splitText(text, mnemonic); 
      List<String> lines = lai.getLines(); 

      FontMetrics fm = label.getFontMetrics(g.getFont()); 
      String[] clippedText = new String[lines.size()]; 
      for (int i = 0; i < lines.size(); i++) { 
       clippedText[i] = layout(label, fm, c.getWidth(), c.getHeight(), 
         lines.size(), i, lines.get(i)); 

       if (icon != null && i == 0) { 
        icon.paintIcon(c, g, paintIconR.x, paintIconR.y); 
       } 

       if (text != null) { 
        int textX = paintTextR.x; 
        int textY = paintTextR.y + fm.getAscent(); 

        if (label.isEnabled()) { 
         paintEnabledText(label, g, clippedText[i], textX, 
           textY, i); 
        } else { 
         paintDisabledText(label, g, clippedText[i], textX, 
           textY, i); 
        } 
       } 
      } 
     } 
     @Override 
     public int getBaseline(JComponent c, int width, int height) { 
      super.getBaseline(c, width, height); 
      JLabel label = (JLabel) c; 
      String text = label.getText(); 
      if (text == null || "".equals(text) || label.getFont() == null) { 
       return -1; 
      } 
      char mnemonic = (char) label.getDisplayedMnemonic(); 
      lai.splitText(text, mnemonic); 
      List<String> lines = lai.getLines(); 
      FontMetrics fm = label.getFontMetrics(label.getFont()); 
      String[] clippedText = new String[lines.size()]; 
      for (int i = 0; i < lines.size(); i++) { 
       clippedText[i] = layout(label, fm, width, height, lines.size(), i, 
         lines.get(i)); 
      } 
      return getBaseline(label, paintTextR.y, fm.getAscent(), 
        paintTextR.width, paintTextR.height); 
     } 

     private static class LinesAndIndex { 
      private char sep; 
      private List<String> lines; 
      private int mnemonicLineIndex; 
      private int mnemonicIndex; 
      LinesAndIndex(char sep) { 
       mnemonicLineIndex = -1; 
       mnemonicIndex = -1; 
       lines = new ArrayList<String>(); 
       this.sep = sep; 
      } 
      public char getSeparator() { 
       return sep; 
      } 
      public void setSeparator(char sep) { 
       this.sep = sep; 
      } 
      public List<String> getLines() { 
       return lines; 
      } 
      public int getMnemonicLineIndex() { 
       return mnemonicLineIndex; 
      } 
      public int getMnemonicIndex() { 
       return mnemonicIndex; 
      } 
      public void splitText(String text, char mnemonic) { 
       if (text == null) { 
        return; 
       } 
       lines.clear(); 
       mnemonicLineIndex = -1; 
       mnemonicIndex = -1; 
       char um = Character.toUpperCase(mnemonic); 
       char lm = Character.toLowerCase(mnemonic); 
       int umi = Integer.MAX_VALUE; 
       int lmi = Integer.MAX_VALUE; 
       int umli = -1; 
       int lmli = -1; 
       for (int i = 0, j = 0, k = 0; i < text.length(); i++) { 
        if (text.charAt(i) == sep) { 
         lines.add(text.substring(j, i)); 
         j = i + 1; 
         k++; 
        } else if (text.charAt(i) == um) { 
         if (umi == Integer.MAX_VALUE) { 
          umi = i - j; 
          umli = k; 
         } 
        } else if (text.charAt(i) == lm) { 
         if (lmi == Integer.MAX_VALUE) { 
          lmi = i - j; 
          lmli = k; 
         } 
        } 
        if (i == text.length() - 1) { 
         lines.add(text.substring(j, i + 1)); 
        } 
       } 
       mnemonicLineIndex = (lmi < umi) ? lmli : umli; 
       mnemonicIndex = (lmi < umi) ? lmi : umi; 
      } 
     } 

    } 
相关问题