Java:表单布局参数的说明

时间:2012-12-17 22:10:24

标签: java swing layout layout-manager form-layout

我在其中一个课程中定义了以下的swing布局,但我无法理解发生了什么。

private void initComponents()
    {
        locationPanel = new JPanel();
        label1 = new JLabel();
        lastUpdateLabel = new JLabel();
        label2 = new JLabel();
        accuracyLabel = new JLabel();
        batteryPanel = new JPanel();
        batteryLabel = new JLabel();
        label3 = new JLabel();
        cc = new CellConstraints();

        setLayout(new FormLayout("default", "default"));

        locationPanel.setBorder(new TitledBorder("Info"));
        locationPanel.setLayout(new FormLayout("right:pref, 6dlu, 50dlu, 4dlu, default", "pref, 3dlu, pref, 3dlu, pref"));


        label1.setText("Last Update:");
        locationPanel.add(label1, cc.xy(1, 1));

        lastUpdateLabel.setText("Unknown");
        locationPanel.add(lastUpdateLabel, cc.xy(3, 1));


        label2.setText("Accuracy:");
        locationPanel.add(label2, cc.xy(1, 3));

        // ---- accuracyLabel ----
        accuracyLabel.setText("Unknown");
        locationPanel.add(accuracyLabel, cc.xy(3, 3));

        label3.setText("Battery Level");
        locationPanel.add(label3, cc.xy(1, 5));


        batteryLabel.setText("Unknown");
        locationPanel.add(batteryLabel, cc.xy(3, 5));

        add(locationPanel, cc.xy(1, 1));
    }

问题:我认为它应该有3行2列?定义了多少行和列;我如何制作4 x 2布局?我如何确定这是什么? locationPanel.setLayout(new FormLayout("right:pref, 6dlu, 50dlu, 4dlu, default", "pref, 3dlu, pref, 3dlu, pref"));

1 个答案:

答案 0 :(得分:2)

http://www.java2s.com/Code/Java/Swing-Components/FormLayoutExplicitAlignmentExample3.htm

你的布局有5列3行。您可以从示例中推断出这一点。