swt让图片随着窗体大小变化而改变的问题

chenwanli11111 2012-07-03 11:38:39
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
}
}
...全文
170 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

58,452

社区成员

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

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