SwingUtilities.getWindowAncestor (구성 요소)은 구성 요소의 첫 번째 상위 창을 반환하는 메서드입니다.
JLabel과 함께 (구성 요소로서) 이것을 사용하여 JOptionPane 메시지 대화 상자 창에 대한 참조를 얻을 수 있습니다. 그러면 JOptionPane 메시지 대화 상자를 닫고 10 초로 설정할 수 있습니다. 그래서 같은 뭔가 :
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class TestClass {
public static void main(String[] args)
{
errorpopup(new Exception("Error"));
}
private static void errorpopup(Exception m)
{
JLabel messageLabel = new JLabel("<html><body><p style='width: 300px;'>"+m.toString()+"</p></body></html>");
Timer timer = new Timer(10000,
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{
SwingUtilities.getWindowAncestor(messageLabel).dispose();
}
});
timer.setRepeats(false);
timer.start();
JOptionPane.showMessageDialog(null, messageLabel, "Error Window Title", JOptionPane.ERROR_MESSAGE);
}
}
또한,이 링크는 그것을 할 수있는 방법의 또 다른 예를 가지고 How to close message dialog programmatically?
당신은 그것이 당신의 목적에 맞게 만들기 위해 약간의 작업을 할 필요를하지만 보여줍니다 당신은 깔끔한 사용자에게 카운트 다운을 어떻게 표시 할 수 있었 을까?
여기를 확인하십시오. https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html – user1803551