简体   繁体   English

阻止JTextArea扩展

[英]Stop JTextArea from expanding

I'm trying to read information off of a data file and process it into a JTextArea for an about file for a term project that I'm making. 我正在尝试从数据文件中读取信息并将其处理到JTextArea ,以获取我正在制作的术语项目的about文件。 The problem is that when I do scanner.nextLine() it's expanding the text area to fit the text and what I wanted was it to keep the size of the text area the same and if it runs out of space on the current line to start on the 问题是,当我执行scanner.nextLine()它正在扩展文本区域以适应文本,我想要的是保持文本区域的大小相同,如果它在当前行上的空间不足则启动在...上

    if(event.getSource() == forward)
    {
        text.setText(s.nextLine());
    }

The text area is setup like this: 文本区域设置如下:

     text = new JTextArea(11, 30); 

Basically what I want to know is if there's a method that text area offers (I looked) that relocates space to the next line if the current is full. 基本上我想知道的是,如果有一个文本区域提供的方法(我看),如果当前已满,则将空间重定位到下一行。 I can most likely just edit the data file if there isn't a way to do it in the text area. 如果在文本区域中没有办法,我很可能只编辑数据文件。

Place the JTextArea within a JScrollPane and add the scroll pane to your container instead JTextArea放在JScrollPane然后将滚动窗格添加到容器中

text = new JTextArea(11, 30);
JScrollPane scrollpane = new JScrollPane(text);
//...
add(scrollpane);

See How to use Scroll Panes for more details 有关更多详细信息,请参见如何使用滚动窗格

This will prevent the scroll pane from growing in the current container. 这将阻止滚动窗格在当前容器中增长。

Also check out JTextArea#setLineWrap and JTextArea#setWrapStyleWord which will effect how the component treats text that would exceed the viewable width. 另请查看JTextArea#setLineWrapJTextArea#setWrapStyleWord ,它将影响组件如何处理超出可查看宽度的文本。

See How to use Text Areas for more details 有关详细信息,请参见如何使用文本区域

You can use JTextArea#setLineWrap(true) by default is set to false. 您可以使用JTextArea #setLineWrap(true)默认设置为false。

Sets the line-wrapping policy of the text area. 设置文本区域的换行策略。 If set to true the lines will be wrapped if they are too long to fit within the allocated width. 如果设置为true,则如果行太长而无法放入分配的宽度内,则会对其进行换行。 If set to false, the lines will always be unwrapped. 如果设置为false,则将始终打开行。 A PropertyChange event ("lineWrap") is fired when the policy is changed. 更改策略时会触发PropertyChange事件(“lineWrap”)。 By default this property is false. 默认情况下,此属性为false。

Also see this method JTextArea#setWrapStyleWord(true) 另请参见此方法JTextArea#setWrapStyleWord(true)

Example a SSCCE: SSCCE示例:

   public class JTextAreaTest {

    public static void main(String args[]){

        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        frame.add(panel);
        JTextArea textArea = new JTextArea(11,30);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setText("Hi how are you? Im testing this textarea and im seeing if it's wrapping words");
        textArea.append("SASASASASDASDASDASDASDASDADSASDASDASDASDAS");
        JScrollPane scrollPane = new JScrollPane(textArea);
        panel.add(scrollPane);

        frame.pack();
        frame.setVisible(true);
    }

}

textArea示例

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

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