java swt TeeItem 的setimage无法让图标出现在标题前面,图标不出现,请问怎么样ti添加前面的图标啊?
importorg.eclipse.swt.SWT;importorg.eclipse.swt.events.SelectionAdapter;importorg.ecl...
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.wb.swt.ResourceManager;
public class TreeExample {
public static void main(String[] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.setText("Tree Example");
shell.setBounds(100, 100, 200, 200);
shell.setLayout(new FillLayout());
final Tree tree = new Tree (shell, SWT.SINGLE);
for (int i = 1; i < 4; i++) {
TreeItem grandParent = new TreeItem (tree, 0);
grandParent.setText ("Grand Parent - " + i);
grandParent.setImage(ResourceManager.getPluginImage("MyProjectName", "icons/1.jpg"));//这里是添加标题前面的图标的代码
for (int j = 1; j < 4; j++) {
TreeItem parent = new TreeItem (grandParent, 0);
parent.setText ("Parent - " + j);
for (int k = 1; k < 4; k++) {
TreeItem child = new TreeItem (parent, 0);
child.setText ("Child - " + k);
}
}
}
tree.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
TreeItem[] selected = tree.getSelection();
if (selected.length > 0)
System.out.println("Selected: " + selected[0].getText());
}
});
shell.open();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
代码没有问题,只是不知道怎么在节点前面添加图标,因为像我代码那样添加图标无法显示,我也不知道为什么,请高手详细指点,本人是初学者。 展开
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.wb.swt.ResourceManager;
public class TreeExample {
public static void main(String[] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.setText("Tree Example");
shell.setBounds(100, 100, 200, 200);
shell.setLayout(new FillLayout());
final Tree tree = new Tree (shell, SWT.SINGLE);
for (int i = 1; i < 4; i++) {
TreeItem grandParent = new TreeItem (tree, 0);
grandParent.setText ("Grand Parent - " + i);
grandParent.setImage(ResourceManager.getPluginImage("MyProjectName", "icons/1.jpg"));//这里是添加标题前面的图标的代码
for (int j = 1; j < 4; j++) {
TreeItem parent = new TreeItem (grandParent, 0);
parent.setText ("Parent - " + j);
for (int k = 1; k < 4; k++) {
TreeItem child = new TreeItem (parent, 0);
child.setText ("Child - " + k);
}
}
}
tree.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
TreeItem[] selected = tree.getSelection();
if (selected.length > 0)
System.out.println("Selected: " + selected[0].getText());
}
});
shell.open();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
代码没有问题,只是不知道怎么在节点前面添加图标,因为像我代码那样添加图标无法显示,我也不知道为什么,请高手详细指点,本人是初学者。 展开
2个回答
展开全部
楼主我将你的程序改拉下,就可以拉。代码如下:
import java.io.InputStream;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
public class TreeExample {
public static void main(String[] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.setText("Tree Example");
shell.setBounds(100, 100, 200, 200);
shell.setLayout(new FillLayout());
final Tree tree = new Tree (shell, SWT.SINGLE);
for (int i = 1; i < 4; i++) {
TreeItem grandParent = new TreeItem (tree, 0);
grandParent.setText ("Grand Parent - " + i);
InputStream iStream=TreeExample.class.getClassLoader().getResourceAsStream("imgs/1.gif");
Image image=new Image(display,iStream);
grandParent.setImage(image);//这里是添加标题前面的图标的代码
for (int j = 1; j < 4; j++) {
TreeItem parent = new TreeItem (grandParent, 0);
parent.setText ("Parent - " + j);
for (int k = 1; k < 4; k++) {
TreeItem child = new TreeItem (parent, 0);
child.setText ("Child - " + k);
}
}
}
tree.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
TreeItem[] selected = tree.getSelection();
if (selected.length > 0)
System.out.println("Selected: " + selected[0].getText());
}
});
shell.open();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
import java.io.InputStream;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
public class TreeExample {
public static void main(String[] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.setText("Tree Example");
shell.setBounds(100, 100, 200, 200);
shell.setLayout(new FillLayout());
final Tree tree = new Tree (shell, SWT.SINGLE);
for (int i = 1; i < 4; i++) {
TreeItem grandParent = new TreeItem (tree, 0);
grandParent.setText ("Grand Parent - " + i);
InputStream iStream=TreeExample.class.getClassLoader().getResourceAsStream("imgs/1.gif");
Image image=new Image(display,iStream);
grandParent.setImage(image);//这里是添加标题前面的图标的代码
for (int j = 1; j < 4; j++) {
TreeItem parent = new TreeItem (grandParent, 0);
parent.setText ("Parent - " + j);
for (int k = 1; k < 4; k++) {
TreeItem child = new TreeItem (parent, 0);
child.setText ("Child - " + k);
}
}
}
tree.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
TreeItem[] selected = tree.getSelection();
if (selected.length > 0)
System.out.println("Selected: " + selected[0].getText());
}
});
shell.open();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询