带垂直滚动条的GridBagLayout

时间:2013-07-17 09:11:55

标签: java swing jpanel gridbaglayout

我尝试使用GridBagLayout创建一个图表,我承认这很难,主要是因为它似乎不是最好的方法......

现在我尝试使用垂直滚动条,这样我基本上可以在GridBagLayout中拥有尽可能多的行,但我无法找到方法。我尝试了最直接(也许是天真的)方式,但它看起来不起作用:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class TestCode2_Scrollbar {

public static void main(String[] args) {

    JFrame window = new JFrame("Test");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(400, 200);
    window.setMinimumSize(new Dimension(350, 150));

    JPanel panelMain = new JPanel(new BorderLayout());      
    window.add(panelMain);

    //Comment the next 2 lines when using without scroll bar
    JScrollPane scrollPanel = new JScrollPane();
    panelMain.add(scrollPanel, BorderLayout.CENTER);

    GridBagLayout innerLayout = new GridBagLayout();
    GridBagConstraints innerConstraints = new GridBagConstraints();     
    JPanel innerPanel = new JPanel(innerLayout);
    innerPanel.setBackground(new Color(0, 220, 250));

    //Comment one of the 2 following lines:
    scrollPanel.add(innerPanel);        
    //panelMain.add(innerPanel, BorderLayout.CENTER);

    innerConstraints.weightx = 0.0;
    innerConstraints.weighty = 0.0;
    innerConstraints.gridy = 0;

    innerConstraints.gridx = 0;
    JLabel lblTitle = new JLabel("Title / ");
    innerLayout.setConstraints(lblTitle, innerConstraints);
    innerPanel.add(lblTitle);               

    innerConstraints.gridx = 1;
    JLabel lblRating = new JLabel("Rating / ");
    innerLayout.setConstraints(lblRating, innerConstraints);
    innerPanel.add(lblRating);

    innerConstraints.gridx = 2;
    JLabel lblCategoryCode = new JLabel("Category code / ");
    innerLayout.setConstraints(lblCategoryCode, innerConstraints);
    innerPanel.add(lblCategoryCode);

    innerConstraints.gridx = 3;
    innerConstraints.weightx = 1.0;
    innerConstraints.fill = GridBagConstraints.HORIZONTAL;
    JLabel lblDescription = new JLabel("Description");
    innerLayout.setConstraints(lblDescription, innerConstraints);
    innerPanel.add(lblDescription);

    int max = 4;

    for(int i = 0; i < max; i++) {

        innerConstraints.weightx = 0.0;
        innerConstraints.fill = GridBagConstraints.NONE;
        innerConstraints.anchor = GridBagConstraints.NORTHWEST;
        innerConstraints.gridy = i + 1;

        innerConstraints.gridx = 0;
        lblTitle = new JLabel("" + i);
        innerLayout.setConstraints(lblTitle, innerConstraints);
        innerPanel.add(lblTitle);           

        innerConstraints.gridx = 1;
        lblRating = new JLabel("" + i);
        innerLayout.setConstraints(lblRating, innerConstraints);
        innerPanel.add(lblRating);

        innerConstraints.gridx = 2;
        lblCategoryCode = new JLabel("" + i);
        innerLayout.setConstraints(lblCategoryCode, innerConstraints);
        innerPanel.add(lblCategoryCode);

        innerConstraints.gridx = 3;
        innerConstraints.weightx = 1.0;
        innerConstraints.fill = GridBagConstraints.HORIZONTAL;
        lblDescription = new JLabel("" + i);
        innerLayout.setConstraints(lblDescription, innerConstraints);
        innerPanel.add(lblDescription);

    }

    innerConstraints.gridy = max + 1;

    JPanel innerVoidPanel = new JPanel();
    innerVoidPanel.setBackground(new Color(0, 250, 250));
    innerConstraints.weighty = 1.0;
    innerConstraints.fill = GridBagConstraints.VERTICAL;
    innerLayout.setConstraints(innerVoidPanel, innerConstraints);
    innerPanel.add(innerVoidPanel);

    //...

    scrollPanel.add(innerPanel);        
    window.setVisible(true);
}

}

