按下按钮时使用 ZEST 框架绘制图形不起作用?
我正在尝试使用 JAVA 中的 ZEST 框架绘制图表。代码的预期工作如下:
1)将Shell设置为FormLayout。
2) 使用 FormData 自定义添加了标签、文本框和按钮。
3) 在按钮右侧添加了一个复合(有边框的)。
4) 为按钮添加监听器
5) 当按下按钮时,应在按钮右侧的组合上创建一个具有三个节点“石头”、“布”和剪刀的图形。 (仅限于复合)。
但是图表没有显示。
请帮助我找出错误。 提前致谢。
import java.io.FileNotFoundException;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.zest.core.widgets.Graph;
import org.eclipse.zest.core.widgets.GraphConnection;
import org.eclipse.zest.core.widgets.GraphNode;
import org.eclipse.zest.layouts.LayoutStyles;
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
public class Demo{
public static void main(String[] args) throws FileNotFoundException, IOException {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Demo Map");
FormLayout formLayout= new FormLayout();
shell.setLayout(formLayout);
//Label
Label label = new Label(shell, SWT.NONE);
label.setText("TEXT:");
FontData[] fontData = label.getFont().getFontData();
for(int i = 0; i < fontData.length; ++i)
fontData[i].setHeight(12);
final Font newFont = new Font(display, fontData);
label.setFont(newFont);
FormData formData= new FormData();
formData.top= new FormAttachment(0, 6);
formData.left= new FormAttachment(0,5);
label.setLayoutData(formData);
//Listener for font object(we created it, so we dispose it)
label.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
newFont.dispose();
}
});
//Text BOX
final Text textBox= new Text(shell, SWT.NONE);
FormData textData = new FormData();
textData.top= new FormAttachment(0, 8);
textData.left= new FormAttachment(label, 10);
textData.height= 20;
textData.width= 200;
textBox.setFont(newFont);
textBox.setLayoutData(textData);
//Button
Button button = new Button(shell, SWT.PUSH);
button.setText("Enter");
FormData buttonData= new FormData();
buttonData.top= new FormAttachment(0,5);
buttonData.left= new FormAttachment(textBox, 10);
buttonData.height= 25;
buttonData.width=50;
button.setLayoutData(buttonData);
//Composite to hold the graph visual
final Composite composite = new Composite(shell, SWT.BORDER);
FormData compositeFormData= new FormData();
compositeFormData.top = new FormAttachment(0,5);
compositeFormData.left = new FormAttachment(button,15);
compositeFormData.right= new FormAttachment(100,-10);
compositeFormData.bottom= new FormAttachment(100,-10);
composite.setLayoutData(compositeFormData);
//drawNodes on button press
button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
switch (e.type) {
case SWT.Selection:
System.out.println("Button pressed");
Graph g = new Graph(composite, SWT.NONE);
GraphNode n = new GraphNode(g, SWT.NONE, "Paper");
GraphNode n2 = new GraphNode(g, SWT.NONE, "Rock");
GraphNode n3 = new GraphNode(g, SWT.NONE, "Scissors");
new GraphConnection(g, SWT.NONE, n, n2);
new GraphConnection(g, SWT.NONE, n2, n3);
new GraphConnection(g, SWT.NONE, n3, n);
g.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
break;
}
}
});
shell.open();
while(!display.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
}
I am trying to draw a graph using the ZEST framwework in JAVA. The intended work of code is as follows:
1) Shell is set to FormLayout.
2) Added a label, textbox, and a button using FormData customization.
3) Added a composite on the right of button (the one with the border).
4) Added a listener to the button
5) When the button is pressed, a graph with three nodes, "Rock", "Paper" and Scissors should be created on the composite to the right of the button. (confined to the composite only).
But the graph is not getting displayed.
Please assist me in finding out the bug.
Thanks in advance.
import java.io.FileNotFoundException;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.zest.core.widgets.Graph;
import org.eclipse.zest.core.widgets.GraphConnection;
import org.eclipse.zest.core.widgets.GraphNode;
import org.eclipse.zest.layouts.LayoutStyles;
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
public class Demo{
public static void main(String[] args) throws FileNotFoundException, IOException {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Demo Map");
FormLayout formLayout= new FormLayout();
shell.setLayout(formLayout);
//Label
Label label = new Label(shell, SWT.NONE);
label.setText("TEXT:");
FontData[] fontData = label.getFont().getFontData();
for(int i = 0; i < fontData.length; ++i)
fontData[i].setHeight(12);
final Font newFont = new Font(display, fontData);
label.setFont(newFont);
FormData formData= new FormData();
formData.top= new FormAttachment(0, 6);
formData.left= new FormAttachment(0,5);
label.setLayoutData(formData);
//Listener for font object(we created it, so we dispose it)
label.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
newFont.dispose();
}
});
//Text BOX
final Text textBox= new Text(shell, SWT.NONE);
FormData textData = new FormData();
textData.top= new FormAttachment(0, 8);
textData.left= new FormAttachment(label, 10);
textData.height= 20;
textData.width= 200;
textBox.setFont(newFont);
textBox.setLayoutData(textData);
//Button
Button button = new Button(shell, SWT.PUSH);
button.setText("Enter");
FormData buttonData= new FormData();
buttonData.top= new FormAttachment(0,5);
buttonData.left= new FormAttachment(textBox, 10);
buttonData.height= 25;
buttonData.width=50;
button.setLayoutData(buttonData);
//Composite to hold the graph visual
final Composite composite = new Composite(shell, SWT.BORDER);
FormData compositeFormData= new FormData();
compositeFormData.top = new FormAttachment(0,5);
compositeFormData.left = new FormAttachment(button,15);
compositeFormData.right= new FormAttachment(100,-10);
compositeFormData.bottom= new FormAttachment(100,-10);
composite.setLayoutData(compositeFormData);
//drawNodes on button press
button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
switch (e.type) {
case SWT.Selection:
System.out.println("Button pressed");
Graph g = new Graph(composite, SWT.NONE);
GraphNode n = new GraphNode(g, SWT.NONE, "Paper");
GraphNode n2 = new GraphNode(g, SWT.NONE, "Rock");
GraphNode n3 = new GraphNode(g, SWT.NONE, "Scissors");
new GraphConnection(g, SWT.NONE, n, n2);
new GraphConnection(g, SWT.NONE, n2, n3);
new GraphConnection(g, SWT.NONE, n3, n);
g.setLayoutAlgorithm(new SpringLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
break;
}
}
});
shell.open();
while(!display.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题在于图形只能在 FillLayout 上绘制。正如您在示例中看到的,它们使用FillLayout,而不是FormLayout。
I think the problem is in the fact that Graphs can be drawn only on FillLayout. As you can see in example, they use FillLayout, not FormLayout.