【问题标题】:Break lines in g.drawString in Java SE [duplicate]Java SE中g.drawString中的换行[重复]
【发布时间】:2012-01-30 09:25:32
【问题描述】:

这正是我想要做的:

Graphics2D g2 = (Graphics2D) g;
g2.setFont(new Font("Serif", Font.PLAIN, 5));
g2.setPaint(Color.black);
g2.drawString("Line 1\nLine 2", x, y);

该行打印如下:

Line1Line2

我想要这样:

Line1
Line2

如何在drawString 中执行此操作?

以及如何为一行设置制表符空间??

【问题讨论】:

  • 通过this thread 看到LabelRenderTest.java。诀窍是在 JLabel 等样式化组件中使用 HTML,然后渲染标签。
  • 如何使用 JTestArea?

标签: java drawstring


【解决方案1】:
私人无效drawString(图形g,字符串文本,int x,int y){ for (String line : text.split("\n")) g.drawString(line, x, y += g.getFontMetrics().getHeight()); } 私人无效drawtabString(图形g,字符串文本,int x,int y){ for (String line : text.split("\t")) g.drawString(line, x += g.getFontMetrics().getHeight(), y); } Graphics2D g2 = (Graphics2D) g; g2.setFont(new Font("Serif", Font.PLAIN, 5)); g2.setPaint(Color.black); drawString(g2,"第 1 行\n第 2 行", 120, 120); drawtabString(g2,"Line 1\tLine 2", 130, 130);

【讨论】:

  • 以及如何为此设置选项卡空间??
  • private void drawtabString(Graphics g, String text, int x, int y) { for (String line : text.split("\t")) g.drawString(line, x += g .getFontMetrics().getHeight(), y); }
  • 你能告诉我如何将它们与代码结合起来吗? (换行符和制表符空间)
  • 正在做一些可能会利用您的解决方案的事情,String text 在这里代表什么?
【解决方案2】:

这是我用来在JPanel 中绘制文本的sn-p,带有标签扩展和多行:

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;

public class Scratch {
    public static void main(String argv[]) {
        JFrame frame = new JFrame("FrameDemo");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel() {
            @Override
            public void paint(Graphics graphics) {
                graphics.drawRect(100, 100, 1, 1);
                String message =
                        "abc\tdef\n" +
                        "abcx\tdef\tghi\n" +
                        "xxxxxxxxdef\n" +
                        "xxxxxxxxxxxxxxxxghi\n";
                int x = 100;
                int y = 100;
                FontMetrics fontMetrics = graphics.getFontMetrics();
                Rectangle2D tabBounds = fontMetrics.getStringBounds(
                        "xxxxxxxx",
                        graphics);
                int tabWidth = (int)tabBounds.getWidth();
                String[] lines = message.split("\n");
                for (String line : lines) {
                    int xColumn = x;
                    String[] columns = line.split("\t");
                    for (String column : columns) {
                        if (xColumn != x) {
                            // Align to tab stop.
                            xColumn += tabWidth - (xColumn-x) % tabWidth;
                        }
                        Rectangle2D columnBounds = fontMetrics.getStringBounds(
                                column,
                                graphics);
                        graphics.drawString(
                                column,
                                xColumn,
                                y + fontMetrics.getAscent());
                        xColumn += columnBounds.getWidth();
                    }
                    y += fontMetrics.getHeight();
                }
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 200);
            }
        };
        frame.getContentPane().add(panel, BorderLayout.CENTER);

        frame.pack();

        frame.setVisible(true);    }
}

Utilities.drawTabbedText() 看起来确实很有希望,但我不知道它需要什么作为输入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-09
    • 2012-08-02
    • 1970-01-01
    相关资源
    最近更新 更多