基本上我有一个游戏会在发生某些事情时调出JOptionPane,我希望能够在用户点击是时返回游戏。有点像未使用功能
答案 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 }
1877 public void updateUI() {
1878 setUI((OptionPaneUI)UIManager.getUI(this));
1879 }
我们在此处看到JOptionPane
从OptionPaneUI
请求UIManager
。 OptionPaneUI
是一个抽象类(看起来更像是一个接口),因此您无法在那里找到任何代码。它唯一的子类是BasicOptionPaneUI
或MultiOptionPaneUI
。使用调试器,我发现BasicOptionPaneUI
为showConfirmDialog
。然后,结果将传递到从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。