Java 将图像添加到工具项
我要将图像设置为toolitem。然而,我的程序不工作,它只显示一个空窗口 到目前为止,我的代码是:Java 将图像添加到工具项,java,image,swt,toolbar,Java,Image,Swt,Toolbar,我要将图像设置为toolitem。然而,我的程序不工作,它只显示一个空窗口 到目前为止,我的代码是: import org.eclipse.swt.*; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.*; public class test { public static void main(String [] args) { Display display = ne
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;
public class test
{
public static void main(String [] args)
{
Display display = new Display( );
final Shell shell = new Shell(display);
shell.setSize(900, 200);
final ToolBar t=new ToolBar(shell,SWT.NONE);
Image image=new Image(display,"C:\\Users\\ART\\Documents\\Parsian\\bottum.jpg");
final ToolItem ti=new ToolItem(t,SWT.NONE);
ti.setImage(image);
ti.setText("test");
shell.open( );
while (!shell.isDisposed( ))
{
if (!display.readAndDispatch( ))
display.sleep( );
}
display.dispose( );
}
}
您的代码缺少的是神奇的功能: 看起来是这样的:
public static void main(String[] args)
{
Display display = new Display();
final Shell shell = new Shell(display);
final ToolBar bar = new ToolBar(shell, SWT.FLAT | SWT.WRAP | SWT.RIGHT);
final ToolItem item = new ToolItem(bar, SWT.PUSH);
Image image = new Image(display, "img/star.png");
item.setImage(image);
item.setText("test");
/* Add this pack() call */
bar.pack();
shell.setSize(400, 250);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}