[英]Java Swing Complex Form - SpringLayout
我需要幫助來創建Java swing表單。 該表格是動態創建的,並要求用戶提供各種輸入。 輸入會有所不同,可能是Textfields,RadioButton,ComboBox。 我正在嘗試確定這種形式的最佳布局。 目前,我有類似的東西:
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的位置。 每行都是根據某些用戶輸入動態生成的。 我如何制作一個看起來像這樣的表格?
我最終使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.