Java 获取多行flowlayout的首选大小
我想不出在swing GUI中调整某些组件大小的方法。某些自定义标签被添加到FlowLayout中,但在调整对话框大小时,它们的行为与FlowLayout不符。 该面板是使用jgoodies表单框架构建的 如果使用此选项,将FlowLayout添加到xy(3,y)的位置 FlowLayout将展开,并显示一个滚动条Java 获取多行flowlayout的首选大小,java,swing,resize,jgoodies,flowlayout,Java,Swing,Resize,Jgoodies,Flowlayout,我想不出在swing GUI中调整某些组件大小的方法。某些自定义标签被添加到FlowLayout中,但在调整对话框大小时,它们的行为与FlowLayout不符。 该面板是使用jgoodies表单框架构建的 如果使用此选项,将FlowLayout添加到xy(3,y)的位置 FlowLayout将展开,并显示一个滚动条 如果使用 FormLayout layout = new FormLayout("r:d, 5px, f:10:g", // columns "p, p,
如果使用
FormLayout layout = new FormLayout("r:d, 5px, f:10:g", // columns
"p, p, 5px, p, 5px, p") // rows
FlowLayout使用可用空间,第二行上的项目消失我想将包含FlowLayout的每一行的高度扩展到组件的当前高度。不幸的是,首选大小始终与单行的高度相对应。
另一种布局是否更合适?左侧的粗体文本应与右侧对齐,然后是FlowLayout [编辑]在尝试了数周后,实际问题可以继续到以下位置:
正在向JPanel添加一组标签。此JPanel应水平使用所有可用空间(对话大小减去标记名称标签的宽度),并根据需要垂直扩展。如果JPanel的高度大于对话框,则应显示一个垂直滚动条(水平滚动条永远不可见)。 该对话框可以显示多个JPanel,这些JPanel将一个接一个(垂直)显示 下面是使用GridBagLayout和:
公共类GridBagLayoutTagPanel扩展了JPanel{
私有静态最终长serialVersionUID=-441746014057882848L;
私人最终int NB_标签=5;
公共网格布局TagPanel(){
setLayout(新的GridLayout());
JPanel pTags=newjpanel(newgridbaglayout());
pTags.挫折背景(颜色:橙色);
GridBagConstraints c=新的GridBagConstraints();
c、 ipadx=5;
c、 ipady=5;
int rowIndex=0;
对于(int i=0;i
我想将包含FlowLayout的每一行的高度扩展到组件的当前高度。不幸的是,首选大小始终对应于单行的高度
处理此问题。您尝试过吗?这是我唯一使用的布局管理器,我从来没有发现过一个布局我做不到。在Rob的帮助下,使用他的,它现在完全可以正常工作了
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);
}
}
公共类GridBagLayoutTagPanel扩展了JPanel{
私人最终int NB_标签=5;
私有最终整数NB_TAGVALUES=50;
公共网格布局TagPanel(){
setLayout(新的GridLayout());
ScrollablePanel pTags=新的ScrollablePanel(新的GridBagLayout());
pTags.setScrollableWidth(ScrollablePanel.ScrollableSizeHint.FIT);
pTags.挫折背景(颜色:橙色);
GridBagConstraints c=新的GridBagConstraints();
c、 ipadx=5;
c、 ipady=5;
int rowIndex=0;
对于(int i=0;i
您是否尝试过使用FormDebugUtils
类和/或FormDebugPanel
?这些调试工具几次帮助我了解了实际情况,为什么在代码明确指示使用了FormLayout
时,您会声明FlowLayout
<代码>表单布局=FlowLayout@AndrewThompson表单布局不是问题所在。它的行为就像它被告知的那样(对于第二个示例),使用可用的水平空间,并根据添加到FormLayout的FlowLayout的首选高度拉伸垂直空间。问题是首选的高度永远不会改变。这似乎是exa
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);
}
}
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);
}
}