JPanel 在 JScrollPane 中的位置
JPanel position in a JScrollPane
使用这段代码,我可以轻松地在 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);
已编辑
基于 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);
创建一个新的 "wrapper" 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());
}
}
使用这段代码,我可以轻松地在 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);
已编辑 基于 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);
创建一个新的 "wrapper" 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());
}
}