YES_NO_OPTION JOptionPane继续'是'的java代码

时间:2017-03-05 21:28:13

标签: java joptionpane messagebox

基本上我有一个游戏会在发生某些事情时调出JOptionPane,我希望能够在用户点击是时返回游戏。有点像未使用功能

1 个答案:

答案 0 :(得分:1)

注意:当我发布这个答案时,问题就完全不同了 - 请阅读评论。

由于OpenJDK是开源的(GNU通用公共许可证版本2),您可以查看其源代码。我通常在grepcode.com上浏览Java源代码。如果您安装OracleJDK并选择安装源的选项,您还可以在JDK安装目录(src.zip)中找到大多数源代码。请注意,该许可证可能不允许您自己重复使用此代码(通常是相同的)(但它肯定比使用反编译器好很多)。

这些指向特定方法的链接无法在我最喜欢的浏览器中运行(Vivaldi,基于Chrome)。如果您不希望自己寻找具体方法,我建议您使用Firefox。

以下是grepcode.com的相关摘录:

JOptionPane (constructor) - 每个show...Dialog方法调用此方法:

1830  public JOptionPane(Object message, int messageType, int optionType,
1831                     Icon icon, Object[] options, Object initialValue) {
          ...
1838      setOptionType(optionType);
          ...
1841      updateUI();
1842  }

JOptionPane (updateUI)

1877  public void updateUI() {
1878      setUI((OptionPaneUI)UIManager.getUI(this));
1879  }

我们在此处看到JOptionPaneOptionPaneUI请求UIManagerOptionPaneUI是一个抽象类(看起来更像是一个接口),因此您无法在那里找到任何代码。它唯一的子类是BasicOptionPaneUIMultiOptionPaneUI。使用调试器,我发现BasicOptionPaneUIshowConfirmDialog。然后,结果将传递到从setUI继承的JComponent方法。除了一些基本的字段检查,它调用ui.installUI方法:

137   public void installUI(JComponent c) {
138       optionPane = (JOptionPane)c;
139       installDefaults();
140       optionPane.setLayout(createLayoutManager());
141       installComponents();
142       installListeners();
143       installKeyboardActions();
144   }

让我们看看下面的installComponents

171   protected void More ...installComponents() {
172       optionPane.add(createMessageArea());
173 
174       Container separator = createSeparator();
175       if (separator != null) {
176           optionPane.add(separator);
177       }
178       optionPane.add(createButtonArea());
179       optionPane.applyComponentOrientation(optionPane.getComponentOrientation());
180   }

createButtonArea听起来很有希望:

613   protected Container createButtonArea() {
614       JPanel bottom = new JPanel();
          ...
630       addButtonComponents(bottom, getButtons(), getInitialValueIndex());
631       return bottom;
632   }

此方法现在调用addButtonComponents。这个方法在这里复制太长了,但简而言之,它获取了按钮的特定于语言环境的字符串,并将它们添加为JButton s。然后它给每个人ButtonActionListener