我正在尝试添加一个垂直滚动我的java程序文本区。我使用这段代码来创建我的JScrollPane:
console =我的文本区域。
我也宣布JScrollPane垂直;
vertical = new JScrollPane(console);
vertical.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
vertical.setVisible(true);
this.add(vertical);编辑:
对节目的看法:

我对Java很陌生,但这不应该起作用,在我的文本框中添加一个垂直滚动条
我做错了什么?
谢谢你的帮助。
发布于 2011-10-14 11:55:13
下面是一个示例:

import java.awt.Dimension;
import javax.swing.*;
public class ScrolledPane extends JPanel
{
private JScrollPane vertical;
private JTextArea console;
public ScrolledPane()
{
setPreferredSize(new Dimension(200, 250));
console = new JTextArea(15, 15);
vertical = new JScrollPane(console);
vertical.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
add(vertical);
}
public static void main( String args[] )
{
new JFrame()
{{
getContentPane().add(new ScrolledPane());
pack();
setVisible(true);
}};
}
}发布于 2011-10-14 11:51:44
我认为在关于JTextArea和JScrollPane的官方教程中描述了关于这一点的所有内容,还有这里和这里的例子。
mySchroll = new JScrollPane(myTextArea,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);发布于 2018-08-27 03:28:33
import javax.swing.*;
import java.awt.*;
public class Demo extends JFrame {
private JTextArea textArea;
private JScrollPane scroll;
Demo(){
super("Write here...");
textArea = new JTextArea();
scroll = new JScrollPane(textArea);
scroll.setVerticalScrollBarPolicy(22); /*22 is a const value for always vertical */
add(scroll, BorderLayout.CENTER);
setSize(270,270);
setVisible(true);
}
public static void main(String[] args) {
new Demo();
}
}代码的输出:看见
https://stackoverflow.com/questions/7766844
复制相似问题