将JPanels安排在ScrollPane的顶部,而不是居中

时间:2014-07-31 11:26:11

标签: java swing user-interface jscrollpane jpanelmenu

以下代码将我的问题重现为SSCCE。我想在JPanels内创建JScrollPane的垂直可滚动列表。我的实际代码在中心对齐JPanels,但应该在顶部对齐。 我怎么能这样做,感谢任何帮助。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
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 TestingWindow {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.setBounds(100, 100, 150, 200);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JScrollPane scrollPane = new JScrollPane();
                frame.getContentPane().add(scrollPane, BorderLayout.CENTER);

                JPanel panel = new JPanel();
                GridBagLayout gbl_panel = new GridBagLayout();
                panel.setLayout(gbl_panel);
                scrollPane.setViewportView(panel);

                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                gbc.anchor = GridBagConstraints.NORTH;
                gbc.weightx = 1;

                gbc.fill = GridBagConstraints.HORIZONTAL;

                JPanel panelToAdd = new JPanel();
                JLabel label = new JLabel("Test 1");
                panelToAdd.setBackground(Color.blue);
                panelToAdd.add(label);
                panelToAdd.setAlignmentY(Component.TOP_ALIGNMENT);
                panel.add(panelToAdd, gbc, -1);
                panel.revalidate();

                JPanel panelToAdd2 = new JPanel();
                JLabel label2 = new JLabel("Test 2");
                panelToAdd2.setBackground(Color.green);
                panelToAdd2.add(label2);
                panel.add(panelToAdd2, gbc, -1);
                panel.revalidate();

                JPanel panelToAdd3 = new JPanel();
                JLabel label3 = new JLabel("Test 3");
                panelToAdd3.setBackground(Color.yellow);
                panelToAdd3.add(label3);
                panel.add(panelToAdd3, gbc, -1);
                panel.revalidate();
                frame.setVisible(true);
            }
        });
    }

}

基于@ MadProgrammer解决方案的解决方案:

添加以下代码以在最后创建一个“填充”不可见组件:

    GridBagConstraints gbc_finish = new GridBagConstraints();
    gbc_finish.gridwidth = GridBagConstraints.REMAINDER;
    gbc_finish.anchor = GridBagConstraints.NORTH;
    gbc_finish.weighty = 1;
    JPanel panelToAdd4 = new JPanel();
    panelToAdd4.setOpaque(false);
    panel.add(panelToAdd4, gbc_finish, -1);
    panel.revalidate();

2 个答案:

答案 0 :(得分:0)

您是否尝试将它们放置在边框布局的北部面板中?

frame.getContentPane().add(scrollPane, BorderLayout.NORTH);

答案 1 :(得分:0)

一个选项可能是在JPanel的末尾添加填充panel,但有以下限制:

    GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.weighty = 1.0;
    panel.add(new JPanel(), gridBagConstraints);