10
joptionpane에 대한 질문이 있습니다.프로그래밍 방식으로 메시지 대화 상자를 닫는 방법?
JOptionPane.showMessageDialog (...)를 사용하여 메시지 대화 상자를 만들 수 있습니다. 그러나 프로그래밍 방식으로 어떻게 닫을 수 있습니까?
joptionpane에 대한 질문이 있습니다.프로그래밍 방식으로 메시지 대화 상자를 닫는 방법?
JOptionPane.showMessageDialog (...)를 사용하여 메시지 대화 상자를 만들 수 있습니다. 그러나 프로그래밍 방식으로 어떻게 닫을 수 있습니까?
항상 보유하고있는 구성 요소의 WindowAncestor를 가져 와서 JOptionPane에 대한 참조를 가져온 다음 Window에서 dispose()
또는 setVisible(false)
을 호출하면 반환 될 수 있습니다. 완벽한보다 더
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class CloseOptionPane {
@SuppressWarnings("serial")
private static void createAndShowGui() {
final JLabel label = new JLabel();
int timerDelay = 1000;
new Timer(timerDelay , new ActionListener() {
int timeLeft = 5;
@Override
public void actionPerformed(ActionEvent e) {
if (timeLeft > 0) {
label.setText("Closing in " + timeLeft + " seconds");
timeLeft--;
} else {
((Timer)e.getSource()).stop();
Window win = SwingUtilities.getWindowAncestor(label);
win.setVisible(false);
}
}
}){{setInitialDelay(0);}}.start();
JOptionPane.showMessageDialog(null, label);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
아무것도 .... –
사람이 하나에서 점수를 공제 할 수는 LOL, 나는이를 위해 이렇게 일을 생각하지 않는다 : 창은 예를 들어
SwingUtilities.getWindowAncestor(component)
를 사용하여 얻을 수 있습니다 멋지게 만들어진 예 :-) –
감사. 열쇠는 JOptionPane이 표시되기 전에 필요한 모든 코드를 호출하는 것입니다. –