2009-06-26 14 views
8

मैं एक ऐसा संवाद बनाना चाहता हूं जिसमें किसी प्रकार का टेक्स्ट तत्व (जेएलएबल/जेटीक्स्टएरिया इत्यादि) है जो बहु पंक्तिबद्ध है और शब्दों को लपेटता है। मैं चाहता हूं कि संवाद एक निश्चित चौड़ाई का हो, लेकिन टेक्स्ट कितना बड़ा है इस पर निर्भर करता है। मैं इस कोड है:डायलॉग का आकार बदलने के लिए निश्चित चौड़ाई वाले मल्टी लाइन टेक्स्ट की ऊंचाई प्राप्त करें

import static javax.swing.GroupLayout.DEFAULT_SIZE; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*; 

public class TextSizeProblem extends JFrame { 
    public TextSizeProblem() { 

    String dummyString = ""; 
    for (int i = 0; i < 100; i++) { 
     dummyString += " word" + i; //Create a long text 
    } 
    JLabel text = new JLabel(); 
    text.setText("<html>" + dummyString + "</html>"); 

    JButton packMeButton = new JButton("pack"); 
    packMeButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
     pack(); 
     } 
    }); 

    GroupLayout layout = new GroupLayout(this.getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setVerticalGroup(layout.createParallelGroup() 
     .addComponent(packMeButton) 
     .addComponent(text) 
    ); 
    layout.setHorizontalGroup(layout.createSequentialGroup() 
     .addComponent(packMeButton) 
     .addComponent(text, DEFAULT_SIZE, 400, 400) //Lock the width to 400 
    ); 

    pack(); 
    } 

    public static void main(String args[]) { 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
     JFrame frame = new TextSizeProblem(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
     } 
    }); 
    } 
} 

जब कार्यक्रम यह इस तरह दिखता है चल रहा है: alt text http://lesc.se/stackoverflow/multiline_size_1.png

लेकिन मैं संवाद इस तरह दिखना चाहते हैं (जैसा कि जब आप पैक बटन दबाएँ): alt text http://lesc.se/stackoverflow/multiline_size_2.png

मुझे लगता है कि समस्या यह है कि लेआउट प्रबंधक स्क्रीन पर प्रदर्शित करने से पहले टेक्स्ट की उचित ऊंचाई निर्धारित करने में सक्षम नहीं था। मैंने विभिन्न मान्य(), अवैध(), validateTree() आदि की कोशिश की है लेकिन सफल नहीं हुए हैं।

उत्तर

4

मैं अपने समस्या का समाधान मिल गया। एक JTextArea साथ JLabel की जगह द्वारा:

pack(); 
layout.invalidateLayout(this.getContentPane()); 
pack(); 
:

JTextArea text = new JTextArea(); 
text.setText(dummyString); 
text.setLineWrap(true); 
text.setWrapStyleWord(true); 

और लेआउट प्रबंधक को एक मंगलाचरण से पैक() का पालन किया बुला घटक फिर से एक और पैक के बाद लेआउट करने के लिए

इससे लेआउट प्रबंधक चौड़ाई को अनुकूलित करने का कारण बन जाएगा।

पूरा कोड:

import static javax.swing.GroupLayout.DEFAULT_SIZE; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*; 

public class TextSizeProblem3 extends JFrame { 
    public TextSizeProblem3() { 

    String dummyString = ""; 
    for (int i = 0; i < 100; i++) { 
     dummyString += " word" + i; //Create a long text 
    } 
    JTextArea text = new JTextArea(); 
    text.setText(dummyString); 
    text.setLineWrap(true); 
    text.setWrapStyleWord(true); 

    JButton packMeButton = new JButton("pack"); 
    packMeButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
     pack(); 
     } 
    }); 

    GroupLayout layout = new GroupLayout(this.getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setVerticalGroup(layout.createParallelGroup() 
     .addComponent(packMeButton) 
     .addComponent(text) 
    ); 
    layout.setHorizontalGroup(layout.createSequentialGroup() 
     .addComponent(packMeButton) 
     .addComponent(text, DEFAULT_SIZE, 400, 400) //Lock the width to 400 
    ); 

    pack(); 
    layout.invalidateLayout(this.getContentPane()); 
    pack(); 
    } 

    public static void main(String args[]) { 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
     JFrame frame = new TextSizeProblem3(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
     } 
    }); 
    } 
} 

(आप कुछ अनुकूलन (सीमा, रंग आदि जोड़ सकते हैं) तो यह सिर्फ JLabel की तरह दिखता है, लेकिन मैं नहीं दिखाए हैं कि)

+0

क्या यह एचटीएमएल टैग प्रस्तुत करता है? नहीं! – Soley

10

यहां आपके कोड का एक अनुकूलन है, जो आप चाहते हैं। लेकिन लेबल के आकार की गणना करने और इसके पसंदीदा आकार को सेट करने के लिए इसे एक छोटी सी चाल की आवश्यकता है।

I found the solution here

import static javax.swing.GroupLayout.DEFAULT_SIZE; 

import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*; 
import javax.swing.text.View; 

public class TextSizeProblem extends JFrame { 
    public TextSizeProblem() { 

     String dummyString = ""; 
     for (int i = 0; i < 100; i++) { 
      dummyString += " word" + i; // Create a long text 
     } 
     JLabel text = new JLabel(); 
     text.setText("<html>" + dummyString + "</html>"); 

     Dimension prefSize = getPreferredSize(text.getText(), true, 400); 

     JButton packMeButton = new JButton("pack"); 
     packMeButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       pack(); 
      } 
     }); 



     GroupLayout layout = new GroupLayout(this.getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setVerticalGroup(layout.createParallelGroup().addComponent(packMeButton) 
       .addComponent(text,DEFAULT_SIZE, prefSize.height, prefSize.height)); 
     layout.setHorizontalGroup(layout.createSequentialGroup().addComponent(packMeButton) 
       .addComponent(text, DEFAULT_SIZE, prefSize.width, prefSize.width) // Lock the width to 400 
       ); 

     pack(); 
    } 

    public static void main(String args[]) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       JFrame frame = new TextSizeProblem(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    private static final JLabel resizer = new JLabel(); 

    /** 
    * Returns the preferred size to set a component at in order to render an html string. You can 
    * specify the size of one dimension. 
    */ 
    public static java.awt.Dimension getPreferredSize(String html, boolean width, int prefSize) { 

     resizer.setText(html); 

     View view = (View) resizer.getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey); 

     view.setSize(width ? prefSize : 0, width ? 0 : prefSize); 

     float w = view.getPreferredSpan(View.X_AXIS); 
     float h = view.getPreferredSpan(View.Y_AXIS); 

     return new java.awt.Dimension((int) Math.ceil(w), (int) Math.ceil(h)); 
    } 
} 
+0

हाँ, यह समाधान काम करता है! –

+0

ईश्वर की इच्छा है कि मैं इस टन के समय वोट दे सकता हूं। – Burimi

5

मैं इस बारे में सोच क्या है आप हैं:

JLabel label = new JLabel("<html><div style=\"width:200px;\">Lots of text here...</div></html>"); 
// add the label to some Container. 

यह 200 पिक्सल चौड़ा और स्वचालित रूप से किया जा रहा पाठ फिट करने के लिए ऊंचाई को समायोजित करने के लिए JLabel को प्रतिबंधित करेगा।

+0

[LabelRenderTest.java] में एक उदाहरण देखें (http://stackoverflow.com/questions/5853879/java-swing-obtain-image-of-jframe/5853992#5853992) (ऊपर दिखाया गया है)। –

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