【问题标题】:Why is my JOptionPane not appearing when I use a Scanner?当我使用扫描仪时,为什么我的 JOptionPane 没有出现?
【发布时间】:2015-05-22 21:57:32
【问题描述】:

我已经缩小了我的问题范围:如果我使用扫描仪,JOptionPane 将不会出现。例如,这很好用:

public static void main(String[] args) {
    System.out.println("Before the dialog.");
    JOptionPane.showMessageDialog(null, "Please.");
    System.out.println("After the dialog.");
}

即使这样也可以:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Before the dialog.");
    JOptionPane.showMessageDialog(null, "Please.");
    System.out.println("After the dialog.");
}

但是,这不起作用:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int hi = butler.nextInt();
    System.out.println("Before the dialog.");
    JOptionPane.showMessageDialog(null, "Please.");
    System.out.println("After the dialog.");
}

在最后一个示例中,程序等待输入,然后显示“在对话框之前”。之后,程序似乎冻结了。

我尝试在创建对话框之前关闭扫描仪,但没有效果。

【问题讨论】:

  • 对我来说很好。也许您的实际代码不同。见stackoverflow.com/help/mcve
  • 您使用的是 IDE 吗? JOptionPane 隐藏在 IDE 界面后面是不是因为 IDE 由于输入而有焦点?

标签: java java.util.scanner joptionpane


【解决方案1】:

它确实在Scanner 读取输入后出现。它只是没有显示为顶级窗口。请参阅JOptionPane won't show its dialog on top of other windows 了解如何解决此问题。

【讨论】:

    猜你喜欢
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    • 2017-03-13
    • 2012-12-01
    • 1970-01-01
    相关资源
    最近更新 更多