マルチラインフローレイアウトの優先サイズを取得します
-
27-10-2019 - |
質問
スイングGUIの一部のコンポーネントをサイズ変更する方法を把握できません。一部のカスタムラベルは、ダイアログをサイズ変更するときにどのように動作するかを振る舞わないFlowLayoutに追加されています。パネルは、JGoodies Forms Frameworkを使用して構築されています。
これを使用する場合、flowlayoutがxyに追加されている場合(3、y)
FormLayout layout = new FormLayout("r:d, 5px, f:d:g", // columns
"p, p, 5px, p, 5px, p") // rows
FlowLayoutが展開し、スクロールバーが表示されます
使用する場合
FormLayout layout = new FormLayout("r:d, 5px, f:10:g", // columns
"p, p, 5px, p, 5px, p") // rows
Flowlayoutは使用可能なスペースを使用し、2行目のアイテムが消えます
フローレイアウトを含む各行の高さをコンポーネントの現在の高さまで拡張したいと思います。残念ながら、優先サイズは常に単一の行のハイトに対応しています。
別のレイアウトがより適切でしょうか?左側の太字のテキストを右に配置し、その後にFlowLayoutを使用する必要があります。
編集 これを何週間も行う方法を見つけようとした後、実際の質問はこれに再開することができます。
一連のラベルがJPanelに追加されています。このJPANELは、利用可能なすべてのスペースを水平方向に使用し(ダイアログサイズマイナスタグ名ラベルの幅)、必要に応じて垂直に拡張する必要があります。 JPANELの高さが大きくなると、対話が大きくなると、垂直スクロールバーが表示されます(水平スクロールバーが表示されません)。対話には、複数のJPanelが表示される可能性があります。
gridbaglayoutを使用した試みは次のとおりです wraplayout:
public class GridBagLayoutTagPanel extends JPanel {
private static final long serialVersionUID = -441746014057882848L;
private final int NB_TAGS = 5;
public GridBagLayoutTagPanel() {
setLayout(new GridLayout());
JPanel pTags = new JPanel(new GridBagLayout());
pTags.setBackground(Color.ORANGE);
GridBagConstraints c = new GridBagConstraints();
c.ipadx = 5;
c.ipady = 5;
int rowIndex = 0;
for (int i = 0; i < NB_TAGS; i++) {
//add tag name
JLabel lTagName = new JLabel(String.format("Tag %s:", i));
lTagName.setFont(lTagName.getFont().deriveFont(Font.BOLD));
c.fill = GridBagConstraints.NONE;
c.gridx = 0;
c.gridy = rowIndex++;
pTags.add(lTagName, c);
//add tag values
JPanel pTag = new JPanel(new BorderLayout());
pTag.add(new JLabel("+"), BorderLayout.LINE_START); //label used to add new tags
pTag.add(getWrapPanel(), BorderLayout.CENTER); //the list of tag values
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
pTags.add(pTag, c);
}
//JScrollPane sp = new JScrollPane(pTags);
//sp.setBorder(BorderFactory.createEmptyBorder());
add(pTags);
}
private static JPanel getWrapPanel() {
JPanel p = new JPanel(new WrapLayout(FlowLayout.LEFT, 5, 0));
for (int i = 0; i < 50; i++) {
p.add(new JLabel("t" + i));
}
return p;
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.getContentPane().add(new GridBagLayoutTagPanel());
f.setSize(new Dimension(500, 300));
f.setVisible(true);
}
}
解決 2
ロブの助けを借りて、彼を使っています ラッパネル と スクロール可能パネル 現在、正確に機能するようになりました
public class GridBagLayoutTagPanel extends JPanel {
private final int NB_TAGS = 5;
private final int NB_TAGVALUES = 50;
public GridBagLayoutTagPanel() {
setLayout(new GridLayout());
ScrollablePanel pTags = new ScrollablePanel(new GridBagLayout());
pTags.setScrollableWidth(ScrollablePanel.ScrollableSizeHint.FIT);
pTags.setBackground(Color.ORANGE);
GridBagConstraints c = new GridBagConstraints();
c.ipadx = 5;
c.ipady = 5;
int rowIndex = 0;
for (int i = 0; i < NB_TAGS; i++) {
// add tag name
JLabel lTagName = new JLabel(String.format("Tag %s:", i));
lTagName.setFont(lTagName.getFont().deriveFont(Font.BOLD));
c.fill = GridBagConstraints.NONE;
c.gridx = 0;
c.gridy = rowIndex++;
c.weightx = 0; // keep minimum size of the cell containing the label when resizing
c.weighty = 0;
pTags.add(lTagName, c);
// add tag values
JPanel pTag = new JPanel(new BorderLayout());
pTag.add(new JLabel("+"), BorderLayout.LINE_START); // label used to add new tags
pTag.add(getWrapPanel(), BorderLayout.CENTER); // the list of tag values
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.weightx = 1; // fill horizontally
c.weighty = 1;
pTags.add(pTag, c);
}
JScrollPane sp = new JScrollPane(pTags);
sp.setBorder(BorderFactory.createEmptyBorder());
add(sp);
}
private JPanel getWrapPanel() {
JPanel p = new JPanel(new WrapLayout(FlowLayout.LEFT, 5, 0));
for (int i = 0; i < NB_TAGVALUES; i++) {
p.add(new JLabel("t" + i));
}
p.setSize(400, 1);
return p;
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.getContentPane().add(new GridBagLayoutTagPanel());
f.setSize(new Dimension(500, 300));
f.setVisible(true);
}
}