YES_NO_OPTION JOptionPane 在 'yes' 上继续 java 代码
YES_NO_OPTION JOptionPane continue java code on 'yes'
基本上,我有一款游戏可以在发生某些事情时调出 JOptionPane,我希望能够在用户单击“是”时 return 进入游戏。有点像取消暂停功能
注意:当我发布这个答案时,问题完全不同 - 请阅读评论。
由于OpenJDK是开源的(GNU General Public License version 2),你可以看看它的源代码。我通常在 grepcode.com 上浏览 Java 源代码。如果您安装 OracleJDK 和 select 选项来安装源代码,您还可以在 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
从 UIManager
请求 OptionPaneUI
。 OptionPaneUI
是一个抽象的 class (看起来更像一个接口),所以你在那里找不到任何代码。它唯一的子class是BasicOptionPaneUI
或MultiOptionPaneUI
。使用调试器,我发现它是 BasicOptionPaneUI
for a showConfirmDialog
。之后,将结果传递给继承自 JComponent
的 setUI
方法。除了一些基本的字段检查,它调用 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. This method is too long to copy here, but, in short, it gets the locale-specific strings for the buttons and adds them as JButtons. It then gives each of them a ButtonActionListener.
基本上,我有一款游戏可以在发生某些事情时调出 JOptionPane,我希望能够在用户单击“是”时 return 进入游戏。有点像取消暂停功能
注意:当我发布这个答案时,问题完全不同 - 请阅读评论。
由于OpenJDK是开源的(GNU General Public License version 2),你可以看看它的源代码。我通常在 grepcode.com 上浏览 Java 源代码。如果您安装 OracleJDK 和 select 选项来安装源代码,您还可以在 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
从 UIManager
请求 OptionPaneUI
。 OptionPaneUI
是一个抽象的 class (看起来更像一个接口),所以你在那里找不到任何代码。它唯一的子class是BasicOptionPaneUI
或MultiOptionPaneUI
。使用调试器,我发现它是 BasicOptionPaneUI
for a showConfirmDialog
。之后,将结果传递给继承自 JComponent
的 setUI
方法。除了一些基本的字段检查,它调用 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. This method is too long to copy here, but, in short, it gets the locale-specific strings for the buttons and adds them as JButtons. It then gives each of them a ButtonActionListener.