Java 如何优化这种循环?

Java 如何优化这种循环?,java,swing,loops,layout,layout-manager,Java,Swing,Loops,Layout,Layout Manager,我正在尝试制作这个GUi 我编写自己的布局,因为现有的布局管理器不符合我的要求 它可以工作,但我试图通过使用循环来优化所有按钮的创建 测试类 public class Test { public static void main(String[] args) { JFrame f = new JFrame(); JPanel parent = new JPanel(); f.add(parent); parent.set

我正在尝试制作这个GUi


我编写自己的布局,因为现有的布局管理器不符合我的要求 它可以工作,但我试图通过使用循环来优化所有按钮的创建

测试类

public class Test {

    public static void main(String[] args) {
        JFrame f = new JFrame();
        JPanel parent = new JPanel();
        f.add(parent);
        parent.setLayout(new BoxLayout(parent, BoxLayout.X_AXIS));

        JPanel[] children = new JPanel[6];
        for (int i = 1; i < children.length; i++) {
            children[i] = new JPanel();
            children[i].setLayout(new XYLayout());
            children[i].setBorder(new LineBorder(Color.red));
            parent.add(children[i]);
        }

        int x = 0, y = 375, w = 0, h = 50;

        children[1].add(new JButton("8"), new XYConstraints(0, 25, 0, 50));
        children[1].add(new JButton("7"), new XYConstraints(0, 75, 0, 50));
        children[1].add(new JButton("6"), new XYConstraints(0, 125, 0, 50));
        children[1].add(new JButton("5"), new XYConstraints(0, 175, 0, 50));
        children[1].add(new JButton("4"), new XYConstraints(0, 225, 0, 50));
        children[1].add(new JButton("3"), new XYConstraints(0, 275, 0, 50));
        children[1].add(new JButton("2"), new XYConstraints(0, 325, 0, 50));
        children[1].add(new JButton("1"), new XYConstraints(0, 375, 0, 50));

        children[2].add(new JButton("7"), new XYConstraints(240, 25, 0, 100));
        children[2].add(new JButton("6"), new XYConstraints(200, 75, 0, 100));
        children[2].add(new JButton("5"), new XYConstraints(160, 125, 0, 100));
        children[2].add(new JButton("4"), new XYConstraints(120, 175, 0, 100));
        children[2].add(new JButton("3"), new XYConstraints(80, 225, 0, 100));
        children[2].add(new JButton("2"), new XYConstraints(40, 275, 0, 100));
        children[2].add(new JButton("1"), new XYConstraints(0, 325, 0, 100));

        children[3].add(new JButton("6"), new XYConstraints(200, 25, 0, 150));
        children[3].add(new JButton("5"), new XYConstraints(160, 75, 0, 150));
        children[3].add(new JButton("4"), new XYConstraints(120, 125, 0, 150));
        children[3].add(new JButton("3"), new XYConstraints(80, 175, 0, 150));
        children[3].add(new JButton("2"), new XYConstraints(40, 225, 0, 150));
        children[3].add(new JButton("1"), new XYConstraints(0, 275, 0, 150));

        //children[4],//children[5]...



        f.setSize(800, 600);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);

    }
公共类测试{
公共静态void main(字符串[]args){
JFrame f=新的JFrame();
JPanel parent=新的JPanel();
f、 添加(父级);
setLayout(新的BoxLayout(父,BoxLayout.X_轴));
JPanel[]children=新JPanel[6];
for(int i=1;i
一个文件中的自定义布局:XYLayout

 public class XYLayout implements LayoutManager2, Serializable {

    private int width;
    private int height;
    Hashtable<Component, Object> info;
    static final XYConstraints defaultConstraints = new XYConstraints();

    public XYLayout() {
        info = new Hashtable<Component, Object>();
    }

    public XYLayout(int width, int height) {
        info = new Hashtable<Component, Object>();
        this.width = width;
        this.height = height;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("XYLayout [width=").append(width).append(", height=").append(height).append("]");
        return builder.toString();
    }

    public void addLayoutComponent(String s, Component component1) {
    }

    public void removeLayoutComponent(Component component) {
        info.remove(component);
    }

    public Dimension preferredLayoutSize(Container target) {
        return getLayoutSize(target, true);
    }

    public Dimension minimumLayoutSize(Container target) {
        return getLayoutSize(target, false);
    }

    public void layoutContainer(Container target) {
        Insets insets = target.getInsets();
        int count = target.getComponentCount();
        for (int i = 0; i < count; i++) {
            Component component = target.getComponent(i);
            if (component.isVisible()) {
                Rectangle r = getComponentBounds(component, true);
                component.setBounds(insets.left + r.x, insets.top + r.y, r.width, r.height);
            }
        }

    }

    public void addLayoutComponent(Component component, Object constraints) {
        if (constraints instanceof XYConstraints)
            info.put(component, constraints);
    }

    public Dimension maximumLayoutSize(Container target) {
        return new Dimension(0x7fffffff, 0x7fffffff);
    }

    public float getLayoutAlignmentX(Container target) {
        return 0.5F;
    }

    public float getLayoutAlignmentY(Container target) {
        return 0.5F;
    }

    public void invalidateLayout(Container container) {
    }

    public Rectangle getComponentBounds(Component component, boolean doPreferred) {
        XYConstraints constraints = (XYConstraints) info.get(component);
        if (constraints == null)
            constraints = defaultConstraints;
        Rectangle r = new Rectangle(constraints.getX(), constraints.getY(), constraints.getW(), constraints.getH());
        if (r.width <= 0 || r.height <= 0) {
            Dimension d = doPreferred ? component.getPreferredSize() : component.getMinimumSize();
            if (r.width <= 0)
                r.width = d.width;
            if (r.height <= 0)
                r.height = d.height;
        }
        return r;
    }

    public Dimension getLayoutSize(Container target, boolean doPreferred) {
        Dimension dim = new Dimension(0, 0);
        if (width <= 0 || height <= 0) {
            int count = target.getComponentCount();
            for (int i = 0; i < count; i++) {
                Component component = target.getComponent(i);
                if (component.isVisible()) {
                    Rectangle r = getComponentBounds(component, doPreferred);
                    dim.width = Math.max(dim.width, r.x + r.width);
                    dim.height = Math.max(dim.height, r.y + r.height);
                }
            }

        }
        if (width > 0)
            dim.width = width;
        if (height > 0)
            dim.height = height;
        Insets insets = target.getInsets();
        dim.width += insets.left + insets.right;
        dim.height += insets.top + insets.bottom;
        return dim;
    }

}

class XYConstraints implements Cloneable, Serializable {

    private int x;

    private int y;

    private int w;

    private int h;

    public XYConstraints() {
        this(0, 0, 0, 0);
    }

    public XYConstraints(int x, int y, int w, int h) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getW() {
        return w;
    }

    public void setW(int w) {
        this.w = w;
    }

    public int getH() {
        return h;
    }

    public void setH(int h) {
        this.h = h;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + h;
        result = prime * result + w;
        result = prime * result + x;
        result = prime * result + y;
        return result;
        // return x ^ y * 37 ^ w * 43 ^ h * 47;
    }

    public boolean equals(Object that) {
        if (that instanceof XYConstraints) {
            XYConstraints other = (XYConstraints) that;
            return other.x == x && other.y == y && other.w == w && other.h == h;
        } else {
            return false;
        }
    }

    public Object clone() {
        return new XYConstraints(x, y, w, h);
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("XYConstraints [x=").append(x).append(", y=").append(y).append(", w=").append(w).append(", h=").append(h).append("]");
        return builder.toString();
    }
公共类XYLayout实现LayoutManager2,可序列化{
私有整数宽度;
私人内部高度;
哈希表信息;
静态最终XYConstraints defaultConstraints=新XYConstraints();
公共布局(){
info=新哈希表();
}
公共木布局(内部宽度、内部高度){
info=新哈希表();
这个。宽度=宽度;
高度=高度;
}
公共int getWidth(){
返回宽度;
}
公共void setWidth(int-width){
这个。宽度=宽度;
}
公共整数getHeight(){
返回高度;
}
公共空间设置高度(内部高度){
高度=高度;
}
/*
*(非Javadoc)
* 
*@see java.lang.Object#toString()
*/
@凌驾
公共字符串toString(){
StringBuilder=新的StringBuilder();
builder.append(“XYLayout[width=”).append(width).append(“,height=”).append(height).append(“]);
返回builder.toString();
}
public void addLayoutComponent(字符串s,组件component1){
}
公共void removeLayoutComponent(组件组件){
信息删除(组件);
}
公共维度preferredLayoutSize(容器目标){
返回getLayoutSize(target,true);
}
公共维度最小布局大小(容器目标){
返回getLayoutSize(target,false);
}
公共void布局容器(容器目标){
Insets Insets=target.getInsets();
int count=target.getComponentCount();
for(int i=0;i
for( int i=1; i<=3; i++ ) {
    x = 0;
    for( y=375-((i-1)*50; y>0; y-=50, x+=40 ) {
        children[i].add(new JButton((String)i), new XYConstraints(x, y, w, i*h));
    }
}
for(inti=1;i0;y-=50,x+=40){
子项[i]。添加(新的JButton((String)i),新的xyconstraint(x,y,w,i*h));
}
}
您可以尝试:

for( int i=1; i<=3; i++ ) {
    x = 0;
    for( y=375-((i-1)*50; y>0; y-=50, x+=40 ) {
        children[i].add(new JButton((String)i), new XYConstraints(x, y, w, i*h));
    }
}
for(inti=1;i0;y-=50,x+=40){
子项[i]。添加(新的JButton((String)i),新的xyconstraint(x,y,w,i*h));
}
}
我编写自己的布局,因为现有的布局管理器不符合我的要求

想法是对的,但是你的实现是错误的
MyLayout layout = new MyLayout(xSize, ySize, xOffset, yOffset);
MyLayout layout = new MyLayout(50, 50, 0, 50);
panel.add( new JButton("1") );
panel.add( new JButton("2") );
panel.add( new JButton("3") );
MyLayout layout = new MyLayout(50, 100, 50, 50);