我们如何在 Java 中的 JTextArea 上添加 JScrollPane?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19212126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can we add JScrollPane on JTextArea in java?
提问by AR7
Can anybody tell me what is the problem in following program? I want to fit JScrollPane
on JtextArea
but when I add it then JTextArea
is not visible.
谁能告诉我下面的程序有什么问题?我想适应JScrollPane
,JtextArea
但是当我添加它时,它JTextArea
是不可见的。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Area extends JFrame
{
private JTextArea ta;
private JTextField tf;
JScrollPane jp;
public Area()
{
super("Text Area");
tf=new JTextField();
tf.setBounds(100,350,300,30);
add(tf);
ta=new JTextArea();
ta.setBounds(100,100,300,200);
jp= new JScrollPane(ta);
add(jp);
setLayout(null);
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String...s)
{
new Area();
}
}
回答by Sylwek
Try this:
尝试这个:
public Area()
{
super("Text Area");
tf=new JTextField();
tf.setBounds(100,350,300,30);
add(tf);
ta=new JTextArea();
jp= new JScrollPane(ta);
jp.setBounds(5, 5, 100, 100);
add(jp);
setLayout(null);
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
You have to use setBounds
on JScrollPane, not on JTextArea
您必须setBounds
在 JScrollPane 上使用,而不是在 JTextArea 上使用
回答by Foo Bar User
sounds like its added but its not shown because of the policy try this:
听起来像是添加了但由于政策而未显示,请尝试以下操作:
jp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
回答by trashgod
I see several problems:
我看到几个问题:
Don't use a
null
layout; do use a real layout.The default layout of
JFrame
isBorderLayout
; the default position isCENTER
; only one component can occupy a position at a time; the example below usesNORTH
&CENTER
.Use the appropriate constructor parameters to size the text components initially.
The scrollbar will appear automatically whenever the scrollpane is smaller than the enclosed component; resize the frame to see the effect.
As shown here, the frame's size is made smaller for effect.
See also Initial Threads.
不要使用
null
布局;使用真正的布局。的默认布局
JFrame
是BorderLayout
; 默认位置是CENTER
; 一次只能有一个组件占据一个位置;下面的示例使用NORTH
&CENTER
。最初使用适当的构造函数参数来调整文本组件的大小。
只要滚动窗格小于封闭的组件,滚动条就会自动出现;调整框架大小以查看效果。
如图所示这里,帧的大小是由用于影响较小。
另请参阅初始线程。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/** @see https://stackoverflow.com/a/19215436/230513 */
public class Area extends JFrame {
public Area() {
super("Text Area");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField tf = new JTextField(12);
add(tf, BorderLayout.NORTH);
JTextArea ta = new JTextArea(24, 12);
JScrollPane jp = new JScrollPane(ta);
add(jp, BorderLayout.CENTER);
pack();
// arbitrary size to make vertical scrollbar appear
setSize(240, 240);
setLocationByPlatform(true);
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Area();
}
});
}
}