gpt4 book ai didi

java - JScrollPane 内的 JViewerPane

转载 作者:行者123 更新时间:2023-12-02 06:25:52 25 4
gpt4 key购买 nike

我有一个 JEditorPane,需要附加一个滚动条。我试图将 JEditorPane 嵌套在 JScrollPane 中。这里我提供一个SSCCE。

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

public class Test{

public static void main(String[] args){
JFrame frame = new JFrame();

//Generate filler text to illustrate the lack of a scrollbar
String fillerText = "";
for(int i = 0; i < 500; i++){
fillerText += "The quick brown fox jumped over the lazy dog. ";
}

//Initialize JEditorPane
JEditorPane viewer = new JEditorPane();
viewer.setPreferredSize(new Dimension(500, 600));
viewer.setText(fillerText);

//Initialize JScrollPane
JScrollPane scroll = new JScrollPane(viewer);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

//Add viewer to the frame
frame.add(viewer);

//Make frame visible
frame.pack();
frame.setVisible(true);
}

}

为什么滚动条不可见,如何才能使滚动条可见?

最佳答案

接下来的问题:您将 JEditorPane 添加到 JFrame 而不是 JScrollPane 中。当您需要使用 JScrollPane 时,您可以将组件添加到该 View 并将 JScrollPane 添加到容器。

frame.add(viewer); 替换为 frame.add(scroll); 就可以了。

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

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