Cod*_*r89 6 java swing joptionpane
我正在开发一个项目,我想以编程方式关闭一个通用的JOptionPane(通过不点击任何按钮).当计时器到期时,我想关闭任何可能打开的可能的JOptionPane并将用户踢回我的程序的登录屏幕.我可以很好地踢回用户,但除非我实际点击它上面的按钮,否则JOptionPane仍然存在.
我看过许多没有运气的网站.似乎不可能在JOptionPane的"Red X"上调用doClick()方法,并且使用JOptionpane.getRootFrame().dispose()不起作用.
kle*_*tra 15
从技术上讲,您可以循环遍历应用程序的所有窗口,检查它们是否为JDialog类型并且具有JOptionPane类型的子项,并且如果是这样处置对话框:
Action showOptionPane = new AbstractAction("show me pane!") {
@Override
public void actionPerformed(ActionEvent e) {
createCloseTimer(3).start();
JOptionPane.showMessageDialog((Component) e.getSource(), "nothing to do!");
}
private Timer createCloseTimer(int seconds) {
ActionListener close = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Window[] windows = Window.getWindows();
for (Window window : windows) {
if (window instanceof JDialog) {
JDialog dialog = (JDialog) window;
if (dialog.getContentPane().getComponentCount() == 1
&& dialog.getContentPane().getComponent(0) instanceof JOptionPane){
dialog.dispose();
}
}
}
}
};
Timer t = new Timer(seconds * 1000, close);
t.setRepeats(false);
return t;
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9015 次 |
| 最近记录: |