JTextArea的行列限制问题!!

fast_time 2003-02-18 06:53:41
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
...全文
175 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xjffj 2003-02-18
  • 打赏
  • 举报
回复
为什么大家都不愿意去研究底层的东西呢?通过改写UI和View来做是最好的方式了,示例如下。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.plaf.basic.*;
import javax.swing.text.*;


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();
}

}
sunni 2003-02-18
  • 打赏
  • 举报
回复
恩呀 我就是这样做的 呵呵
希偌 2003-02-18
  • 打赏
  • 举报
回复
首先判断每行的字符,如果超过10个就插入换行符号
如果草果5行则将多余的字符删除
fast_time 2003-02-18
  • 打赏
  • 举报
回复
还有列呢???
baitianhai 2003-02-18
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/882/882722.xml?temp=.4957544

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