有难度!!! 怎样实现自上而下排列组件并能根据面板大小自动折到下一列,比如win资源管理器的文件夹查看方式中的列表模式 晕,用流布局管理(FlowLayout.LEFT)不就可以吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Flow是自左向右的,自上而下倒是有个Box,不过不会换列. 给个例子!import java.awt.*;import java.awt.event.*;import javax.swing.*;public class TestLayout extends JFrame { public TestLayout () { Container contentPane = getContentPane(); ContainerWithBoxLayout yaxis = new ContainerWithBoxLayout(BoxLayout.Y_AXIS); contentPane.setLayout(new FlowLayout()); yaxis.add(new JButton("reach")); yaxis.add(new JButton("punch")); yaxis.add(new JButton("open_hand")); yaxis.add(new JButton("ladybug")); yaxis.add(new JButton("crab")); yaxis.add(new JButton("frog")); yaxis.add(new JButton("snail")); contentPane.add(yaxis); } public static void main(String args[]) { final JFrame f = new TestLayout () f.setBounds(100,100,500,500); f.setTitle("An Application"); f.setVisible(true); f.setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE); f.addWindowListener(new WindowAdapter() { public void windowClosed(WindowEvent e) { System.exit(0); } }); }}class ContainerWithBoxLayout extends JPanel { public ContainerWithBoxLayout(int orientation) { setLayout(new BoxLayout(this, orientation)); } } GridBagLayout就够了,它的功能很强大的,建议楼主好好搞清楚与之相关的那么多参数的意义,然后做布局,另外结合其他的layout进行局部的排版 JBuilder有一个VerticalFlowLayout:// FrontEnd Plus GUI for JAD// DeCompiled : VerticalFlowLayout.classpackage com.borland.jbcl.layout;import java.awt.*;import java.io.Serializable;public class VerticalFlowLayout extends FlowLayout implements Serializable{ public static final int TOP = 0; public static final int MIDDLE = 1; public static final int BOTTOM = 2; int hgap; int vgap; boolean hfill; boolean vfill; public VerticalFlowLayout() { this(0, 5, 5, true, false); } public VerticalFlowLayout(boolean hfill, boolean vfill) { this(0, 5, 5, hfill, vfill); } public VerticalFlowLayout(int align) { this(align, 5, 5, true, false); } public VerticalFlowLayout(int align, boolean hfill, boolean vfill) { this(align, 5, 5, hfill, vfill); } public VerticalFlowLayout(int align, int hgap, int vgap, boolean hfill, boolean vfill) { setAlignment(align); this.hgap = hgap; this.vgap = vgap; this.hfill = hfill; this.vfill = vfill; } public int getHgap() { return hgap; } public void setHgap(int hgap) { super.setHgap(hgap); this.hgap = hgap; } public int getVgap() { return vgap; } public void setVgap(int vgap) { super.setVgap(vgap); this.vgap = vgap; } public Dimension preferredLayoutSize(Container target) { Dimension tarsiz = new Dimension(0, 0); for(int i = 0; i < target.getComponentCount(); i++) { Component m = target.getComponent(i); if(!m.isVisible()) continue; Dimension d = m.getPreferredSize(); tarsiz.width = Math.max(tarsiz.width, d.width); if(i > 0) tarsiz.height += vgap; tarsiz.height += d.height; } Insets insets = target.getInsets(); tarsiz.width += insets.left + insets.right + hgap * 2; tarsiz.height += insets.top + insets.bottom + vgap * 2; return tarsiz; } public Dimension minimumLayoutSize(Container target) { Dimension tarsiz = new Dimension(0, 0); for(int i = 0; i < target.getComponentCount(); i++) { Component m = target.getComponent(i); if(!m.isVisible()) continue; Dimension d = m.getMinimumSize(); tarsiz.width = Math.max(tarsiz.width, d.width); if(i > 0) tarsiz.height += vgap; tarsiz.height += d.height; } Insets insets = target.getInsets(); tarsiz.width += insets.left + insets.right + hgap * 2; tarsiz.height += insets.top + insets.bottom + vgap * 2; return tarsiz; } public void setVerticalFill(boolean vfill) { this.vfill = vfill; } public boolean getVerticalFill() { return vfill; } public void setHorizontalFill(boolean hfill) { this.hfill = hfill; } public boolean getHorizontalFill() { return hfill; } private void placethem(Container target, int x, int y, int width, int height, int first, int last) { int align = getAlignment(); Insets insets = target.getInsets(); if(align == 1) y += height / 2; if(align == 2) y += height; for(int i = first; i < last; i++) { Component m = target.getComponent(i); Dimension md = m.getSize(); if(m.isVisible()) { int px = x + (width - md.width) / 2; m.setLocation(px, y); y += vgap + md.height; } } } public void layoutContainer(Container target) { Insets insets = target.getInsets(); int maxheight = target.getSize().height - (insets.top + insets.bottom + vgap * 2); int maxwidth = target.getSize().width - (insets.left + insets.right + hgap * 2); int numcomp = target.getComponentCount(); int x = insets.left + hgap; int y = 0; int colw = 0; int start = 0; for(int i = 0; i < numcomp; i++) { Component m = target.getComponent(i); if(!m.isVisible()) continue; Dimension d = m.getPreferredSize(); if(vfill && i == numcomp - 1) d.height = Math.max(maxheight - y, m.getPreferredSize().height); if(hfill) { m.setSize(maxwidth, d.height); d.width = maxwidth; } else { m.setSize(d.width, d.height); } if(y + d.height > maxheight) { placethem(target, x, insets.top + vgap, colw, maxheight - y, start, i); y = d.height; x += hgap + colw; colw = d.width; start = i; continue; } if(y > 0) y += vgap; y += d.height; colw = Math.max(colw, d.width); } placethem(target, x, insets.top + vgap, colw, maxheight - y, start, numcomp); } static { TOP = 0; MIDDLE = 1; BOTTOM = 2; }} 谢谢了,不过我还想问下 nwpulipeng(夜听春雨) ( ) 信誉:129 2004-09-16 15:51:00 得分: 0 GridBagLayout就够了,它的功能很强大的,建议楼主好好搞清楚与之相关的那么多参数的意义,然后做布局,另外结合其他的layout进行局部的排版 要是用GriBagLayout,在不用继承该类改写方法的情况下,属性该怎么设置. 求解:ArrayList 代码如何写 请教一个关于this的问题 基础: 速战速决--htmlparser linux 下出现乱码问题 [130分]java的socket怎么上sock5代理?怎么设置代理用户和密码? 求救!在线等 如何将一个int转成byte? JB7中作数据库程序急问! 大家进来看看我这段代码,怎么在jb上编译不了呢, 《Java核心编程》第一个小程序就过不去!求助 各位前辈,我该采用java吗? 谁能告诉我如何在java写的窗口里插入图片??谢谢啊~~
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class TestLayout extends JFrame {
public TestLayout () {
Container contentPane = getContentPane();
ContainerWithBoxLayout yaxis =
new ContainerWithBoxLayout(BoxLayout.Y_AXIS);
contentPane.setLayout(new FlowLayout()); yaxis.add(new JButton("reach"));
yaxis.add(new JButton("punch"));
yaxis.add(new JButton("open_hand"));
yaxis.add(new JButton("ladybug"));
yaxis.add(new JButton("crab"));
yaxis.add(new JButton("frog"));
yaxis.add(new JButton("snail"));
contentPane.add(yaxis);
}
public static void main(String args[]) {
final JFrame f = new TestLayout ()
f.setBounds(100,100,500,500);
f.setTitle("An Application");
f.setVisible(true);
f.setDefaultCloseOperation(
WindowConstants.DISPOSE_ON_CLOSE); f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
}
class ContainerWithBoxLayout extends JPanel {
public ContainerWithBoxLayout(int orientation) {
setLayout(new BoxLayout(this, orientation));
}
}
// DeCompiled : VerticalFlowLayout.classpackage com.borland.jbcl.layout;import java.awt.*;
import java.io.Serializable;public class VerticalFlowLayout extends FlowLayout
implements Serializable
{ public static final int TOP = 0;
public static final int MIDDLE = 1;
public static final int BOTTOM = 2;
int hgap;
int vgap;
boolean hfill;
boolean vfill; public VerticalFlowLayout()
{
this(0, 5, 5, true, false);
} public VerticalFlowLayout(boolean hfill, boolean vfill)
{
this(0, 5, 5, hfill, vfill);
} public VerticalFlowLayout(int align)
{
this(align, 5, 5, true, false);
} public VerticalFlowLayout(int align, boolean hfill, boolean vfill)
{
this(align, 5, 5, hfill, vfill);
} public VerticalFlowLayout(int align, int hgap, int vgap, boolean hfill, boolean vfill)
{
setAlignment(align);
this.hgap = hgap;
this.vgap = vgap;
this.hfill = hfill;
this.vfill = vfill;
} public int getHgap()
{
return hgap;
} public void setHgap(int hgap)
{
super.setHgap(hgap);
this.hgap = hgap;
} public int getVgap()
{
return vgap;
} public void setVgap(int vgap)
{
super.setVgap(vgap);
this.vgap = vgap;
} public Dimension preferredLayoutSize(Container target)
{
Dimension tarsiz = new Dimension(0, 0);
for(int i = 0; i < target.getComponentCount(); i++)
{
Component m = target.getComponent(i);
if(!m.isVisible())
continue;
Dimension d = m.getPreferredSize();
tarsiz.width = Math.max(tarsiz.width, d.width);
if(i > 0)
tarsiz.height += vgap;
tarsiz.height += d.height;
} Insets insets = target.getInsets();
tarsiz.width += insets.left + insets.right + hgap * 2;
tarsiz.height += insets.top + insets.bottom + vgap * 2;
return tarsiz;
} public Dimension minimumLayoutSize(Container target)
{
Dimension tarsiz = new Dimension(0, 0);
for(int i = 0; i < target.getComponentCount(); i++)
{
Component m = target.getComponent(i);
if(!m.isVisible())
continue;
Dimension d = m.getMinimumSize();
tarsiz.width = Math.max(tarsiz.width, d.width);
if(i > 0)
tarsiz.height += vgap;
tarsiz.height += d.height;
} Insets insets = target.getInsets();
tarsiz.width += insets.left + insets.right + hgap * 2;
tarsiz.height += insets.top + insets.bottom + vgap * 2;
return tarsiz;
} public void setVerticalFill(boolean vfill)
{
this.vfill = vfill;
} public boolean getVerticalFill()
{
return vfill;
} public void setHorizontalFill(boolean hfill)
{
this.hfill = hfill;
} public boolean getHorizontalFill()
{
return hfill;
} private void placethem(Container target, int x, int y, int width, int height, int first, int last)
{
int align = getAlignment();
Insets insets = target.getInsets();
if(align == 1)
y += height / 2;
if(align == 2)
y += height;
for(int i = first; i < last; i++)
{
Component m = target.getComponent(i);
Dimension md = m.getSize();
if(m.isVisible())
{
int px = x + (width - md.width) / 2;
m.setLocation(px, y);
y += vgap + md.height;
}
} } public void layoutContainer(Container target)
{
Insets insets = target.getInsets();
int maxheight = target.getSize().height - (insets.top + insets.bottom + vgap * 2);
int maxwidth = target.getSize().width - (insets.left + insets.right + hgap * 2);
int numcomp = target.getComponentCount();
int x = insets.left + hgap;
int y = 0;
int colw = 0;
int start = 0;
for(int i = 0; i < numcomp; i++)
{
Component m = target.getComponent(i);
if(!m.isVisible())
continue;
Dimension d = m.getPreferredSize();
if(vfill && i == numcomp - 1)
d.height = Math.max(maxheight - y, m.getPreferredSize().height);
if(hfill)
{
m.setSize(maxwidth, d.height);
d.width = maxwidth;
} else
{
m.setSize(d.width, d.height);
}
if(y + d.height > maxheight)
{
placethem(target, x, insets.top + vgap, colw, maxheight - y, start, i);
y = d.height;
x += hgap + colw;
colw = d.width;
start = i;
continue;
}
if(y > 0)
y += vgap;
y += d.height;
colw = Math.max(colw, d.width);
} placethem(target, x, insets.top + vgap, colw, maxheight - y, start, numcomp);
} static
{
TOP = 0;
MIDDLE = 1;
BOTTOM = 2;
}
}
GridBagLayout就够了,它的功能很强大的,建议楼主好好搞清楚与之相关的那么多参数的意义,然后做布局,另外结合其他的layout进行局部的排版
要是用GriBagLayout,在不用继承该类改写方法的情况下,属性该怎么设置.