在JPane上正确显示多行

时间:2015-08-10 10:12:49

标签: java swing

我有一个关于多行支持JPane的问题:如果内容只有一行,JPane显示正确。但是从2行或更多行,内容移动到顶部,它显示不是预期的。 有1行: enter image description here

2行或更多: enter image description here

代码在这里:

    String test=" Deutsch, Bahasa Indonesia,Italiano &nbsp; <a href=\"\">  edit</a>";
this.add(panelWithHtmlListener(test), new GridBagConstraints(0, POS_Y, 1, 1,
                1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.CENTER,
                new Insets(0, GUIConstants.ELM_FIRST_COLUMN_WIDTH+8 , 0,
                        GUIConstants.ELM_ALIGN_RIGHT), 8, 0));      

private static JPanel panelWithHtmlListener(String msg){

        JEditorPane jEditorPane = new JEditorPane("text/html", msg);
        jEditorPane.setEditable(false);
        jEditorPane.setOpaque(false);
        int w= getContentWidtht(msg);
        int h= getContentHeight(msg);
       // jEditorPane.setPreferredSize(new Dimension(370,h));
        jEditorPane.setAlignmentY(LEFT_ALIGNMENT);
        HyperlinkListener listener = new HyperlinkListener() {
            @Override
            public void hyperlinkUpdate(HyperlinkEvent hyperLink) {

                if (HyperlinkEvent.EventType.ACTIVATED.
                                equals(hyperLink.getEventType())) {
                    try {
                       //respond to html link clicked
                       // JOptionPane.showMessageDialog(null, "HTML has been clicked !!" );
                        JFrame frame;
                        SearchLanguageDialog window = new SearchLanguageDialog();
                        window.frame.setVisible(true);
                    }
                    catch (Exception ex) { ex.printStackTrace();}


                }
            }
        };

        jEditorPane.addHyperlinkListener(listener);

        JPanel jPanel = new JPanel();
        jPanel.setLayout(new BorderLayout(5, 5));
        jPanel.add(jEditorPane,  BorderLayout.CENTER);
        jPanel.setBackground(Color.white);


        return jPanel;
    }

请帮我复习一下。谢谢!

1 个答案:

答案 0 :(得分:1)

此代码对您有帮助吗?

我在JLabel文本中添加了一些html并将布局更改为GroupLayout(我知道最灵活的 - 我们一直使用它:P)

import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class TestPanel extends JPanel {

    final String LONG_MSG = "This is some sample text with many words to display. It should be definitely wrapped";
    final String LINK = "<a href='#'>link</a>";
    JLabel lblTitle = new JLabel("Content:");
    JLabel lblContent = new JLabel("<html><body style='width: 300px'>" + LONG_MSG + " " + LINK + "</body></html>");

    public TestPanel() {
        GroupLayout layout = new GroupLayout(this);
        setLayout(layout);
        layout.setHorizontalGroup(layout.createSequentialGroup()
                .addComponent(lblTitle)
                .addGap(20)
                .addComponent(lblContent));
        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(lblTitle)
                .addComponent(lblContent));
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setContentPane(new TestPanel());
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

enter image description here