如何格式化文本?

时间:2012-09-29 13:01:51

标签: java text string-formatting

解决

嗨,大家好,如果我有一个这样的程序:

import java.awt.*;
import javax.swing.*;

public class Tab extends JFrame
{
    public Tab()
    {
        super ("Swimming Pool Calculator");

        JTabbedPane tab = new JTabbedPane();

        // constructing the first panel
        JLabel l1 = new JLabel( "Welcome to blah,blah!", SwingConstants.CENTER); 

        JPanel p1 = new JPanel();
        p1.add( l1 );
        tab.addTab( "Tab#1", null, p1, " Panel #1" );

        // constructing the second panel
        JLabel l2 = new JLabel("Welcome to JTabbedPaneDemo",SwingConstants.CENTER);
        JPanel p2 = new JPanel();
        p2.setBackground( Color.blue );
        p2.add( l2 );
        tab.addTab( "Tab#2", null, p2, " Panel #2" );

    }
}

我希望JLabel l1中的措辞说:

                                     "Welcome! 
                              Thank you for using the
                              Swimming Pool Calculator.

我该怎么做?

我尝试将\n用于新行,但这对我来说效果不佳。

1 个答案:

答案 0 :(得分:0)

Swing组件理解一些HTML

JLabel l1 = new JLabel( "<html><center>Welcome!<br>Thank you for using the<br>Swimming Pool Calculator.</center>");