2 个答案:

答案 0 :(得分:3)

删除将innerPanel添加到滚动窗格的所有行。而是在主方法的末尾添加这两行:

JScrollPane scrollPanel = new JScrollPane(innerPanel);
panelMain.add(scrollPanel, BorderLayout.CENTER);

完整代码:

public class TestCode2_Scrollbar {

    public static void main(String[] args) {

        JFrame window = new JFrame("Test");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(400, 200);
        window.setMinimumSize(new Dimension(350, 150));

        JPanel panelMain = new JPanel(new BorderLayout());
        window.add(panelMain);

        //Comment the next 2 lines when using without scroll bar

        GridBagLayout innerLayout = new GridBagLayout();
        GridBagConstraints innerConstraints = new GridBagConstraints();
        JPanel innerPanel = new JPanel(innerLayout);
        innerPanel.setBackground(new Color(0, 220, 250));

        //Comment one of the 2 following lines:
        //panelMain.add(innerPanel, BorderLayout.CENTER);

        innerConstraints.weightx = 0.0;
        innerConstraints.weighty = 0.0;
        innerConstraints.gridy = 0;

        innerConstraints.gridx = 0;
        JLabel lblTitle = new JLabel("Title / ");
        innerLayout.setConstraints(lblTitle, innerConstraints);
        innerPanel.add(lblTitle);

        innerConstraints.gridx = 1;
        JLabel lblRating = new JLabel("Rating / ");
        innerLayout.setConstraints(lblRating, innerConstraints);
        innerPanel.add(lblRating);

        innerConstraints.gridx = 2;
        JLabel lblCategoryCode = new JLabel("Category code / ");
        innerLayout.setConstraints(lblCategoryCode, innerConstraints);
        innerPanel.add(lblCategoryCode);

        innerConstraints.gridx = 3;
        innerConstraints.weightx = 1.0;
        innerConstraints.fill = GridBagConstraints.HORIZONTAL;
        JLabel lblDescription = new JLabel("Description");
        innerLayout.setConstraints(lblDescription, innerConstraints);
        innerPanel.add(lblDescription);

        int max = 4;

        for(int i = 0; i < max; i++) {

            innerConstraints.weightx = 0.0;
            innerConstraints.fill = GridBagConstraints.NONE;
            innerConstraints.anchor = GridBagConstraints.NORTHWEST;
            innerConstraints.gridy = i + 1;

            innerConstraints.gridx = 0;
            lblTitle = new JLabel("" + i);
            innerLayout.setConstraints(lblTitle, innerConstraints);
            innerPanel.add(lblTitle);

            innerConstraints.gridx = 1;
            lblRating = new JLabel("" + i);
            innerLayout.setConstraints(lblRating, innerConstraints);
            innerPanel.add(lblRating);

            innerConstraints.gridx = 2;
            lblCategoryCode = new JLabel("" + i);
            innerLayout.setConstraints(lblCategoryCode, innerConstraints);
            innerPanel.add(lblCategoryCode);

            innerConstraints.gridx = 3;
            innerConstraints.weightx = 1.0;
            innerConstraints.fill = GridBagConstraints.HORIZONTAL;
            lblDescription = new JLabel("" + i);
            innerLayout.setConstraints(lblDescription, innerConstraints);
            innerPanel.add(lblDescription);

        }

        innerConstraints.gridy = max + 1;

        JPanel innerVoidPanel = new JPanel();
        innerVoidPanel.setBackground(new Color(0, 250, 250));
        innerConstraints.weighty = 1.0;
        innerConstraints.fill = GridBagConstraints.VERTICAL;
        innerLayout.setConstraints(innerVoidPanel, innerConstraints);
        innerPanel.add(innerVoidPanel);

        //...

        JScrollPane scrollPanel = new JScrollPane(innerPanel);
        panelMain.add(scrollPanel, BorderLayout.CENTER);

        window.setVisible(true);
    }

}

答案 1 :(得分:0)

代码对我来说很好看。如果您想要始终查看滚动条,请尝试为滚动窗格设置滚动条策略,如下所示。

scrollPanel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

这应该有帮助