How can I create a JTextArea with a specified width and the smallest possible height required to display all the text?

前端 未结 4 573
青春惊慌失措
青春惊慌失措 2020-12-10 13:47

In all the examples that I can find that use a JTextArea, the height & width is known before constructing the JTextArea, and if the JText

4条回答
  •  天命终不由人
    2020-12-10 14:03

    import java.awt.*;
    import javax.swing.*;
    
    class FixedWidthLabel {
    
        public static void main(String[] args) {
    
            Runnable r = new Runnable() {
                public void run() {
                    String pt1 = "

    Label Height

    " + "

    Many Swing components support HTML 3.2 &" + " (simple) CSS. By setting a body width we can cause the " + " component to find the natural height needed to display" + " the component.

    " + "

    The body width in this text is set to " + ""; String pt3 = " pixels." + ""; JPanel p = new JPanel( new BorderLayout() ); JLabel l1 = new JLabel( pt1 + "125" + pt2 + "125" + pt3 ); p.add(l1, BorderLayout.WEST); JLabel l2 = new JLabel( pt1 + "200" + pt2 + "200" + pt3 ); p.add(l2, BorderLayout.CENTER); JOptionPane.showMessageDialog(null, p); } }; SwingUtilities.invokeLater(r); } }

提交回复
热议问题