JLabel设置为文本列表项

时间:2017-01-17 16:00:39

标签: java stringbuilder

我有一个包含String的列表,我正在尝试将其项目设置为JLabel文本,唯一的问题是结果是单行

JButton btnSearch = new JButton("Search");
btnSearch.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent arg0) {
        String fileName = textField.getText();
        StringBuilder result = new StringBuilder();
        List <String> searchResult = new Search().cautaFisiere("C:\\AdwCleaner", fileName);

        for (int x = 0; x < searchResult.size(); x++) {
            result.append(searchResult.get(x)).append("\n");

        }
        lblNewLabel.setText(result.toString());
    }

});

正如您所看到的,我尝试将项目作为新行追加但没有结果。我也试过append(System.getProperty("line.separator"))但仍然没有结果,文字仍然显示在一行

1 个答案:

答案 0 :(得分:1)

正如Multiline text in JLabel中所述,只需使用HTML代码<br>创建一个新行,但它必须位于<html></html>代码中。

JButton btnSearch = new JButton("Search");
btnSearch.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent arg0) {
        String fileName = textField.getText();
        StringBuilder result = new StringBuilder();
        List <String> searchResult = new Search().cautaFisiere("C:\\AdwCleaner", fileName);

        for (int x = 0; x < searchResult.size(); x++) {
            result.append(searchResult.get(x)).append("<br>");
        }
        lblNewLabel.setText("<html>" + result.toString() + "</html>");
    }
});