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 がどこにあるかを示すために、灰色の線は無視できます。各行は、ユーザー入力に基づいて動的に生成されます。このようなフォームを作成するにはどうすればよいですか?