coo*_*ird 72
正如弗雷德里克在他的回答中提到的那样,实现这一目标的简单方法就是将其JTextArea
放入JScrollPane
.这将允许滚动视图区域JTextArea
.
仅仅为了完整起见,以下是如何实现:
JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta); // JTextArea is placed in a JScrollPane.
Run Code Online (Sandbox Code Playgroud)
一旦JTextArea
包含在内JScrollPane
,JScrollPane
应将其添加到文本区域应该的位置.在以下示例中,带有滚动条的文本区域将添加到JFrame
:
JFrame f = new JFrame();
f.getContentPane().add(sp);
Run Code Online (Sandbox Code Playgroud)
谢谢kd304在评论中提到应该添加JScrollPane
到容器而不是JTextArea
- 我觉得将文本区域本身添加到目标容器而不是带有文本区域的滚动窗格是一个常见错误.
The Java Tutorials的以下文章有更多细节:
Fre*_*rik 18
把它放在JScrollPane中
编辑:这是一个链接:http://java.sun.com/docs/books/tutorial/uiswing/components/textarea.html
小智 15
您首先必须按照惯例定义JTextArea:
public final JTextArea mainConsole = new JTextArea("");
Run Code Online (Sandbox Code Playgroud)
然后你将一个JScrollPane放在TextArea上
JScrollPane scrollPane = new JScrollPane(mainConsole);
scrollPane.setBounds(10,60,780,500);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
Run Code Online (Sandbox Code Playgroud)
最后一行表示垂直滚动条将始终存在.横向有一个类似的命令.否则,滚动条只会在需要时显示(或者从不显示,如果使用_SCROLLBAR_NEVER).我想这是你想要使用它的方式.
如果要执行以下操作,还可以将Wordwrap添加到JTextArea:Guide Here
祝你好运,
Norm M.
PS确保将ScrollPane添加到JPanel而不添加JTextArea.
txtarea = new JTextArea();
txtarea.setRows(25);
txtarea.setColumns(25);
txtarea.setWrapStyleWord(true);
JScrollPane scroll = new JScrollPane (txtarea);
panel2.add(scroll); //Object of Jpanel
Run Code Online (Sandbox Code Playgroud)
以上给定的线条会自动显示水平和垂直滚动条.
小智 5
使用 JScrollPan 在 JScrollBar 中添加 JTextArea 的简单方法
import javax.swing.*;
public class ScrollingTextArea
{
JFrame f;
JTextArea ta;
JScrollPane scrolltxt;
public ScrollingTextArea()
{
// TODO Auto-generated constructor stub
f=new JFrame();
f.setLayout(null);
f.setVisible(true);
f.setSize(500,500);
ta=new JTextArea();
ta.setBounds(5,5,100,200);
scrolltxt=new JScrollPane(ta);
scrolltxt.setBounds(3,3,400,400);
f.add(scrolltxt);
}
public static void main(String[] args)
{
new ScrollingTextArea();
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
126915 次 |
最近记录: |