简体   繁体   English

将JScrollPane添加到JTextArea

[英]Add JScrollPane to JTextArea

In my program one user should fill a "Object" field with text of an arbitrary length. 在我的程序中,一个用户应该用任意长度的文本填充“对象”字段。 So I'd like to create a JTextArea with reasonable dimensions and with an associated JSrollPane in order to read all the inserted text, if it's very long. 因此,我想创建一个具有合理尺寸并带有关联的JSrollPane的JTextArea,以便读取所有插入的文本(如果很长)。 This is what I've done: 这是我所做的:

    body.add(new JLabel("OGGETTO"), "1,2");

    JTextArea oggetto = new JTextArea(5,20);
    oggetto.setOpaque(true);
    oggetto.setBackground(Color.cyan);

    Border borderOgg = BorderFactory.createLineBorder(Color.BLACK);
    oggetto.setBorder(BorderFactory.createCompoundBorder(borderOgg,
            BorderFactory.createEmptyBorder(1, 1, 1, 1)));

    oggetto.setLineWrap(true);
    oggetto.setWrapStyleWord(true);

    JScrollPane scroll1 = new JScrollPane(oggetto);
    scroll1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    body.add(scroll1,"3,2");

    body.add(oggetto, "2,2");

where body is a JPanel whose layout is TableLayout. 其中body是一个JPanel,其布局为TableLayout。 But the scroll doesn't work even if it's showed. 但是,即使显示了滚动条也不起作用。 Why? 为什么?

remove below line 删除以下行

body.add(oggetto, "2,2");

because JTextArea is already added in JScrollPane hence there is no need to add it again. 因为JScrollPane已经添加了JTextArea ,所以不需要再次添加它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM