简体   繁体   English

JTextArea问题

[英]JTextArea issue

I'm having an issue with resizing of JTextArea in java.swing. 我在java.swing中调整JTextArea的大小时遇到​​问题。 The problem is that once the current line is finished (so for example if i keep pressing space) - it doesn't go to the second line - it just keeps on going. 问题是,一旦当前行结束(例如,如果我继续按空格键),它就不会转到第二行,它只会继续前进。 Same thing when i press enter - it stretches out the box vertically. 当我按下Enter时,它也是一样-它会垂直拉长框。 How do I prevent this? 我该如何预防? I'm using GridBagLayout. 我正在使用GridBagLayout。

JTextArea MainText = new JTextArea();
MainText.setFont(new Font("Arial", Font.PLAIN, 16));
MainText.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets (10, 10, 10, 10);
c.gridx = 0;
c.gridy = 2;
c.weightx = 1.0;
c.weighty = 1.0;
c.gridwidth = 3;
c.gridheight = 1;
panel.add(MainText, c);

To get your JTextAreas to wrap lines on words appropriately, use: mainText.setWrapStyleWord(true) and mainText.setLineWrap(true) 要使您的JTextAreas适当地在单词上换行,请使用: mainText.setWrapStyleWord(true)mainText.setLineWrap(true)

Most important though, get very familiar with using the Java API as it will likely answer 90% of similar questions: 不过,最重要的是,要熟悉使用Java API,因为它可能会回答90%的类似问题:

JTextArea API JTextArea API

Set the lineWrap and wrapStyleWord properties of the JTextArea 设置JTextArealineWrapwrapStyleWord属性

JTextArea MainText = new JTextArea();
MainText.setLineWrap(true);
MainText.setWrapStyleWord(true);

Take a look at How to use Text Areas for more details 看看如何使用文本区域了解更多详细信息

You might also find having a read through Code Conventions for the Java Programming Language of some use 您可能还会发现通读Java编程语言代码约定有一定用处

Unless you really don't want to, I would also suggest adding the JTextArea into a JScrollPane instead of adding it directly to the conatiner 除非您真的不想,否则我也建议您将JTextArea添加到JScrollPane而不是直接将其添加到conatiner中

panel.add(new JScrollPane(MainText), c);

This will prevent the JTextArea from wanting to grow as more text is added to it (unless that's what you're going for) 这将防止JTextArea希望随着添加更多文本而增长(除非您要这样做)

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

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