swt绘图问题,教程上看来的代码在eclipse上不能用 10
packagedraw;importorg.eclipse.swt.events.PaintEvent;importorg.eclipse.swt.events.Pain...
package draw;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Draw {
public static void main(String[] arg){
Display display =new Display();
Shell shell = new Shell(display);
shell.addPaintListener(new PaintListener(){
public void paintControl(PaintEvent e){
Rectangle clientArea = shell.getClientArea();
e.gc.drawLine(0,0,clientArea.width,clientArea.height);
}
});
shell.setSize(150,150);
}
}
这个时候编译有错误,按照提示把shell改成final运行之后没有窗口弹出
好了,成功了 展开
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Draw {
public static void main(String[] arg){
Display display =new Display();
Shell shell = new Shell(display);
shell.addPaintListener(new PaintListener(){
public void paintControl(PaintEvent e){
Rectangle clientArea = shell.getClientArea();
e.gc.drawLine(0,0,clientArea.width,clientArea.height);
}
});
shell.setSize(150,150);
}
}
这个时候编译有错误,按照提示把shell改成final运行之后没有窗口弹出
好了,成功了 展开
展开全部
//这个在LINUX下做的(未在WIN下测试哦),交替画圆和方框
//: SWTDrawTest.java
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
public class SWTDrawTest {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("SWT 交替画圆和方框");
shell.setLayout(new FillLayout());
C c = new C(shell,SWT.NONE);
shell.setBounds(300,100,300,300);
shell.layout(true);
shell.open ();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
c.dispose();
}
}
//用画布类来画图:
class C extends Canvas implements PaintListener,Runnable{
private int delayInMillis = 1000;//更新时间为1秒
private boolean sw;
private Color bgColor,fgColor;
public C(Composite parent, int style) {
super(parent, style);
this.addPaintListener(this);
this.bgColor = getDisplay().getSystemColor(SWT.COLOR_BLACK);
this.fgColor = getDisplay().getSystemColor(SWT.COLOR_GREEN);
new Thread(this).start();
}
//这个方法就是画图的主要方法
public void paintControl(PaintEvent e) {
GC g = e.gc;
g.setBackground(bgColor);
g.setForeground(fgColor);
g.fillRectangle(this.getClientArea());
int w = this.getClientArea().width;
int h = this.getClientArea().height;
int x = w/4,y=h/4;
w/=2;h/=2;
if(sw)
g.drawOval(x,y,w,h);
else
g.drawRectangle(x,y,w,h);
g.dispose();
}
public void run(){
while(true){
if(isDisposed())return;
getDisplay().asyncExec(new BackgroundThread());
trycatch(Exception e){}
}
}
//后台线程
private class BackgroundThread implements Runnable{
public void run(){
sw = !sw;
redraw();
}
}
}
//: SWTDrawTest.java
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
public class SWTDrawTest {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("SWT 交替画圆和方框");
shell.setLayout(new FillLayout());
C c = new C(shell,SWT.NONE);
shell.setBounds(300,100,300,300);
shell.layout(true);
shell.open ();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
c.dispose();
}
}
//用画布类来画图:
class C extends Canvas implements PaintListener,Runnable{
private int delayInMillis = 1000;//更新时间为1秒
private boolean sw;
private Color bgColor,fgColor;
public C(Composite parent, int style) {
super(parent, style);
this.addPaintListener(this);
this.bgColor = getDisplay().getSystemColor(SWT.COLOR_BLACK);
this.fgColor = getDisplay().getSystemColor(SWT.COLOR_GREEN);
new Thread(this).start();
}
//这个方法就是画图的主要方法
public void paintControl(PaintEvent e) {
GC g = e.gc;
g.setBackground(bgColor);
g.setForeground(fgColor);
g.fillRectangle(this.getClientArea());
int w = this.getClientArea().width;
int h = this.getClientArea().height;
int x = w/4,y=h/4;
w/=2;h/=2;
if(sw)
g.drawOval(x,y,w,h);
else
g.drawRectangle(x,y,w,h);
g.dispose();
}
public void run(){
while(true){
if(isDisposed())return;
getDisplay().asyncExec(new BackgroundThread());
trycatch(Exception e){}
}
}
//后台线程
private class BackgroundThread implements Runnable{
public void run(){
sw = !sw;
redraw();
}
}
}
展开全部
提示的什么错误啊?我没有见过public static void main(String[] arg){}
把arg换做 args ,试试
把arg换做 args ,试试
追问
哦,少打了个s,但还是跟之前一样
提示的是Cannot refer to a non-final variable shell inside an inner class defined in a different method
追答
那是说你搞的shell对象不能被调用,把他弄成全局变量,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
内部类使用外部变量时是要求外部变量是final的,这样外部变量才不会被无故释放掉。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询