在awt中将组件添加到面板时如何包装组件的行高?

时间:2018-01-14 22:13:58

标签: java swing awt layout-manager

我正在尝试创建能够输入类别和entires的面板。

在图片中,您可以在窗口底部看到public class Dinner { private JPanel panelRoot; private JTextField textFieldCategory; private JButton buttonAddCategory; private JPanel panelInput; private JPanel panelControl; public static void main(String[] args) { new Dinner().doStuff(); } private void doStuff() { panelInput.setLayout(new BoxLayout(panelInput, BoxLayout.Y_AXIS)); buttonAddCategory.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JPanel jPanel = new JPanel(); jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.X_AXIS)); JTextField jTextField = new JTextField(); jPanel.add(jTextField); JButton jButton = new JButton("Add entry"); jPanel.add(jButton); panelInput.add(jPanel); panelRoot.revalidate(); } }); showWindow(); } private void showWindow() { JFrame frame = new JFrame("Dinner"); frame.setContentPane(panelRoot); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setMinimumSize(new Dimension(680, 0)); frame.pack(); frame.setVisible(true); } } <?xml version="1.0" encoding="UTF-8"?> <form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="xxx.Dinner"> <grid id="27dc6" binding="panelRoot" layout-manager="BorderLayout" hgap="0" vgap="0"> <constraints> <xy x="20" y="20" width="500" height="400"/> </constraints> <properties/> <border type="none"/> <children> <grid id="57444" binding="panelInput" layout-manager="BorderLayout" hgap="0" vgap="0"> <constraints border-constraint="Center"/> <properties/> <border type="none"/> <children/> </grid> <grid id="59d09" binding="panelControl" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <margin top="0" left="0" bottom="0" right="0"/> <constraints border-constraint="South"/> <properties/> <border type="none"/> <children> <component id="1076b" class="javax.swing.JTextField" binding="textFieldCategory"> <constraints> <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"> <preferred-size width="150" height="-1"/> </grid> </constraints> <properties/> </component> <component id="d75cb" class="javax.swing.JButton" binding="buttonAddCategory"> <constraints> <grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> </constraints> <properties> <text value="Add Category"/> </properties> </component> </children> </grid> </children> </grid> </form>

enter image description here

如果用户添加了一个类别,则会在上面板中显示带有组件的新行。

问题是,新添加的行不会换行,占用所有空间。

我如何强制每个添加的行包裹它的高度(如下所示)?

enter image description here

这是我的代码:

的.java:

{{1}}

.FORM:

{{1}}

1 个答案:

答案 0 :(得分:0)

  1. 添加BorderLayout类型的容器JPanel
  2. 将每个新的BoxLayout JPanel添加到BorderLayout.PAGE_START;
  3. 重复Keep BoxLayout From Expanding Children

    归功于作者。