如何在JLabels中应用换行符



请看下面的代码

 JLabel menuItemTitle =  new JLabel("How to Find Waist");
     JLabel  menuItemDescription = new JLabel("Take the Neck measurement with your shoulders down and relaxed.n Looking straight ahead rap the tape around the smallest point, below the Larynx, and sloping down slightly toward the front.nTry a few spots to get the lowest number, and where it feels the most natural.");
        JPanel northPanel = new JPanel();
        northPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        northPanel.add(menuItemTitle);
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new FlowLayout());
        centerPanel.add(menuItemDescription);

        JDialog dialog = new JDialog();
        dialog.setLayout(new BorderLayout());
        dialog.add(northPanel,"North");
        dialog.add(centerPanel,"Center");
                dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                dialog.setLocationRelativeTo(null);
                dialog.pack();
                dialog.setVisible(true);

我已经添加了所需的转义字符,但它没有断行。如何在JLabels中添加换行符?我也用HTML试过上面的方法。但这也失败了。请帮助。

html版本
menuItemDescription = new JLabel("<html>Take the Neck measurement with your shoulders down and relaxed.</br> Looking straight ahead rap the tape around the smallest point, below the Larynx, and sloping down slightly toward the front.</br>Try a few spots to get the lowest number, and where it feels the most natural.</html>");

可以这样在JLabel中使用HTML代码

JLabel label = new JLabel("<html>Welcome to<br>Stackoverflow</html>");

最新更新