gpt4 book ai didi

java - 如何将 scrollPane 放在 BoxLayout 上?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:46:55 25 4
gpt4 key购买 nike

我想让我的 JFrame 有一个 BoxLayout 能够有一个滚动 Pane ,这样我就可以继续添加组,就像它是收据或发票打印机。有没有办法添加 ScrollPane

问题:如何给JFrame添加ScrollPane?

代码,最初看到here :

import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

/** @see https://stackoverflow.com/a/8504753/230513 */
public class GroupPanel extends JPanel {
private static final long serialVersionUID = 1L;
private JLabel label1 = new JLabel("Primary:");
private JTextField field1 = new JTextField(16);
private JLabel label2 = new JLabel("Secondary:");
private JTextField field2 = new JTextField(16);
private JLabel label3 = new JLabel("Tertiary:");
private JTextField field3 = new JTextField(16);
JScrollPane sp = new JScrollPane();

public GroupPanel(int n) {
this.setBorder(BorderFactory.createTitledBorder("Panel " + n));
GroupLayout l = new GroupLayout(this);
this.setLayout(l);
l.setAutoCreateGaps(true);
l.setAutoCreateContainerGaps(true);
l.setHorizontalGroup(l.createSequentialGroup()
.addGroup(l.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(label1)
.addComponent(label2)
.addComponent(label3))
.addGroup(l.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(field1)
.addComponent(field2)
.addComponent(field3))
);

l.setVerticalGroup(l.createSequentialGroup()
.addGroup(l.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label1)
.addComponent(field1))
.addGroup(l.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label2)
.addComponent(field2))
.addGroup(l.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label3)
.addComponent(field3))
);

sp.setToolTipText("test");
sp.add(this);
}

private static void display() {
JFrame f = new JFrame("GroupPanel");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS));
f.getContentPane().add(new GroupPanel(1));
f.getContentPane().add(new GroupPanel(2));
f.getContentPane().add(new GroupPanel(3));
f.getContentPane().add(new GroupPanel(4));
f.getContentPane().add(Box.createVerticalGlue());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

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

最佳答案

只需将所有组件先封装到JPanel中,再添加到JScrollPane中,最后将JScrollPane添加到JFrame中>s 内容 Pane 。

示例代码:

JPanel panel = new JPanel();
JScrollPane scrollPane = new JScrollPane(panel);

panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(new GroupPanel(1));
panel.add(new GroupPanel(2));
panel.add(new GroupPanel(3));
panel.add(new GroupPanel(4));
panel.add(Box.createVerticalGlue());

f.getContentPane().add(scrollPane);

截图;

enter image description here

关于java - 如何将 scrollPane 放在 BoxLayout 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23747894/

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