我无法想出一种在swing GUI中调整某些组件大小的方法。一些自定义标签正被添加到FlowLayout中,当调整对话框大小时,它不会表现如何。 该小组正在使用jgoodies表单框架构建。
如果使用此项,则将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使用可用空间,第二行上的项目消失
我想将包含FlowLayout的每一行的高度扩展到组件的当前高度。不幸的是,首选尺寸始终对应于单行的高度 另一种布局会更合适吗?左侧的粗体文本应右对齐,然后是FlowLayout。
[edit] 在试图弄清楚如何执行此操作数周之后,实际问题可以恢复到此:
一组标签正被添加到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);
}
}
答案 0 :(得分:11)
我想将包含FlowLayout的每一行的高度扩展到组件的当前高度。不幸的是,首选大小始终对应于单行的高度。
Wrap Layout处理此问题。
答案 1 :(得分:3)
借助Rob的帮助并使用他的WrapPanel和ScrollablePanel,它现在可以正常工作
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);
}
}
答案 2 :(得分:2)
你试过MigLayout吗?它是我使用的唯一布局管理器,我从来没有找到一个我无法做到的布局。