2011-12-30 74 views
4

這正是我想要做的事:打破g.drawString線在Java SE

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

以及我如何做一個行的標籤空間?

+0

請參閱'LabelRenderTest.java'通過[this thread](http://stackoverflow.com/a/7775713/418556)。訣竅是在樣式化的組件中使用HTML,比如'JLabel',然後渲染標籤。 – 2011-12-30 06:16:02

+0

使用JTextArea。 – camickr 2011-12-30 06:55:52

+0

如何使用JTestArea進行此操作? – ArMEd 2011-12-30 07:00:44

回答

5
 





     private void drawString(Graphics g, String text, int x, int y) { 
       for (String line : text.split("\n")) 
        g.drawString(line, x, y += g.getFontMetrics().getHeight()); 
      } 

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


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

+0

以及如何可以爲此選項卡空間? – ArMEd 2011-12-30 07:05:34

+0

http://www.java2s.com/Tutorial/Java/0300__SWT-2D-Graphics/Drawstringanddisplaynewlinesandtabsasnonprintablecharacters.htm – Rupok 2011-12-30 07:09:28

+0

我無法理解這一點。 – ArMEd 2011-12-30 07:17:27

0

這是我用來繪製文本在JPanel與選項卡擴展和多行的代碼段:

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()是有希望的,但我無法弄清楚它需要什麼作爲輸入。