ogs API 和 Utilities API。
NetBeans:您的第一个插件(5)
时间:2011-07-20 Silveira
现在我们第一次整理代码。
在 HelloAction.java 文件中, HelloAction 类扩展了 CallableSystemAction(它是一个抽象类)。在 performAction() 方法中只 有:
//TODO implement action body
我们将重新编写 performAction 方法:
public void performAction() {
String msg = "Hello NetBeans World!";
int msgType = NotifyDescriptor.INFORMATION_MESSAGE;
NotifyDescriptor d = new NotifyDescriptor.Message(msg, msgType);
DialogDisplayer.getDefault().notify(d);
}
您将看到一些警告(不能找到符号)。修复所导入的内容(Ctrl+Shift+I)。要测试您的模块,请再次执行以下操作: HelloYou → Install/Reload in Target Platform。NetBeans 的新 实例打开后,您将再次看到笑脸按钮。当您单击笑脸按钮时,NetBeans 将显示一段对话。

图 17. Hello NetBeans World。
让我们再做一些其他工作。我们使用 NotifyDescriptor.InputLine 与用户交谈。我们需要创建一个输入对话行,获取用户名称(如果单击 Ok 按钮),然后发送一条消息给他。
public void performAction() {
NotifyDescriptor.InputLine question;
question = new NotifyDescriptor.InputLine("Name:",
"What''s your name?",
NotifyDescriptor.OK_CANCEL_OPTION,
NotifyDescriptor.QUESTION_MESSAGE);
if (DialogDisplayer.getDefault().notify(question) == NotifyDescriptor.OK_OPTION) {
String msg = "Hello "+question.getInputText()+"!";
int msgType = NotifyDescriptor.INFORMATION_MESSAGE;
NotifyDescriptor d = new NotifyDescriptor.Message(msg, msgType);
DialogDisplayer.getDefault().notify(d);
}
}
再次执行 HelloYou → Install/Reload in Target Platform 操作。
在 NetBeans 实例打开之后,单击我们设计的微笑按钮。


我们已经完成了 NetBeans 插件的创建! |