2014-09-27 2 views
2

일정 기간 (예 : 5 초) 후에 자동으로 응용 프로그램 또는 애플릿 내부의 팝업 창을 닫을 수있는 방법이 있습니까?JAVA : 자동 팝업 창을 닫는 방법?


나는 해결책을 발견했습니다 : 귀하의 답변들에 대한

public static void main(String[] args) { 
    JFrame f = new JFrame(); 
    final JDialog dialog = new JDialog(f, "Test", true); 
    Timer timer = new Timer(2000, new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      dialog.setVisible(false); 
      dialog.dispose(); 
     } 
    }); 
    timer.setRepeats(false); 
    timer.start(); 

    dialog.setVisible(true); // if modal, application will pause here 

    System.out.println("Dialog closed"); 
} 

감사 : 같은 찾고 될 수있는 사람들을위한

.

+0

유용 할 수 있습니다. http://stackoverflow.com/questions/1306868/can-i-set-a-timer-on-a-java-swing-jdialog-box-to-close-after-a-number -of-millise –

+0

그 링크에 대해 정말 고마워, 내가 찾고 있었던 대답이 들어 있었다;) –

답변

1

자동으로 팝업 창을 닫으려면 새 스레드를 설정하고 타이머를 설정해야합니다.

public class FrmPopUpInfo extends JDialog{ 
public boolean isCancel = false; 

private String trackHeader = "Pop Up is:"; 
private final String message; 

public FrmPopUpInfo(String message){ 
    this.message = message; 
    initComponents(); 
} 

private void initComponents() { 
    Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();// size of the screen 
    Insets toolHeight = Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration());// height of the task bar 
    setLocation(scrSize.width - 275, scrSize.height - toolHeight.bottom - 120); 
    ImageIcon image; 

    setSize(225,120); 
    setLayout(null); 
    setUndecorated(true); 
    setLayout(new GridBagLayout()); 

    GridBagConstraints constraints = new GridBagConstraints(); 
    constraints.gridx = 0; 
    constraints.gridy = 0; 
    constraints.weightx = 1.0f; 
    constraints.weighty = 1.0f; 
    constraints.insets = new Insets(5, 5, 5, 5); 
    constraints.fill = GridBagConstraints.BOTH; 

    JLabel headingLabel = new JLabel(trackHeader + message); 
    image = new ImageIcon(Toolkit.getDefaultToolkit().getImage(FrmPopUpInfo.class.getResource("/images/yourImage.jpg"))); 
    headingLabel .setIcon(image); 
    headingLabel.setOpaque(false); 

    add(headingLabel, constraints); 

    constraints.gridx++; 
    constraints.weightx = 0f; 
    constraints.weighty = 0f; 
    constraints.fill = GridBagConstraints.NONE; 
    constraints.anchor = GridBagConstraints.NORTH; 
    JButton cloesButton = new JButton(new AbstractAction("x") { 
     @Override 
     public void actionPerformed(final ActionEvent e) { 
       dispose(); 
     } 
    }); 
    cloesButton.setMargin(new Insets(1, 4, 1, 4)); 
    cloesButton.setFocusable(false); 

    add(cloesButton, constraints); 

    setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); 
    setVisible(true); 
    setAlwaysOnTop(true); 

    new Thread(){ 
      @Override 
      public void run() { 
       try { 
         Thread.sleep(5000); // time after which pop up will be disappeared. 
         dispose(); 
       } catch (InterruptedException e) { 
         e.printStackTrace(); 
       } 
      }; 
    }.start(); 
} 
} 

아래

try 코드 당신은 5 초에서 사라집니다이

FrmPopUpInfo frm = new FrmPopUpInfo(); 

팝업 대화 상자와 같은 다른 프레임에서 클래스 위의 호출.

관련 문제