简体   繁体   English

JAVA消息对话框彼此重叠出现

[英]JAVA Message dialogs apear on top of each other

I am writing a reminder program that pops up message dialogs. 我正在编写一个提醒程序,该程序会弹出消息对话框。 The problem is that all of the dialogs appear right on top of each other. 问题在于所有对话框都彼此重叠。 I would prefer to have them cascade. 我希望让它们级联。 Normally with other programs a dialog will apear then the next one will be slightly down and off to the side. 通常,在其他程序中会出现一个对话框,然后下一个对话框会稍微向下移到一边。 My code snippet is as follows: 我的代码段如下:

final JFrame frame = new JFrame( "A timer to be a reminder" );
frame.setVisible( false ); 
frame.setAlwaysOnTop(true);
int result = JOptionPane.showConfirmDialog( frame, msg, "Timer", JOptionPane.DEFAULT_OPTION);

Can anyone point out how to get the desired behavior? 谁能指出如何获得所需的行为?

This has been solved, I am attaching my code snippet so that someone else will be able to find it. 这已解决,我将附加我的代码段,以便其他人可以找到它。

JFrame frame = new JFrame( "A timer to be a reminder" );
frame.setLocationByPlatform( true );
frame.setVisible( true ); 
frame.setAlwaysOnTop(true);
int result = JOptionPane.showConfirmDialog( frame, msg, "Timer", JOptionPane.DEFAULT_OPTION);
frame.setVisible( false );
frame = null;

A JOptionPane is centered on the parent component or the center of the window if a null component is used. 如果使用null组件,则JOptionPane将位于父组件的中心或窗口的中心。

To control placement you will need to use a custom JDialog . 要控制放置,您将需要使用自定义JDialog Then you can use: 然后,您可以使用:

dialog.setLocationByPlatform( true );

and the location will be determined by the fules of each platform for new windows. 位置将取决于每个平台用于新窗口的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM