JTextArea的每一行的长度限制为10
限制为5行,如何实现??(烦了几天了)
如: 1 2 3 4 5 6 7 8 9 10
1 a a a a a a a a a a
2 a a a a a
3 s s s s s s s s s s
4 c c c c c c c c
5 z z z z z z z z z z
...全文
1755打赏收藏
JTextArea的行列限制问题!!
JTextArea的每一行的长度限制为10 限制为5行,如何实现??(烦了几天了) 如: 1 2 3 4 5 6 7 8 9 10 1 a a a a a a a a a a 2 a a a a a 3 s s s s s s s s s s 4 c c c c c c c c 5 z z z z z z z z z z
public class TestFrame_1 extends JFrame{
/** Creates a new instance of TestFrame */
public TestFrame_1() {
Container c = getContentPane();
JTextArea text = new JTextArea(5, 10);
text.setLineWrap(true);
text.setUI(new MyTextUI());
c.add(text, "Center");
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
});
}
class MyTextUI extends javax.swing.plaf.basic.BasicTextAreaUI
{
public View create(Element elem)
{
JTextComponent c = getComponent();
if (c instanceof JTextArea) {
JTextArea area = (JTextArea) c;
View v;
if (area.getLineWrap()) {
v = new MyWrappedPlainView(elem, area.getWrapStyleWord());
} else {
v = new PlainView(elem);
}
return v;
}
return null;
}
}
class MyWrappedPlainView extends WrappedPlainView
{
public MyWrappedPlainView(Element elem, boolean b)
{
super(elem, b);
}
protected int calculateBreakPosition(int p0, int p1)
{
int p = p0 + Math.min(10, p1-p0);
return p;
}
}
public static void main(String[] args)
{
JFrame f = new TestFrame_1();
f.setSize(640, 480);
f.show();
}