用SWT实现一个输入text,需要做到和eclipse一样,左边有一个行数Linenumber显示

guoxiaopeng1982 2008-03-18 02:20:01
本人代码如下,不知道各位大大还有没有更加好的写法
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class Tswt {

private StyledText styledText;
private List list;
private Text text;
private Composite container;
private Integer i = 1;//输入框行计数器

protected Shell shell;

/**
* Launch the application
* @param args
*/
public static void main(String[] args) {
try {
Tswt window = new Tswt();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Open the window
*/
public void open() {
final Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}

/**
* Create contents of the window
*/
protected void createContents() {
shell = new Shell();
shell.setSize(500, 375);
shell.setText("SWT Application");

final Composite composite = new Composite(shell, SWT.NONE);
composite.setBounds(2, 0, 490, 321);

final SashForm sashForm = new SashForm(composite, SWT.VERTICAL);

sashForm.setBounds(0, 0, 490, 321);

final Composite topLayout = new Composite(sashForm, SWT.NONE);

list = new List(topLayout, SWT.BORDER);
list.setBounds(0, 0, 24, 159);
list.add(i.toString());

styledText = new StyledText(topLayout, SWT.BORDER);
styledText.addKeyListener(new KeyAdapter() {
public void keyReleased(final KeyEvent e) {
Integer linenumber = styledText.getLineCount();
if(i<linenumber){
i = i+1;
list.add(linenumber.toString());
}
if(i>linenumber){
list.remove(i.toString());
i = i-1;
}
}
});
styledText.setBounds(24, 0, 466, 159);

container = new Composite(sashForm, SWT.NONE);
container.setLayoutData(new GridData(GridData.FILL_BOTH));
final GridLayout gridLayout = new GridLayout();
container.setLayout(gridLayout);

text = new Text(container, SWT.MULTI | SWT.BORDER);
final GridData gd_text = new GridData(SWT.FILL, SWT.FILL, false, false);
gd_text.heightHint = 121;
gd_text.widthHint = 484;
text.setLayoutData(gd_text);
sashForm.setWeights(new int[] {124, 124 });
//
}
}
另外,如果我需要做到更加行号Linenumber来取得该行的输入字符串该怎么做呢??请各位大大,发表一下看法,小弟在线等候………………
...全文
555 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Inhibitory 2008-03-18
  • 打赏
  • 举报
回复
那就在文本编辑器的旁边加个Panel, 文本编辑器中也进行回车的监听, 当有回车后, 在显示行数的Panel上显示行数, 显示数字的位置就需要计算一下了. 对于文本编辑器中的字体大小可以设置为不同的大小时, 处理比较麻烦, 不知道可不可以从文本编辑器中得到光标的屏幕坐标, 如果能得到, 直接传给Panel, Panel再把屏幕坐标转换为Panel上的坐标.
chiny 2008-03-18
  • 打赏
  • 举报
回复
为什么不看看JDT 的编辑器源代码呢?
guoxiaopeng1982 2008-03-18
  • 打赏
  • 举报
回复
楼上你的方法是对的…………不过,貌似这样做,从document里面取出的数据就会包含行号了…………我想做成的是eclipse的效果
Inhibitory 2008-03-18
  • 打赏
  • 举报
回复
对输入区加个键盘事件监听, 当有回车时, 在document中加个行的数字就可以了.
guoxiaopeng1982 2008-03-18
  • 打赏
  • 举报
回复
把代码重新发一下,大家好看
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class Tswt {

private StyledText styledText;
private List list;
private Text text;
private Composite container;
private Integer i = 1;//输入框行计数器

protected Shell shell;

/**
* Launch the application
* @param args
*/
public static void main(String[] args) {
try {
Tswt window = new Tswt();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Open the window
*/
public void open() {
final Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}

/**
* Create contents of the window
*/
protected void createContents() {
shell = new Shell();
shell.setSize(500, 375);
shell.setText("SWT Application");

final Composite composite = new Composite(shell, SWT.NONE);
composite.setBounds(2, 0, 490, 321);

final SashForm sashForm = new SashForm(composite, SWT.VERTICAL);

sashForm.setBounds(0, 0, 490, 321);

final Composite topLayout = new Composite(sashForm, SWT.NONE);

list = new List(topLayout, SWT.BORDER);
list.setBounds(0, 0, 24, 159);
list.add(i.toString());

styledText = new StyledText(topLayout, SWT.BORDER);
styledText.addKeyListener(new KeyAdapter() {
public void keyReleased(final KeyEvent e) {
Integer linenumber = styledText.getLineCount();
if(i <linenumber){
i = i+1;
list.add(linenumber.toString());
}
if(i> linenumber){
list.remove(i.toString());
i = i-1;
}
}
});
styledText.setBounds(24, 0, 466, 159);

container = new Composite(sashForm, SWT.NONE);
container.setLayoutData(new GridData(GridData.FILL_BOTH));
final GridLayout gridLayout = new GridLayout();
container.setLayout(gridLayout);

text = new Text(container, SWT.MULTI ¦ SWT.BORDER);
final GridData gd_text = new GridData(SWT.FILL, SWT.FILL, false, false);
gd_text.heightHint = 121;
gd_text.widthHint = 484;
text.setLayoutData(gd_text);
sashForm.setWeights(new int[] {124, 124 });
//
}
}
guoxiaopeng1982 2008-03-18
  • 打赏
  • 举报
回复
我只想到一个笨办法,用styledText.getText(int start, int end) 这个东西来手动取得某行数据,唉

62,614

社区成员

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

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