gpt4 book ai didi

java - 如何在 Scrollpane 中以多行排列组件

转载 作者:行者123 更新时间:2023-11-29 05:23:27 25 4
gpt4 key购买 nike

我需要向滚动 Pane 添加大约 600 张图片,但所有图片都是并排排列的

Screenshot

public CollectionPanel(Controller controller)
this.setBackground(Color.white);
this.setLayout(new BorderLayout());
JPanel content = new JPanel();
content.setLayout(new FlowLayout());
JScrollPane scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
for(int i = 0; i < 100; ++i){
content.add(new Sticker(i+1));
}
scrollPane.setViewportView(content);
this.add(scrollPane, BorderLayout.CENTER);
}

如何安排它们在到达屏幕末尾时“换行”?

最佳答案

看看 Rob Camick 的 WrapLayout .

您可以调整框架的大小,所有组件都会为您重新格式化。这是一个示例用法

enter image description here enter image description here

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

public class TestWrapLayout {
public TestWrapLayout () {
ImageIcon icon = new ImageIcon(getClass().getResource("/resources/stackoverflow2.png"));
JPanel panel = new JPanel(new WrapLayout());
for (int i = 1; i <= 250; i++) {
JLabel iconlabel = new JLabel(icon);
iconlabel.setLayout(new BorderLayout());
JLabel textlabel = new JLabel(String.valueOf(i));
textlabel.setHorizontalAlignment(JLabel.CENTER);
textlabel.setForeground(Color.WHITE);
textlabel.setFont(new Font("impact", Font.PLAIN,20));
iconlabel.add(textlabel);
panel.add(iconlabel);
}
JFrame frame = new JFrame();
frame.add(new JScrollPane(panel));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

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

关于java - 如何在 Scrollpane 中以多行排列组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23721981/

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