dispose 和 setVisible 后的默认按钮
给出以下代码:
public class DialogTest implements ActionListener {
public static void main(String[] args) {DialogTest g = new DialogTest();}
public DialogTest() {
JButton b1 = new JButton("Button A");
b1.addActionListener(this);
JDialog d = new JDialog();
d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
JPanel p = new JPanel();
p.add(b1);
d.add(p);
d.getRootPane().setDefaultButton(b1);
d.pack();
d.setVisible(true);
d.dispose();
d.pack();
d.setVisible(true);
}
public void actionPerformed(ActionEvent e) {System.out.println("hello");}
}
按 Enter 键不应该向控制台写入内容吗?根据文档(http:// /java.sun.com/javase/7/docs/api/java/awt/Window.html#dispose()):
Window 及其子组件可以 可以再次显示 重建本地资源 随后调用打包或展示。 的 重新创建的窗口及其状态 子组件将与 这些对象在该点的状态 窗口放置的位置
这是预期的行为吗?
given the following code:
public class DialogTest implements ActionListener {
public static void main(String[] args) {DialogTest g = new DialogTest();}
public DialogTest() {
JButton b1 = new JButton("Button A");
b1.addActionListener(this);
JDialog d = new JDialog();
d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
JPanel p = new JPanel();
p.add(b1);
d.add(p);
d.getRootPane().setDefaultButton(b1);
d.pack();
d.setVisible(true);
d.dispose();
d.pack();
d.setVisible(true);
}
public void actionPerformed(ActionEvent e) {System.out.println("hello");}
}
Shouldn't pressing the Enter key write something to the console? According to the docs (http://java.sun.com/javase/7/docs/api/java/awt/Window.html#dispose()):
The Window and its subcomponents can
be made displayable again by
rebuilding the native resources with a
subsequent call to pack or show. The
states of the recreated Window and its
subcomponents will be identical to the
states of these objects at the point
where the Window was disposed
Is this intended behaviour?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原因是在 < code>JButton.removeNotify (似乎是在
dispose
处调用的)DefaultButton
被重置:The reason is that in
JButton.removeNotify
(which seems to be called atdispose
) theDefaultButton
is reset: