使GridBagLayout看起来像模式

时间:2013-12-22 23:30:19

标签: java jpanel layout-manager gridbaglayout

我想做这样的事情:

pattern

这是我的代码:

public class MainFrame extends JFrame{

public MainFrame(){
    super("Symulator Lotow");

    JPanel returnPanel = new JPanel(new GridBagLayout());
    add(returnPanel);
    Vector<JButton> v = new Vector<JButton>();
    int licznik = 0;

    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.BOTH;
    c.ipadx = 0; c.ipady = 0;
    c.insets = new Insets(0,0,0,0);
    c.weightx = 1.0;
    c.weighty = 1.0;
    c.anchor = GridBagConstraints.CENTER;

    v.add(new JButton());
    c.gridwidth = 6;
    c.gridheight = 6;
    c.gridx = 0;
    c.gridy = 0;
    v.get(licznik).setBackground(Color.black);
    returnPanel.add(v.get(licznik),c);
    licznik++;

    v.add(new JButton());
    c.gridwidth = 6;
    c.gridheight = 4;
    c.gridx = 6;
    c.gridy = 0;
    v.get(licznik).setBackground(Color.white);
    returnPanel.add(v.get(licznik),c);
    licznik++;

    v.add(new JButton());
    c.gridwidth = 3;
    c.gridheight = 2;
    c.gridx = 6;
    c.gridy = 4;
    v.get(licznik).setBackground(Color.blue);
    returnPanel.add(v.get(licznik),c);
    licznik++;

    v.add(new JButton());
    c.gridwidth = 3;
    c.gridheight = 2;
    c.gridx = 9;
    c.gridy = 4;
    v.get(licznik).setBackground(Color.yellow);
    returnPanel.add(v.get(licznik),c);
    licznik++;

    v.add(new JButton());
    c.gridwidth = 6;
    c.gridheight = 2;
    c.gridx = 0;
    c.gridy = 6;
    v.get(licznik).setBackground(Color.yellow);
    returnPanel.add(v.get(licznik),c);
    licznik++;

    v.add(new JButton());
    c.gridwidth = 6;
    c.gridheight = 2;
    c.gridx = 6;
    c.gridy = 6;
    v.get(licznik).setBackground(Color.yellow);
    returnPanel.add(v.get(licznik),c);
    licznik++;

    v.add(new JButton());
    c.gridwidth = 4;
    c.gridheight = 2;
    c.gridx = 0;
    c.gridy = 8;
    v.get(licznik).setBackground(Color.red);
    returnPanel.add(v.get(licznik),c);
    licznik++;

    v.add(new JButton());
    c.gridwidth = 4;
    c.gridheight = 2;
    c.gridx = 4;
    c.gridy = 8;
    v.get(licznik).setBackground(Color.red);
    returnPanel.add(v.get(licznik),c);
    licznik++;

    v.add(new JButton());
    c.gridwidth = 4;
    c.gridheight = 2;
    c.gridx = 8;
    c.gridy = 8;
    v.get(licznik).setBackground(Color.red);
    returnPanel.add(v.get(licznik),c);
    licznik++;


    this.setExtendedState(JFrame.MAXIMIZED_BOTH);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        

    this.setVisible(true);
    this.setResizable(true);
}
public static void main(String[] args) {
    // TODO Auto-generated method stub
    new MainFrame();
}
}

从我的代码运行时可以看出,它在模式中看起来不像。颜色只是样品,所以我不关心它们。我只是希望按钮的相对大小是正确的。我的错误在哪里?

2 个答案:

答案 0 :(得分:1)

您可以使用Relative Layout

您需要嵌套面板才能获得所需的结果,但这种布局的好处是所有计算都是根据可用空间完成的,而不是基于组件的首选大小。因此,所有组件将保持与帧大小相同的相对大小。

例如:

import java.awt.*;
import java.util.*;
import javax.swing.*;

public class MainFrame3 extends JFrame{

    public MainFrame3(){
        super("Symulator Lotow");

        JButton button = null;
        RelativeLayout layout = new RelativeLayout(RelativeLayout.Y_AXIS);
        layout.setFill(true);
        JPanel returnPanel = new JPanel( layout );
        add(returnPanel);

        // top

        layout = new RelativeLayout(RelativeLayout.X_AXIS);
        layout.setFill(true);
        JPanel top = new JPanel( layout );
        returnPanel.add(top, new Float(6));

        button = new JButton();
        button.setBackground(Color.black);
        top.add(button, new Float(6));

        layout = new RelativeLayout(RelativeLayout.Y_AXIS);
        layout.setFill(true);
        JPanel topRight = new JPanel( layout );
        top.add(topRight, new Float(6));

        button = new JButton();
        button.setBackground(Color.white);
        topRight.add(button, new Float(4));

        layout = new RelativeLayout(RelativeLayout.X_AXIS);
        layout.setFill(true);
        JPanel rightBottom = new JPanel( layout );
        topRight.add(rightBottom, new Float(2));

        button = new JButton();
        button.setBackground(Color.blue);
        rightBottom.add(button, new Float(3));

        button = new JButton();
        button.setBackground(Color.blue);
        rightBottom.add(button, new Float(3));

        // middle

        layout = new RelativeLayout(RelativeLayout.X_AXIS);
        layout.setFill(true);
        JPanel middle = new JPanel( layout );
        returnPanel.add(middle, new Float(2));

        button = new JButton();
        button.setBackground(Color.yellow);
        middle.add(button, new Float(6));

        button = new JButton();
        button.setBackground(Color.yellow);
        middle.add(button, new Float(6));

        // bottom

        layout = new RelativeLayout(RelativeLayout.X_AXIS);
        layout.setFill(true);
        JPanel bottom = new JPanel( layout );
        returnPanel.add(bottom, new Float(2));

        button = new JButton();
        button.setBackground(Color.red);
        bottom.add(button, new Float(4));

        button = new JButton();
        button.setBackground(Color.red);
        bottom.add(button, new Float(4));

        button = new JButton();
        button.setBackground(Color.red);
        bottom.add(button, new Float(4));

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600, 500);
        this.setVisible(true);
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new MainFrame3();
    }
}

答案 1 :(得分:0)

您无需使用gridWidthgridHeight个大号码。使用weightXweightY字段可以更好地处理相对宽度和高度。

public class MainFrame extends JFrame{

    public MainFrame(){
        super("Symulator Lotow");

        JPanel returnPanel = new JPanel(new GridBagLayout());
        add(returnPanel);
        Vector<JButton> v = new Vector<JButton>();
        int licznik = 0;

        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.ipadx = 0; c.ipady = 0;
        c.insets = new Insets(0,0,0,0);
        c.anchor = GridBagConstraints.CENTER;

        v.add(new JButton());
        c.gridwidth = 2;
        c.gridheight = 2;
        c.weightx = 6.0;
        c.weighty = 6.0;
        c.gridx = 0;
        c.gridy = 0;
        v.get(licznik).setBackground(Color.black);
        returnPanel.add(v.get(licznik),c);
        licznik++;

        v.add(new JButton());
        c.gridwidth = 3;
        c.gridheight = 1;
        c.weightx = 6.0;
        c.weighty = 4.0;
        c.gridx = 2;
        c.gridy = 0;
        v.get(licznik).setBackground(Color.white);
        returnPanel.add(v.get(licznik),c);
        licznik++;

        v.add(new JButton());
        c.gridwidth = 2;
        c.gridheight = 1;
        c.weightx = 3.0;
        c.weighty = 2.0;
        c.gridx = 2;
        c.gridy = 1;
        v.get(licznik).setBackground(Color.blue);
        returnPanel.add(v.get(licznik),c);
        licznik++;

        v.add(new JButton());
        c.gridwidth = 1;
        c.gridheight = 1;
        c.weightx = 3.0;
        c.weighty = 2.0;
        c.gridx = 4;
        c.gridy = 1;
        v.get(licznik).setBackground(Color.yellow);
        returnPanel.add(v.get(licznik),c);
        licznik++;

        v.add(new JButton());
        c.gridwidth = 2;
        c.gridheight = 1;
        c.weightx = 6.0;
        c.weighty = 2.0;
        c.gridx = 0;
        c.gridy = 2;
        v.get(licznik).setBackground(Color.yellow);
        returnPanel.add(v.get(licznik),c);
        licznik++;

        v.add(new JButton());
        c.gridwidth = 3;
        c.gridheight = 1;
        c.weightx = 6.0;
        c.weighty = 2.0;
        c.gridx = 2;
        c.gridy = 2;
        v.get(licznik).setBackground(Color.yellow);
        returnPanel.add(v.get(licznik),c);
        licznik++;

        v.add(new JButton());
        c.gridwidth = 1;
        c.gridheight = 1;
        c.weightx = 4.0;
        c.weighty = 2.0;
        c.gridx = 0;
        c.gridy = 3;
        v.get(licznik).setBackground(Color.red);
        returnPanel.add(v.get(licznik),c);
        licznik++;

        v.add(new JButton());
        c.gridwidth = 2;
        c.gridheight = 1;
        c.weightx = 4.0;
        c.weighty = 2.0;
        c.gridx = 1;
        c.gridy = 3;
        v.get(licznik).setBackground(Color.red);
        returnPanel.add(v.get(licznik),c);
        licznik++;

        v.add(new JButton());
        c.gridwidth = 2;
        c.gridheight = 1;
        c.weightx = 4.0;
        c.weighty = 2.0;
        c.gridx = 3;
        c.gridy = 3;
        v.get(licznik).setBackground(Color.red);
        returnPanel.add(v.get(licznik),c);
        licznik++;


        this.setExtendedState(JFrame.MAXIMIZED_BOTH);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        

        this.setVisible(true);
        this.setResizable(true);
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new MainFrame();
    }
}