多线JLabel没有正确包装

时间:2016-04-07 12:47:10

标签: java swing user-interface jlabel miglayout

我创建了一个SSCCE来证明我的问题。我有一个包装精美的多行JLabel,但是当打包到JFrame中时,由于换行所需的额外高度没有被考虑在内,因此窗口需要滚动或调整大小。这是创建时的窗口:

enter image description here

我更喜欢用正确的大小初始化窗口,如下所示:

enter image description here

我已尝试过填充,增长,跨度,最小值,首选值,最大值等的所有组合,但我无法获得所需的行为。我也尝试过JTextAreas,但他们给了我比JLabels更多的问题。

这是我的单文件SSCCE:

package SSCCE;

import net.miginfocom.swing.MigLayout;
import java.awt.*;
import javax.swing.*;
import static javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
import static javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;

public class App
{
    public static void main( String[] args ) {
        App program = new App();
        SwingUtilities.invokeLater(program::run);
    }

    private void run() {
        JFrame w = new JFrame();
        w.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);



        // "main" shall contain everything to be packed into the JFrame.
        JPanel main = new JPanel();
        main.setLayout(new MigLayout(
                "debug",
                "[][][][][]", // cols
                "[][][fill, grow]" // rows
        ));

        // First row (search UI)
        main.add(new JLabel("Chinese character:"), "spany 2");
        main.add(new JTextField("type here"), "growx, spanx 3");
        main.add(new JButton("Search"), "spany 2, wrap");

        // Second row (checkboxes)
        main.add(new JCheckBox("box1"));
        main.add(new JCheckBox("box2"));
        main.add(new JCheckBox("box3"), "wrap");



        // Third row setup: a 'content' JPanel to be viewed by a JScrollPane, the latter which is to be added to the main JPanel.
        JPanel content = new JPanel();
        content.setLayout(new MigLayout(
                "debug, wrap 4",
                "[left][][][fill, grow]", // cols
                "[top][fill]" // rows
        ));

        // First row of 'content' JPanel: The multi-line JLabel which wraps beautifully but doesn't pack properly.
        JLabel tutorial = new JLabel("<html>" +
                "**WELCOME**<br>This is an English-language interface for the Chinese character etymology search engine     'XiaoXue', " +
                "produced by the Taiwanese academic institution Academia Sinica (中央研究院).<br> NOTE: if the text     between " +
                "those brackets doesn't display, Chinese language support is not configured on your computer." +
                "<br><br>" +
                "**HOW TO USE**<br>Simply enter a single Chinese character into the search field and press 'enter' or     the " +
                "Search button. The checkboxes below the search field are non-operational and merely proof-of-concept     for a future capability to " +
                "combine resources from different databases.<br>NOTE: supports traditional Chinese characters (and those     shared with Japanese), " +
                "but not simplified."
                + "</html>");
        tutorial.setPreferredSize(new Dimension(1,1)); // This encourages the JLabel to wrap.
        content.add(tutorial, "grow, spanx 4");

        // Second row of 'content' JPanel:
        content.add(new JLabel("label1"), "");
        content.add(new JLabel("label2"), "");
        content.add(new JLabel("label3"), "");
        content.add(new JLabel("label4"), "");

        // JScrollPane view of the 'content' JPanel
        JScrollPane contentScrollPane = new JScrollPane(content, VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_ALWAYS);
        contentScrollPane.getVerticalScrollBar().setUnitIncrement(20);
        contentScrollPane.getHorizontalScrollBar().setUnitIncrement(50);

        // Third row finally added to main JPanel.
        main.add(contentScrollPane, "span, width :100%:");



        w.add(main);
        w.pack();
        w.setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:-1)

您无需更改JPanel中的所有内容。您可以从一开始就更改JFrame的大小。尝试使用w.setSize(200, 400);或类似的东西来更改JFrame的大小。

另一个解决方案是使用w.pack(),但我不确定这是否有效。