gpt4 book ai didi

java - JScrollPane 中的 JPanel 位置

转载 作者:行者123 更新时间:2023-12-02 11:53:30 26 4
gpt4 key购买 nike

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

现在,如果只有一个带有按钮的 JPanel,它会显示在滚动 Pane 的中间,而不是应该显示在顶部。

可以吗?

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 回复,我为多个 Jpanels 创建了代码,但 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

最佳答案

创建一个使用 BorderLayout 的新“包装器”JPanel,将 Pane 添加到包装器 JPanel 的 BorderLayout.PAGE_START 位置,然后将相同的包装器 JPanel 添加到JScrollPane 的视口(viewport)。例如:

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

关于java - JScrollPane 中的 JPanel 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47741358/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com