简体   繁体   English

使用putConstraint在Spring Layout中TextArea的动态高度-Java

[英]Dynamic height of TextArea in Spring Layout using putConstraint - Java

I'd like to display notes in the textarea with a dynamic height(some notes are small and other notes are long). 我想在文本区域中以动态高度显示注释(一些注释很小,而其他注释则很长)。 I am retrieving note lines from the textarea to define height in the layout. 我正在从文本区域检索注释行,以定义布局中的高度。 I've tried different layouts but it didn't work out. 我尝试了不同的布局,但没有成功。 (meaning, different height is set based on the note length correctly but in GUI, all the note heights are distributed evenly.) (意思是,根据音符长度正确设置了不同的高度,但是在GUI中,所有音符高度均匀分布。)

This time, I am trying Springlayout to see if it works better but I haven't made much progress. 这次,我正在尝试Springlayout,看看它是否能更好地工作,但是我没有取得太大进展。 I am not too sure if I am using putConstraint() correctly in my situation. 我不太确定在我的情况下是否正确使用putConstraint()。

    TextArea textArea = new JTextArea();
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    ComponentUtils.setComponentText(textArea, note.getDisplayLines());

    int height= textArea.getLineCount(); 

    JPanel innerPanel = new JPanel();
    SpringLayout layout = new SpringLayout();
    innerPanel.setLayout(layout);
    innerPanel.add(textArea);
    layout.putConstraint(SpringLayout.NORTH, textArea,
            5, SpringLayout.NORTH, innerPanel);

    outerPanel.add(innerPanel , BorderLayout.CENTER);

I found the method to solve my own question. 我找到了解决自己问题的方法。 As for the layout, GridBagLayout was a perfect match to create different sizes of height of JTextArea. 对于布局,GridBagLayout是创建不同大小的JTextArea高度的理想选择。 (When used other layouts, they evenly distributed heights by default which was a problem in my case.) (当使用其他布局时,默认情况下它们会平均分配高度,这对我来说是个问题。)

When you use GridBagLayout, you need to instantiate GradBagConstraints. 使用GridBagLayout时,需要实例化GradBagConstraints。 With the object, you can call built-in functions; 使用该对象,您可以调用内置函数。 fill, weightx, gridx, gridwith etc to meet your personal needs in the GUI. fill,weightx,gridx,gridwith等,以满足您在GUI中的个人需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM