Friday, August 1, 2014

swt canvas.redraw doesn't work for clicking button


the code is :

import org.eclipse.swt.SWT;

import org.eclipse.swt.events.PaintEvent;

import org.eclipse.swt.events.PaintListener;

import org.eclipse.swt.events.SelectionAdapter;

import org.eclipse.swt.events.SelectionEvent;

import org.eclipse.swt.layout.GridData;

import org.eclipse.swt.layout.GridLayout;

import org.eclipse.swt.widgets.Button;

import org.eclipse.swt.widgets.Canvas;

import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Shell;

public class caseButton {

private Shell shell;

public static void main(String[] args) {

try {

caseButton window = new caseButton();

window.open();

} catch (Exception e) {

e.printStackTrace();

}

}


/**

* Open the window.

*/

public void open() {

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.setText("SWT Application");

GridLayout gridLayout = new GridLayout();

gridLayout.numColumns = 1;

gridLayout.marginWidth = 0;

gridLayout.marginHeight = 0;

gridLayout.verticalSpacing = 4;

gridLayout.makeColumnsEqualWidth=false;

shell.setLayout(gridLayout);


GridData gridData=new GridData();

gridData.verticalSpan = 0;

gridData.horizontalSpan = 3;

gridData.verticalAlignment = GridData.FILL;

gridData.grabExcessVerticalSpace= true;

gridData.horizontalAlignment = GridData.FILL;

gridData.grabExcessHorizontalSpace = true;


final Button button = new Button(shell, SWT.PUSH);

button.setText("draw Rectangle");


final Canvas canvas = new Canvas(shell, SWT.NONE);

canvas.setLayoutData(gridData);


canvas.addPaintListener(new PaintListener() {

public void paintControl(PaintEvent e) {

e.gc.drawRoundRectangle(10, 10, 180, 80, 10, 10);

}

});

button.addSelectionListener(new SelectionAdapter() {

@Override

public void widgetSelected(SelectionEvent e) {

System.out.println("buttton_start ");

canvas.redraw(); // it can' work

System.out.println("buttton_end ");


}

});

}


}


the question is : when I click this button, it will print "buttton_start" and "buttton_end", the "canvas.redraw()" can't work. I don't know the reason , who can help me? thanks



No comments:

Post a Comment