gpt4 book ai didi

java - Eclipse RCP 获取部件实例

转载 作者:行者123 更新时间:2023-11-30 06:06:20 25 4
gpt4 key购买 nike

我正在尝试获取与 Java 类连接的部分的引用。我可以使用

`@PostConstruct
public void createComposite(Composite parent) {

}`

然后“父”变量就是我需要的。但我想有另一种方法。我正在尝试添加静态变量来保存它:

public class BibliotekaZmianyPart {
private static Label label;
private static Button button;
private static Composite part;

@PostConstruct
public void createComposite(Composite parent) {
part = parent;
}

public static void editBook() {
GridLayout layout = new GridLayout(2, false);
part.setLayout(layout);
label = new Label(part, SWT.NONE);
label.setText("A label");
button = new Button(part, SWT.PUSH);
button.setText("Press Me");
}}

然后“部分”应该是我需要的变量 - 但它不起作用。

最佳答案

不能有像引用实例变量那样的静态方法。

如果您想从另一个部件引用现有部件,请使用EPartService来查找该部件:

@Inject
EPartService partService;


MPart mpart = partService.findPart("part id");

BibliotekaZmianyPart part = (BibliotekaZmianyPart)mpart.getObject();

part.editBook(); // Using non-static 'editBook'

如果零件尚未打开,您可以使用零件服务 showPart 方法:

MPart mpart = partService.showPart("part id", PartState.ACTIVATE);

BibliotekaZmianyPart part = (BibliotekaZmianyPart)mpart.getObject();

part.editBook();

所以你的类(class)将是:

public class BibliotekaZmianyPart {
private Label label;
private Button button;
private Composite part;

@PostConstruct
public void createComposite(Composite parent) {
part = parent;
}

public void editBook() {
GridLayout layout = new GridLayout(2, false);
part.setLayout(layout);
label = new Label(part, SWT.NONE);
label.setText("A label");
button = new Button(part, SWT.PUSH);
button.setText("Press Me");

// You probably need to call layout on the part
part.layout();
}}

关于java - Eclipse RCP 获取部件实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51233649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com