2

请看下面的代码

 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 中添加 LineBreaks?我也尝试过使用 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>");
4

1 回答 1

5

您可以通过这种方式在 JLabel 中使用 HTML 代码

JLabel label = new JLabel("<html>Welcome to<br>Stackoverflow</html>");
于 2013-01-02T14:36:20.307 回答