2013-01-11 14 views
8

मैं ग्राफिक्स का उपयोग करके स्ट्रिंग खींचने के लिए drawString() विधि का उपयोग करता हूं, लेकिन मैं अपने पाठ को आयत में केंद्रित करना चाहता हूं। मैं कैसे करूं?आयत में जावा सेंटर टेक्स्ट

+1

आपका शीर्षक राज्यों, आप उपयोग कर रहे 'JTextField', लेकिन आपका सवाल। तो कौन सा सच है? – Mordechai

+0

आपका शीर्षक है: * टेक्स्टफील्ड में जावा सेंटर टेक्स्ट * लेकिन आपकी पोस्ट कहती है: * मैं ग्राफिक्स पर स्ट्रिंग खींचने के लिए 'drawString()' विधि का उपयोग करता हूं, लेकिन मैं अपने पाठ को आयत में केंद्रित करना चाहता हूं। मुझे यह कैसे करना चाहिए? * यह कैसे –

+0

से मेल खाता है मेरे प्रश्न को सही करने के लिए: मेरे पास जेएफआरएएम में कहीं आयताकार है .... मैं उस आयताकार में केंद्रित स्ट्रिंग को आकर्षित करना चाहता हूं। मैं कैसे करूं? –

उत्तर

27

मैं एक JPanel

 Graphics2D g2d = (Graphics2D) g; 
     FontMetrics fm = g2d.getFontMetrics(); 
     Rectangle2D r = fm.getStringBounds(stringTime, g2d); 
     int x = (this.getWidth() - (int) r.getWidth())/2; 
     int y = (this.getHeight() - (int) r.getHeight())/2 + fm.getAscent(); 
     g.drawString(stringTime, x, y); 
+2

पाठ की वजह से टेक्स्ट को लंबवत रूप से केंद्रित करना थोड़ा अधिक शामिल है चढ़ाई और जिस तरह से पाठ प्रस्तुत किया जाता है। स्मृति से, यह वाई = ((getHeight() - fm.getHeight())/2) + fm.getAscent(); ... घटक केंद्र रेखा में एक रेखा खींचने का प्रयास करें और दो विधि की तुलना करें, अगर मेरी याददाश्त अच्छी तरह से मेरी सेवा कर रही है, तो मेरा मानना ​​है कि यह अधिक उचित ढंग से केंद्रित होगा ... +1 – MadProgrammer

+0

आप सही हैं। मैंने अपना जवाब तय कर दिया। –

+0

आप [इस] के माध्यम से पढ़ना भी पसंद कर सकते हैं (http://stackoverflow.com/questions/1055851/how-do-you-draw-a-string-centered-vertically-in-java) – MadProgrammer

15

केंद्रित पाठ पर केंद्र पाठ करने के लिए इस का उपयोग किया है "विकल्प" का एक बहुत है। क्या आप पूरी तरह से आधार रेखा पर आधारित हैं ??

enter image description here

व्यक्तिगत रूप से, मैं पूर्ण केंद्र स्थिति को पसंद करते हैं, लेकिन यह आप क्या कर रहे पर निर्भर करता है ...

public class CenterText { 

    public static void main(String[] args) { 
     new CenterText(); 
    } 

    public CenterText() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Test"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 

     }); 
    } 

    public class TestPane extends JPanel { 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 200); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      int height = getHeight(); 
      String text = "This is a test xyx"; 

      g.setColor(Color.RED); 
      g.drawLine(0, height/2, getWidth(), height/2); 

      FontMetrics fm = g.getFontMetrics(); 
      int totalWidth = (fm.stringWidth(text) * 2) + 4; 

      // Baseline 
      int x = (getWidth() - totalWidth)/2; 
      int y = (getHeight() - fm.getHeight())/2; 
      g.setColor(Color.BLACK); 

      g.drawString(text, x, y + ((fm.getDescent() + fm.getAscent())/2)); 

      // Absolute... 
      x += fm.stringWidth(text) + 2; 
      y = ((getHeight() - fm.getHeight())/2) + fm.getAscent(); 
      g.drawString(text, x, y); 

     } 

    } 

} 
+0

+1 इसके विपरीत। – trashgod

संबंधित मुद्दे