[英]How can we add JScrollPane on JTextArea in java?
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. 我想在
JtextArea
上使用JScrollPane
,但是当我添加它时, 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();
}
}
I see several problems: 我看到几个问题:
Don't use a null
layout; 不要使用
null
布局; do use a real layout . 使用真实的布局 。
The default layout of JFrame
is BorderLayout
; JFrame
的默认布局是BorderLayout
; the default position is CENTER
; 默认位置是
CENTER
; only one component can occupy a position at a time; 只有一个组件可以一次占据一个位置; the example below uses
NORTH
& CENTER
. 以下示例使用
NORTH
& 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 . 另请参见初始线程 。
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();
}
});
}
}
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 您必须在JScrollPane上使用
setBounds
,而不是在JTextArea上使用
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.