如何在不使用HTML的情况下向JLabel添加换行符

时间:2011-09-07 10:00:50

标签: java swing jlabel multiline

如何向JLabel添加换行符?我知道如果我使用简单的HTML,它会起作用。但是如果我使用html,JLabel没有显示嵌入应用程序的字体。我使用方法createFont()嵌入字体并使用JLabel.setFont()来应用字体。

6 个答案:

答案 0 :(得分:9)

SwingX支持多行标签:

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

答案 1 :(得分: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"));

希望这有帮助。

答案 2 :(得分:4)

  

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

请尝试在HTML中进行设置,如图here所示。

答案 3 :(得分:3)

我记得,JLabel最初不是用于多行文字的。您需要覆盖各种渲染方法以手动进行文本行拆分。

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

答案 4 :(得分:1)

1)如果你想在不使用JLabel的情况下使用Multiline JComponents,那么你必须查找TextComponent和JTextArea,JTextPane,JEditorPane,如果不能编辑,那么myTextComponent#setEditable(false);

2)我从未发现Html& amp;字体& Swing中的颜色,例如:

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);
            }
        });
    }
}

答案 5 :(得分: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;
            }
        }

    }