gpt4 book ai didi

Java Swing 复杂表单 - SpringLayout

转载 作者:太空宇宙 更新时间:2023-11-04 07:27:53 24 4
gpt4 key购买 nike

我需要帮助创建 Java swing 表单。该表单是动态创建的,并要求用户提供各种输入。输入各不相同,可以是文本字段、单选按钮、组合框。我正在尝试确定这种表单的最佳布局。目前,我有类似的东西:

JPanel pnlForm = new JPanel(new SpringLayout());

for (Parameter p : globals) {
JLabel lblName = new JLabel(p.getName() + ": ", JLabel.TRAILING);
pnlForm.add(lblName);

// The input field depends on parameter type
if (p.getType().equals("filename")) {
JPanel pnlFileChooser = new JPanel();
JTextArea txtArea = new JTextArea(p.getValue());
JButton btnFileChooser = new JButton("Browse");
pnlFileChooser.add(txtArea);
pnlFileChooser.add(btnFileChooser);
pnlForm.add(pnlFileChooser);
} else if (p.getType().equals("textbox")) {
JTextArea txtArea = new JTextArea(p.getValue());
pnlForm.add(txtArea);
} else if (p.getType().equals("checkbox")) {
// not yet implemented
} else if (p.getType().equals("radio")) {
ButtonGroup bgRadios = new ButtonGroup();
for(String option : p.getSelections()){
JRadioButton btnOption = new JRadioButton(option);
btnOption.setMnemonic(KeyEvent.VK_B);
btnOption.setActionCommand(option);
bgRadios.add(btnOption);
pnlForm.add(btnOption);
}
} else {
JLabel lblError = new JLabel("ERROR! Unknown type!" + p.getType());
lblName.setLabelFor(lblError);
pnlForm.add(lblError);
}

//Lay out the panel.
SpringUtilities.makeCompactGrid(pnlDetails,
globals.size() - 1, 2, //rows, cols
1, 1, //initX, initY
5, 5); //xPad, yPad

目前的情况非常不稳定(单选按钮不会停留在同一区域)。关于如何使这个布局更好有什么想法吗?

谢谢!

[编辑]

添加下图以了解其外观。灰线可以忽略,只是用来显示 JPanel 可能所在的位置。每行都是根据某些用户输入动态生成的。我怎样才能制作一个像这样的表格?

enter image description here

最佳答案

我最终使用 SpringLayout 来完成这项工作,每行有两个面板。左侧面板包含标签,右侧面板包含输入字段。这似乎运作良好:

    for (Parameter p : params) {
// The label for this parameter
JLabel pname = new JLabel(p.getName() + ": ", JLabel.TRAILING);

// Setup the sub panels for this layout, aligned to point each other
JPanel pnlLeft = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JPanel pnlRight = new JPanel(new FlowLayout(FlowLayout.LEFT));

// The value field for this parameter, depends on param type
if (p.getType().equals("filename")) {
JButton btnBrowse = new JButton("Browse");
btnBrowse.setActionCommand(pfilepath + p.getId());
btnBrowse.addActionListener(this);
JTextField pvalue = new JTextField(p.getValue(),50);
pnlRight.add(pvalue);
pnlRight.add(btnBrowse);
} else if (p.getType().equals("directory")) {
JButton btnBrowse = new JButton("Browse");
btnBrowse.setActionCommand(pdirpath + p.getId());
btnBrowse.addActionListener(this);
JTextField pvalue = new JTextField(p.getValue(),50);
pnlRight.add(pvalue);
pnlRight.add(btnBrowse);
} else if (p.getType().equals("textbox")) {
JTextField pvalue = new JTextField(p.getValue(),50);
pnlRight.add(pvalue);
} else if (p.getType().equals("checkbox")) {
for(String option : p.getSelections()){
JCheckBox chkbox = new JCheckBox(option);
if (p.getValue().contains(option)){
chkbox.setSelected(true);
}
chkbox.setName(p.getId() + "_" + option);
chkbox.setActionCommand(pchkbox + p.getId() + "_" + option);
chkbox.addActionListener(this);
pnlRight.add(chkbox);
}
} else if (p.getType().equals("radio")) {
ButtonGroup bgRadios = new ButtonGroup();
for(String option : p.getSelections()){
JRadioButton radio = new JRadioButton(option);
if (p.getValue().equals(option)){
radio.setSelected(true);
}
radio.setName(p.getId() + "_" + option);
radio.setActionCommand(pradio + p.getId() + "_" + option);
radio.addActionListener(this);
bgRadios.add(radio);
pnlRight.add(radio);
}
} else {
JTextField pvalue = new JTextField(p.getValue(),50);
pvalue.setText("ERROR! Invalid global parameter type!" + p.getType());
pvalue.setEditable(false);
pnlRight.add(pvalue);
}

// Add subpanels to this main panel and set labeling
pnlLeft.add(pname);
pname.setLabelFor(pnlRight);
add(pnlLeft);
add(pnlRight);
}

// Correctly setup SpringLayout grid for this main panel
SpringUtilities.makeCompactGrid(this,
params.size(), 2, //rows, cols
1, 1, //initX, initY
1, 1); //xPad, yPad

关于Java Swing 复杂表单 - SpringLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18244407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com