swt让图片随着窗体大小变化而改变的问题
package org.betguide.autobet.view;
import java.awt.Toolkit;
import org.betguide.autobet.component.SWTResourceManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
public class Login1 extends Shell {
/**
* Launch the application.
*
* @param args
*/
public static void main(String args[]) {
try {
Display display = Display.getDefault();
Login1 shell = new Login1(display);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the shell.
*
* @param display
*/
public Login1(Display display) {
super(display, SWT.SHELL_TRIM);
setLayout(new FormLayout());
final Label lbl_loginHead = new Label(this, SWT.NONE);
final Image loginHead = new Image(display,
System.getProperty("user.dir") + "/image/loginHead.jpg");
FormData fd_lbl_loginHead = new FormData();
fd_lbl_loginHead.right = new FormAttachment(100, 0);
fd_lbl_loginHead.top = new FormAttachment(0);
fd_lbl_loginHead.left = new FormAttachment(0);
fd_lbl_loginHead.bottom = new FormAttachment(35, 0);
lbl_loginHead.setLayoutData(fd_lbl_loginHead);
// lbl_loginHead.setImage(loginHead);
lbl_loginHead.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent arg0) {
Rectangle bounds = loginHead.getBounds();
Point size = lbl_loginHead.getSize();
int MAX_WIDTH = size.x;
int MAX_HEIGHT = size.y;
int imageWidth = bounds.width;
int imageHeight = bounds.height;
// GC gc = new GC(lbl_loginHead);//使用这个gc将窗口变大没有问题,缩小的时候图片要 过一段时间才会变小
// gc.setAdvanced(true);
//使用这个arg0的gc则会有锯齿
arg0.gc.drawImage(loginHead, 0, 0, bounds.width, bounds.height, 0,
0, MAX_WIDTH, MAX_HEIGHT);
}
});
lbl_loginHead.redraw();
createContents();
}
/**
* Create contents of the shell.
*/
protected void createContents() {
setText("SWT Application");
setSize(450, 300);
}
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
}