1

我尝试使用 a 制作图表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);
}

}
4

2 回答 2

3

删除将 innerPanel 添加到滚动窗格的所有行。相反,在 main 方法的末尾添加这两行:

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);
    }

}
于 2013-07-17T10:57:18.563 回答
0

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

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

这应该有帮助

于 2013-07-17T09:19:22.023 回答