开发者

How do I wrap long lines of text in a Java TextBox?

开发者 https://www.devze.com 2022-12-11 06:13 出处:网络
I want to load a text box in Java from a text file. This sounds simple but the big question is how to return at the end add newlines when text get close to the edge of the box, for example.

I want to load a text box in Java from a text file. This sounds simple but the big question is how to return at the end add newlines when text get close to the edge of the box, for example.

                              |
                              |
                              |
Java java java Java java java |Java java java Java java java
                     开发者_Go百科         |
                              |

Lets pretend the line represents the edge of the text box. It spills over.

How do I do this?

Java java java Java java java|
Java java java Java java java|
Java java java Java java java|

Currently I am using txtWords.setTxt(list); I don't think this is correct. Does anyone have a solution? Appritiate it!


Actually you will want to use the setLineWrap property as follows:

JTextArea t = new JTextArea();
t.setText(yourText);
t.setLineWrap(true);


Take a look at this example (taken from here)

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;

public class MainClass extends JFrame {

  static String sometext = "Text Text Text Text Text Text Text Text Text Text Text Text ";

  public MainClass() {
    super("Simple SplitPane Frame");
    setSize(450, 200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JTextArea jt1 = new JTextArea(sometext);
    JTextArea jt2 = new JTextArea(sometext);

    jt1.setLineWrap(true);
    jt2.setLineWrap(true);
    jt1.setMinimumSize(new Dimension(150, 150));
    jt2.setMinimumSize(new Dimension(150, 150));
    jt1.setPreferredSize(new Dimension(250, 200));
    JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jt1, jt2);
    getContentPane().add(sp, BorderLayout.CENTER);
  }

  public static void main(String args[]) {
    MainClass ssb = new MainClass();
    ssb.setVisible(true);
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消