62,614
社区成员
发帖
与我相关
我的任务
分享
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 });
//
}
}