我一直致力于使用Swing创建表单,以编程方式将自定义组件添加到ScrollPane中,以便组件具有固定大小,并且添加更多组件时,它们将添加到前一个下方,并且可以滚动到。 / p>
我一直在努力使用Swing中的各种大小功能。目前,我设法以编程方式将子组件添加到scrollPane中的面板中,并在添加更多子项时调整该面板的大小。然而,孩子们目前正试图填满整个小组,随着更多孩子的增加而缩小,直到他们达到最小尺寸。只有达到此最小尺寸后,面板才会扩展以适应新的组件。我已经使用了setSize和setPreferredSize的各种组合,但我还没能让它正常工作。
Here就是表格。
Here只需点击一下按钮即可。
Here点击两次后。
Here只需点击几下即可。
这是我用于测试的数据对象。
SimpleData.java
public class SimpleData {
public SimpleData(String s0,String s1)
{
name = s0;
ID = s1;
}
public String name;
public String ID;
}
以下是组件的代码。
SimpleComponent.java
import javax.swing.*;
import java.awt.*;
import java.util.function.BiFunction;
import java.util.function.Supplier;
public class SimpleComponent extends JPanel
{
private JPanel mainPanel;
private JLabel mainName;
private JPanel sectionName;
private JLabel labelName;
private JTextField textName;
private JPanel sectionID;
private JLabel labelID;
private JTextField textID;
private SimpleComponent()
{
super();
}
public static SimpleComponent FromSimpleData(SimpleData data, Dimension size)
{
SimpleComponent ret = new SimpleComponent();
BoxLayout lay = new BoxLayout(ret,BoxLayout.PAGE_AXIS);
ret.setLayout(lay);
BiFunction<JPanel,String,JTextField> makeTextField =
(JPanel p,String s) ->
{
JTextField tf;
tf = new JTextField(s == null ? "" : s);
p.add(tf);
return tf;
};
BiFunction<JPanel,String,JLabel> makeLabel =
(JPanel p,String s) ->
{
JLabel jl;
jl = new JLabel(s);
p.add(jl);
return jl;
};
Supplier<JPanel> makePanel =
() ->
{
JPanel p;
p = new JPanel();
BoxLayout bl = new BoxLayout(p,BoxLayout.X_AXIS);
p.setLayout(bl);
ret.add(p);
return p;
};
ret.mainPanel = makePanel.get();
ret.mainName = makeLabel.apply(ret.mainPanel,data.name);
ret.sectionName = makePanel.get();
ret.labelName = makeLabel.apply(ret.sectionName,"Name:");
ret.textName = makeTextField.apply(ret.sectionName,data.name);
ret.sectionID = makePanel.get();
ret.labelID = makeLabel.apply(ret.sectionID,"ID:");
ret.textID = makeTextField.apply(ret.sectionID,data.ID);
ret.setSize(size);
ret.revalidate();
return ret;
}
}
以下是表单的java代码。
SimpleGUI.java
import javax.swing.*;
import java.awt.*;
public class simpleGUI {
private JButton buttonAdd;
private JPanel panelData;
private JPanel panelMain;
private static int count = 0;
public static void main(String[] args) {
JFrame frame = new JFrame("simpleGUI");
simpleGUI sGUI = new simpleGUI();
frame.setContentPane(sGUI.panelMain);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sGUI.panelData.setLayout(
new BoxLayout(
sGUI.panelData,
BoxLayout.PAGE_AXIS));
sGUI.buttonAdd.addActionListener(
(e)->{
Dimension size = sGUI.panelData.getSize();
String id = Integer.toString(count++);
String name = "DATA-" + id;
SimpleData data = new SimpleData(name,id);
SimpleComponent component = SimpleComponent.FromSimpleData(
data,
new Dimension(
10,
10));
sGUI.panelData.add(component);
Dimension newSize = new Dimension(size.width,size.height * 2);
sGUI.panelData.setSize(newSize);
sGUI.panelData.validate();
});
frame.pack();
frame.setVisible(true);
}
}
以下是使用InteliJ Idea生成的表单的XML。
SimpleGUI.form
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="simpleGUI">
<grid id="27dc6" binding="panelMain" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="4a868" class="javax.swing.JButton" binding="buttonAdd">
<constraints>
<grid row="0" column="0" 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"/>
</properties>
</component>
<scrollpane id="29e66">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<verticalScrollBarPolicy value="22"/>
</properties>
<border type="none"/>
<children>
<grid id="2d3af" binding="panelData" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints/>
<properties/>
<border type="none">
<color color="-16729344"/>
</border>
<children/>
</grid>
</children>
</scrollpane>
</children>
</grid>
</form>
我可以做些什么来完成我需要的行为?