This question already has answers here:
Problems with newline in Graphics2D.drawString

(3个答案)


3年前关闭。




这正是我想要做的:
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中做到这一点?

以及如何为行创建制表符空间?

最佳答案

private void drawString(Graphics g,String text,int x,int y){
对于(字符串行:text.split(“\n”))
g.drawString(line,x,y + = g.getFontMetrics()。getHeight());
}

private void drawtabString(Graphics g,String text,int x,int y){
对于(字符串行: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,“Line 1\nLine 2”,120,120);
drawtabString(g2,“Line 1\tLine 2”,130,130);

关于java - Java SE中g.drawString中的换行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8676691/

10-10 08:57