Java JPanel在JScrollPane中的位置

Java JPanel在JScrollPane中的位置,java,swing,jpanel,jscrollpane,layout-manager,Java,Swing,Jpanel,Jscrollpane,Layout Manager,有了这段代码,我可以在JScrollPane中轻松创建按钮。我使用了滚动窗格,因为将来按钮会很多 现在,如果只有一个“代码> jPoCTs/CODE”按钮,它会显示在滚动窗格的中间,而应该显示在顶部。p> 有可能吗 JPanel pane = new JPanel(new GridBagLayout()); JButton button; pane.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstra

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

现在,如果只有一个“代码> jPoCTs/CODE”按钮,它会显示在滚动窗格的中间,而应该显示在顶部。p> 有可能吗

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

已编辑 基于装满鳗鱼的气垫船回复,我为多个JPanel创建了代码,但JPanel是水平可见的,而不是垂直可见的:

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);
JPanel pane=newjpanel(newgridbaglayout());
对于(int i=0;i<100;i++){
按钮;
setLayout(新的GridBagLayout());
GridBagConstraints c=新的GridBagConstraints();
c、 填充=GridBagConstraints.HORIZONTAL;
按钮=新的JButton(“按钮1”);
c、 权重x=0.5;
c、 gridx=0;
c、 gridy=0;
窗格。添加(按钮,c);
按钮=新的JButton(“按钮2”);
c、 gridx=0;
c、 gridy=1;
窗格。添加(按钮,c);
按钮=新的JButton(“按钮3”);
c、 gridx=1;
c、 gridy=1;
窗格。添加(按钮,c);
按钮=新的JButton(“长名按钮4”);
c、 ipady=40;
c、 权重x=0.0;
c、 网格宽度=2;
c、 gridx=0;
c、 gridy=2;
窗格。添加(按钮,c);
}
JPanel borderLayoutPanel=newjpanel(new BorderLayout());//包装纸
borderLayoutPanel.add(窗格,BorderLayout.PAGE_START);//将窗格添加到顶部
jScrollPane2.setViewportView(borderLayoutPanel);

创建一个新的“包装器”JPanel,使用
BorderLayout
,将您的窗格添加到包装器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());
    }
}

c.gridwidth=3逻辑是什么?我认为
c.gridwidth=2更合适。顺便说一句-为了更快地获得更好的帮助,请发布一个or。@user2847219:正如安德鲁·汤普森所建议的,如果您仍然被卡住,请编辑您的问题,然后发布一个有效的、小的独立程序,小到可以作为问题中的代码格式文本,一个编译、运行并向我们展示您的问题的程序。@user2847219:还有什么JPanels?在发布的代码中不显示多个水平JPanel。同样,一个体面的MCVE会为我们解答这个问题。@user2847219:请参阅“编辑我的答案”,以获取我们要求您编写的代码类型的示例,并查看如何在JScrollPane中嵌套JPanel。我插入了新的image@user2847219:图像?请阅读或重读我们的评论。没有人在这里要求图像。