私は5 JPanels
が縦に並んでいます。各JPanel
には同じ要素が入力されていますが、異なる値(JPanel
,JButton
,JLabel
)が入力されています。GridBagLayoutは一直線になっていません
Panel Button Label
Panel Button Label
言い訳それは間隔が少しオフですが、コードは、各コンテナのと全く同じである
Panel Button Label
Panel Button Label
次のように輩出だ:私は彼らが次のようになりたいです。これをどうすれば解決できますか?
public class AnswerChoice extends JPanel {
private static final long serialVersionUID = 1L;
private AnswerButton button;
private JLabel answerLabel;
public AnswerChoice(String imageURL) {
setBackground(Color.RED);
setLayout(new GridBagLayout());
button = new AnswerButton(imageURL);
answerLabel = new JLabel();
answerLabel.setFont(new Font("Times New Roman", Font.PLAIN, 32));
GridBagConstraints gbc = new GridBagConstraints();
JPanel panel = new JPanel();
panel.setBackground(Color.ORANGE);
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 1;
gbc.weightx = 0.2;
add(panel, gbc);
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 2;
gbc.weightx = 0.0;
gbc.insets = new Insets(0, 0, 0, 30);
add(button, gbc);
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 3;
gbc.weightx = 0.8;
add(answerLabel, gbc);
}
}
JLabel
はそれでテキストを持っていませんが、私はどこか別の場所に設定します。
おそらく 'gbc.anchor = GridBagConstraints.BASELINE_LEADING;'が必要です。アンカーフィールドは、セル内のコンポーネントの配置を決定します。 – VGR
3列テーブルはデータの表示に適しているようです。異なるパネルの 'GridBagLayout'インスタンスを別々に整列させることはできません! –
テーブル内で比例するスペースを与えることはできますか?私は 'GridBagLayout'を使いましたので、0.2と0.8の重みを使うことができました。 – kneedhelp