gpt4 book ai didi

java - 我的 JScrollPane 不会在 JPanel 上垂直滚动

转载 作者:行者123 更新时间:2023-12-02 02:55:48 27 4
gpt4 key购买 nike

我有一个代码,其中有很多“嵌套”布局。 Border、Flow,在我的主程序中我有一个 GridBag。我将面板动态添加到一个面板,并调整其大小,以便每个元素都垂直添加,但不会向上或向下滚动。我在网上查找了类似的内容,但这只允许我水平滚动,而不是像我想要的那样垂直滚动。这是我编写的测试代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Main {

public Main() {
JFrame frame = new JFrame("Blah");

Font font = new Font("Sans-Serif", Font.BOLD, 30);

JPanel memoryPanel = new JPanel();
memoryPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
//memoryPanel.setPreferredSize(new Dimension(80 * 4, 55 * 9));

JScrollPane s = new JScrollPane(memoryPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
s.getViewport().setPreferredSize(new Dimension(80 * 4, 55 * 9));

for (int i = 0; i <= 10; i++) {

JPanel nextPanel = new JPanel();
nextPanel.setLayout(new BorderLayout());
nextPanel.setBackground(Color.CYAN);
nextPanel.setForeground(Color.BLACK);

JPanel labelPanel = new JPanel();
labelPanel.setLayout(new BorderLayout());

JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.TRAILING, 0, 0));

JLabel label = new JLabel();
label.setText("" + i);
label.setPreferredSize(new Dimension(80 * 4, 55));
label.setHorizontalAlignment(JLabel.RIGHT);
label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));

JButton button1 = new JButton("A");
JButton button2 = new JButton("B");
JButton button3 = new JButton("C");

label.setFont(font);
labelPanel.add(label);

buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);

nextPanel.add(labelPanel, BorderLayout.PAGE_START);
nextPanel.add(buttonPanel, BorderLayout.PAGE_END);

memoryPanel.add(nextPanel, 0);
memoryPanel.revalidate();
memoryPanel.repaint();
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(s);
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
new Main();
}
}

使用该注释行,它会显示水平滚动条。由于没有评论,因此不会显示任何内容。不确定我是否使用了错误的布局,但我已经尝试修复它几个小时了。我添加的行是 s.getViewport().setPreferredSize(new Dimension(80 * 4, 55 * 9)); ,这使得所有内容都水平而不是垂直。不知道如何解决这个问题。有建议吗?

最佳答案

首先删除s.getViewport().setPreferredSize(new Dimension(80 * 4, 55 * 9));。您不想更改视口(viewport)的大小,它是内容的容器,并充当显示组件可见部分的“窗口”。

ScrollPane

接下来,去掉frame.setResizes(false);,这将允许 pack 实际上将窗口打包到内容周围。由于您使用的是 FlowLayout,这往往会使窗口与屏幕一样宽。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class Main {

public Main() {
JFrame frame = new JFrame("Blah");

Font font = new Font("Sans-Serif", Font.BOLD, 30);

JPanel memoryPanel = new JPanel();
memoryPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
//memoryPanel.setPreferredSize(new Dimension(80 * 4, 55 * 9));

JScrollPane s = new JScrollPane(memoryPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// s.getViewport().setPreferredSize(new Dimension(80 * 4, 55 * 9));

for (int i = 0; i <= 10; i++) {

JPanel nextPanel = new JPanel();
nextPanel.setLayout(new BorderLayout());
nextPanel.setBackground(Color.CYAN);
nextPanel.setForeground(Color.BLACK);

JPanel labelPanel = new JPanel();
labelPanel.setLayout(new BorderLayout());

JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.TRAILING, 0, 0));

JLabel label = new JLabel();
label.setText("" + i);
label.setPreferredSize(new Dimension(80 * 4, 55));
label.setHorizontalAlignment(JLabel.RIGHT);
label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));

JButton button1 = new JButton("A");
JButton button2 = new JButton("B");
JButton button3 = new JButton("C");

label.setFont(font);
labelPanel.add(label);

buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);

nextPanel.add(labelPanel, BorderLayout.PAGE_START);
nextPanel.add(buttonPanel, BorderLayout.PAGE_END);

memoryPanel.add(nextPanel, 0);
memoryPanel.revalidate();
memoryPanel.repaint();
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(s);
// frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Main();
}
});
}
}

如果您想影响可 ScrollView 区域的大小,那么您将需要创建一个实现 Scrollable 的自定义类。接口(interface)并根据您的需求返回适当的值

关于java - 我的 JScrollPane 不会在 JPanel 上垂直滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43106207/

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