gpt4 book ai didi

java - JScrollPane 不滚动

转载 作者:行者123 更新时间:2023-11-30 04:37:33 24 4
gpt4 key购买 nike

我正在打印从文本文件扫描到 JPanel 的文本。文本显示出来,但没有向下滚动。有任何想法吗?这是我的代码:

rFrame = new JFrame();
rFrame.setBounds(10, 10, 502, 502);
rFrame.getContentPane().setLayout(null);


JPanel pan = new JPanel();
pan.setBackground(Color.WHITE);
pan.setBounds(100, 100, 400, 400);
rFrame.getContentPane().add(pan);
pan.setEnabled(false);

JScrollPane scrollBar=new JScrollPane(pan,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

rFrame.add(scrollBar);

JTextArea area = new JTextArea();
area.setText(printToFrame()); //Function prints from text file
pan.add(area);
rFrame.add(pan);

rFrame.setVisible(true);
rFrame.setLayout(new FlowLayout());
rFrame.getContentPane().add(pan);
rFrame.pack();

ClientWindow window = new ClientWindow();
window.rFrame.setVisible(true);

最佳答案

它不会向下滚动,因为 JPanel 的尺寸不大于 JScrollPane 的视口(viewport)。您必须通过调用 *Object name*.setPreferredSize(new Dimension(int w, int h)) 来增加 JPanel 的尺寸,OR 通过在 JTextArea 内显示文本来实现此目的,这是为此目的而制作的组件。

例如:

public static void main(String[] args) {
JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta);

// disables editing
ta.setEditable(false);

// enable line wrap to wrap text around
ta.setLineWrap(true);

// words will not be cut off when wrapped around
ta.setWrapStyleWord(true);

// displays the text you read in
ta.append( *text you read in* );
}

Oracle 有一个关于如何使用 JTextAreas 的页面,您还可以查看API供其他方法使用

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

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