【问题标题】:NetBeans RCP - Centering popup window fails to work as expectedNetBeans RCP - 居中弹出窗口无法按预期工作
【发布时间】:2011-11-22 21:44:18
【问题描述】:
 errorPopup= popFactory.getPopup(this, errorBox, 
                    (verifierTopComponent.super.getX()+verifierTopComponent.super.getWidth()/2),
                    (verifierTopComponent.super.getY()+verifierTopComponent.super.getHeight()/2));

上面的代码可以正常工作,并且正确地将弹出窗口居中...但前提是窗口是全屏的,在我的主监视器上。

如何使它更健壮?我想把它放在当前 RCP 实例的中间。

(verifierTopComponent 是我在模块中错误命名的 TopComponent)。

在下面的评论之后,我想知道你们是否通常使用完全不同的方法来创建弹出窗口?我只是想把一些东西放在用户的脸上,让他们知道为什么事情不会像他们那样工作。

【问题讨论】:

  • 对不起。 PopupFactory 是 popFactory 使用的类。 errorBox 是我存储所有内容的 JPanel。

标签: java swing netbeans popup netbeans-platform


【解决方案1】:

在使用 NetBeans RCP 时,您应该使用 DialogDisplayerDialogDescriptor

类似这样的:

DialogDescriptor dd = new DialogDescriptor(errorBox, "Error message");
Object result = DialogDisplayer.getDefault().notify(dd);

它会自动计算正确的位置。

【讨论】:

  • 谢谢。我会调查的。
【解决方案2】:

我不确定如何解决您的具体问题,但根据我的经验,您可以/应该使用 NetBeans 的 org.openide.NotifyDescriptor 类向用户显示通知。您需要将 Dialog API 的依赖项添加到您的模块才能使用以下内容。

    NotifyDescriptor nd = new NotifyDescriptor(
            "This is the message that will go in the main body of the message. This could also be a custom JPanel",
            "Title of Dialog",
            NotifyDescriptor.DEFAULT_OPTION,
            NotifyDescriptor.ERROR_MESSAGE, 
            null, // this could be an array of JButtons that will replace the dialog's built-in buttons
            NotifyDescriptor.OK_OPTION);
    Object returnedValue = DialogDisplayer.getDefault().notify(nd);
    if (returnedValue == NotifyDescriptor.OK_OPTION) {
        // user pressed OK button
    }

与往常一样,请参阅javadoc for NotifyDescriptor 了解更多信息

编辑如另一个答案中所述,您可以使用 DialogDescriptor 类,它扩展了 NotifyDescriptor 类并添加了将对话框设置为模式的能力以及其他一些有用的功能。

还有一些其他有用的类扩展了 NotifyDescriptor 类,这些类可能对其他情况有用。有关子类的列表,请参阅 NotifyDescriptor 的 javadoc。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    • 2021-03-18
    相关资源
    最近更新 更多