选择排序代码java 选择排序代码讲解( 二 )


fd bottom = new FormAttachment(btSort );
numGroup setLayoutData(fd);
// Label for input numbers
Label numLabel = new Label(numGroup SWT WRAP);
numLabel
setText( Please input the numbers you want to sort: (Note: Numbers need to be seperated by space) );
fd = new FormData();
fd top = new FormAttachment( );
fd left = new FormAttachment( );
fd right = new FormAttachment( );
numLabel setLayoutData(fd);
// Text for input numbers
numText = new Text(numGroup SWT BORDER | SWT MULTI | SWT V_SCROLL
| SWT WRAP);
numText setToolTipText( Numbers need to be seperated by space );
fd = new FormData();
fd top = new FormAttachment(numLabel );
fd left = new FormAttachment( );
fd right = new FormAttachment( );
fd bottom = new FormAttachment( );
numText setLayoutData(fd);
// The results group
Group resGroup = new Group(shell SWT NONE);
resGroup setText( The results: );
resGroup setLayout(layout);
fd = new FormData();
fd top = new FormAttachment(btSort );
fd left = new FormAttachment( );
fd right = new FormAttachment( );
fd bottom = new FormAttachment( );
resGroup setLayoutData(fd);
// Label for results
Label resLabel = new Label(resGroup SWT WRAP);
resLabel
setText( The results after sorted are: (Note: Results are seperated by space) );
fd = new FormData();
fd top = new FormAttachment( );
fd left = new FormAttachment( );
fd right = new FormAttachment( );
resLabel setLayoutData(fd);
// Text for results
resText = new Text(resGroup SWT BORDER | SWT MULTI | SWT V_SCROLL
| SWT WRAP);
resText setToolTipText( Results are seperated by space );
resText setEditable(false);
fd = new FormData();
fd top = new FormAttachment(resLabel );
fd left = new FormAttachment( );
fd right = new FormAttachment( );
fd bottom = new FormAttachment( );
resText setLayoutData(fd);
// Label for showing error message
errorLabel = new Label(shell SWT NONE);
fd = new FormData();
fd top = new FormAttachment( );
fd left = new FormAttachment( );
fd right = new FormAttachment( );
fd bottom = new FormAttachment( );
errorLabel setLayoutData(fd);
errorLabel setForeground(display getSystemColor(SWT COLOR_RED));
// Listen to the numText change
numText addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
numString = numText getText() trim();
hasError = false;
}
});
// If press Return focus go to Start Sort button and start sort
numText addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e keyCode == \r ) {
e doit = false;
btSort setFocus();
startSort();
}
}
});
// Listen to the button selection
btSort addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
startSort();
}
});
shell open();
while (!shell isDisposed()) {
if (!display readAndDispatch())
display sleep();
}
display dispose();
}
/**
* Get double values from string
*/
public void getDoubleFromString() {
int index = ;
// Split string using space
String[] splitedNumbers = numString split( );
if (numList size() != )
// Clear the arrayList for last used
numList clear();
for (int i = ; isplitedNumbers length; i++) {
if (splitedNumbers[i] trim() length() != ) {
try {
numList add(index++ Double valueOf(splitedNumbers[i]));
} catch (NumberFormatException e) {
setErrorMessage( Please input the correct numbers );

推荐阅读