Dee*_*pak 2 java swing netbeans netbeans-platform
我已经学会了使用 NotifyDescriptor 创建一个弹出对话框。我设计了一个带有两个大按钮的 JPanel,它们读取PURCHASE和CASHOUT并且我使用的代码在底部显示另外两个按钮,读取Yes和No。我不希望 NotifyDescriptor 在屏幕上放置自己的按钮。我只希望我的按钮在那里,当我的自定义按钮之一被单击时,弹出窗口将关闭并存储该值。(就像单击yes或no单击时关闭窗口一样)。我使用的代码如下
// 创建面板的实例,扩展 JPanel...
ChooseTransactionType popupSelector = new ChooseTransactionType();
// 创建自定义NotifyDescriptor,指定面板实例作为参数+其他参数
NotifyDescriptor nd = 新的 NotifyDescriptor(
popupSelector, // 面板实例
"Title", // 对话框的标题
NotifyDescriptor.YES_NO_OPTION, // 是/否对话框 ...
NotifyDescriptor.QUESTION_MESSAGE, // ... 问题类型 => 问号图标
null, // 我们已经指定了 YES_NO_OPTION => 可以为空,选项由 L&F 指定,
// 否则将选项指定为:
// new Object[] { NotifyDescriptor.YES_OPTION, ... 等等 },
NotifyDescriptor.YES_OPTION // 默认选项为“是”
);
// 让我们现在显示对话框...
如果 (DialogDisplayer.getDefault().notify(nd) == NotifyDescriptor.YES_OPTION) {
// 用户点击是,在这里做一些事情,例如:
System.out.println(popupSelector.getTRANSACTION_TYPE());
}
为了替换options按钮上的文本,您可以传入 aString[]作为options参数,或者,如果您需要更多控制,可以传入 a JButton[]。因此,在您的情况下,您需要从message面板中删除按钮并传入 aString[]作为options参数。
对于initialValue(最后一个参数),NotifyDescriptor.YES_OPTION您可以使用其中一个String[]值(Purchase 或 Cashout)来代替使用。该DialogDisplayer.notify()方法将返回选择的任何值。所以,在这种情况下,它会返回 aString但如果你传入 aJButton[]那么返回的值将是 a JButton。
String initialValue = "Purchase";
String cashOut = "Cashout";
String[] options = new String[]{initialValue, cashOut};
NotifyDescriptor nd = new NotifyDescriptor(
popupSelector,
"Title",
NotifyDescriptor.YES_NO_OPTION,
NotifyDescriptor.QUESTION_MESSAGE,
options,
initialValue
);
String selectedValue = (String) DialogDisplayer.getDefault().notify(nd);
if (selectedValue.equals(initialValue)) {
// handle Purchase
} else if (selectedValue.equals(cashOut)) {
// handle Cashout
} else {
// dialog closed with top close button
}
Run Code Online (Sandbox Code Playgroud)