JPanel在JScrollPane中的位置

时间:2017-12-10 17:15:45

标签: java swing jpanel jscrollpane layout-manager

使用此代码,我可以轻松地在JScrollPane内创建按钮。我使用了滚动窗格,因为将来按钮会很多。

现在,如果按钮只有JPanel,它会显示在滚动窗格的中间,而应显示在顶部。

有可能吗?

JPanel pane = new JPanel(new GridBagLayout());

JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;

button = new JButton("Button 1");
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);

button = new JButton("Button 2");
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);

button = new JButton("Button 3");
c.gridx = 1;
c.gridy = 1;
pane.add(button, c);

button = new JButton("Long-Named Button 4");
c.ipady = 40;     
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 2;
pane.add(button, c);

jScrollPane2.setViewportView(pane);

enter image description here

编辑 基于Hovercraft Full Of Eels的回复我创建了多个Jpanel的代码,但是Jpanels是水平可见的而不是垂直的:

JPanel pane = new JPanel(new GridBagLayout());
        for (int i = 0; i < 100; i++) {

            JButton button;
            pane.setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            c.fill = GridBagConstraints.HORIZONTAL;

            button = new JButton("Button 1");
            c.weightx = 0.5;
            c.gridx = 0;
            c.gridy = 0;
            pane.add(button, c);

            button = new JButton("Button 2");
            c.gridx = 0;
            c.gridy = 1;
            pane.add(button, c);

            button = new JButton("Button 3");
            c.gridx = 1;
            c.gridy = 1;
            pane.add(button, c);

            button = new JButton("Long-Named Button 4");
            c.ipady = 40;
            c.weightx = 0.0;
            c.gridwidth = 2;
            c.gridx = 0;
            c.gridy = 2;
            pane.add(button, c);

        }
        JPanel borderLayoutPanel = new JPanel(new BorderLayout()); // wrapper JPanel
        borderLayoutPanel.add(pane, BorderLayout.PAGE_START); // add pane to the top

        jScrollPane2.setViewportView(borderLayoutPanel);

enter image description here

1 个答案:

答案 0 :(得分:1)

创建一个新的&#34;包装器&#34;使用BorderLayout的JPanel将您的窗格添加到包装器JPanel的BorderLayout.PAGE_START位置,然后将相同的包装器JPanel添加到JScrollPane的视口中。例如:

JPanel borderLayoutPanel = new JPanel(new BorderLayout()); // wrapper JPanel
borderLayoutPanel.add(pane, BorderLayout.PAGE_START); // add pane to the top
jScrollPane2.setViewportView(borderLayoutPanel); // add wrapper to viewport

有关工作MCVE的示例,请查看:

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

@SuppressWarnings("serial")
public class Foo extends JPanel {
    private static final int PREF_W = 400;
    private static final int PREF_H = 600;
    private JPanel gridPanel = new JPanel(new GridLayout(0, 1));
    public Foo() {
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(new JButton(new AbstractAction("Add Panel") {

            @Override
            public void actionPerformed(ActionEvent e) {
                gridPanel.add(createButtonPanel());
                gridPanel.revalidate();
                gridPanel.repaint();
            }
        }));

        JPanel borderLayoutPanel = new JPanel(new BorderLayout());
        borderLayoutPanel.add(gridPanel, BorderLayout.PAGE_START);
        JScrollPane scrollPane = new JScrollPane(borderLayoutPanel);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        setPreferredSize(new Dimension(PREF_W, PREF_H));
        setLayout(new BorderLayout());
        add(scrollPane);
        add(buttonPanel, BorderLayout.PAGE_END);
    }

    private JPanel createButtonPanel() {
        JPanel pane = new JPanel(new GridBagLayout());
        pane.setBorder(BorderFactory.createLineBorder(Color.BLUE));

        JButton button;
        pane.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.PAGE_START;

        button = new JButton("Button 1");
        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 0;
        pane.add(button, c);

        button = new JButton("Button 2");
        c.gridx = 0;
        c.gridy = 1;
        pane.add(button, c);

        button = new JButton("Button 3");
        c.gridx = 1;
        c.gridy = 1;
        pane.add(button, c);

        button = new JButton("Long-Named Button 4");
        c.ipady = 40;     
        c.weightx = 0.0;
        c.gridwidth = 3;
        c.gridx = 0;
        c.gridy = 2;
        pane.add(button, c);
        return pane;
    }

    private static void createAndShowGui() {
        Foo mainPanel = new Foo();

        JFrame frame = new JFrame("Foo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}