【问题标题】:Split string into List<String> if too long如果太长,将字符串拆分为 List<String>
【发布时间】:2012-01-25 16:00:56
【问题描述】:

我有这个字符串:“这是我的很长的字符串,不能放在一行上” 而且我需要将它分成多行,以便它适合我需要的地方。

假设每行只有大约 15 个字母的空间,那么它应该如下所示:

"This is my very" 
"long String" 
"that wont fit"
"on one line"

我想把它拆分成List&lt;String&gt; 这样我就可以了

for(String s : lines) draw(s,x,y);

任何关于如何做到这一点的帮助都会得到帮助!

我渲染文本的方式是使用 Graphics.drawString()

这是我迄今为止尝试过的(可怕,我知道)

String diaText = "This is my very long String that wont fit on one line";
String[] txt = diaText.trim().split(" ");
int max = 23;
List<String> lines = new ArrayList<String>();
String s1 = "";
if (!(diaText.length() > max)) {
    lines.add(diaText);
} else {
    for (int i = 0; i < txt.length; i++) {
        String ns = s1 += txt[i] + " ";
        if (ns.length() < 23) {
            lines.add(s1);
            s1 = "";
        } else {
            s1 += txt[i] + "";
        }
    }
}
int yo = 0;
for (String s : lines) {
    Font.draw(s, screen, 70, 15 + yo, Color.get(-1, 555, 555,555));
    yo += 10;
}

【问题讨论】:

  • 请发布您尝试过的任何内容
  • 超过 15 个字符的单词怎么办?
  • 只是为了好奇,你是如何显示这个字符串的?我的意思是控制台、Swing、html 等。
  • 您正在使用的 GUI 框架很可能已经有这样的多行文本小部件,请使用提供的一个,因为拆分文本比计算字母的数量要困难得多;您还必须考虑到,在大多数字体中,M 比 i 宽得多。
  • @Bhushan 我将它添加到 OP EvertonAgner:我正在使用 Graphics.drawString() LieRyan:我不能使用任何小部件,只有 Graphics 类可以做的东西

标签: java string graphics awt


【解决方案1】:

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class LabelRenderTest {

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

            String title = "<html><body style='width: 200px; padding: 5px;'>"
                + "<h1>Do U C Me?</h1>"
                + "Here is a long string that will wrap.  "
                + "The effect we want is a multi-line label.";

                JFrame f = new JFrame("Label Render Test");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                BufferedImage image = new BufferedImage(
                    400,
                    300,
                    BufferedImage.TYPE_INT_RGB);
                Graphics2D imageGraphics = image.createGraphics();
                GradientPaint gp = new GradientPaint(
                    20f,
                    20f,
                    Color.red,
                    380f,
                    280f,
                    Color.orange);
                imageGraphics.setPaint(gp);
                imageGraphics.fillRect(0, 0, 400, 300);

                JLabel textLabel = new JLabel(title);
                textLabel.setSize(textLabel.getPreferredSize());

                Dimension d = textLabel.getPreferredSize();
                BufferedImage bi = new BufferedImage(
                    d.width,
                    d.height,
                    BufferedImage.TYPE_INT_ARGB);
                Graphics g = bi.createGraphics();
                g.setColor(new Color(255, 255, 255, 128));
                g.fillRoundRect(
                    0,
                    0,
                    bi.getWidth(f),
                    bi.getHeight(f),
                    15,
                    10);
                g.setColor(Color.black);
                textLabel.paint(g);
                Graphics g2 = image.getGraphics();
                g2.drawImage(bi, 20, 20, f);

                ImageIcon ii = new ImageIcon(image);
                JLabel imageLabel = new JLabel(ii);

                f.getContentPane().add(imageLabel);
                f.pack();
                f.setLocationByPlatform(true);

                f.setVisible(true);
            }
        });
    }
}

【讨论】:

    【解决方案2】:

    您可以考虑使用LineBreakMeasurer,它适用于图形环境中的此类事情。请查看 JavaDocs here,其中包含有关如何使用它的几个详细示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-17
      • 2012-03-05
      • 1970-01-01
      • 1970-01-01
      • 2011-06-20
      • 1970-01-01
      • 2021-06-08
      相关资源
      最近更新 更多