2012-09-16 3 views
-2

단추없이 Timer를 사용하여 다른 JFrame을 호출 : 시간이 줄어들고 버튼없이 다른 JFrame이 열림. 도와주세요. netbeans에서 사용버튼없이 타이머를 사용하여 다른 JFrame 호출하기

+1

질문은 무엇입니까? –

+4

Google 번역기 등을 사용하여 질문을 만든 것으로 추측됩니다. 일종의 친절한 것처럼 보이기 때문입니다. –

+1

[여러 JFrames 사용, 좋음/나쁨 연습]을 참조하십시오. (http://stackoverflow.com/a/9554657/418556) –

답변

13

귀하의 질문은 명확하지 않지만 여러 프레임을 사용하면 not recommended입니다. 대안으로 다음과 같은 모덜리스 대화 상자를 고려하십시오. 대화 상자의 JOptionPanePropertyChangeEvent을 청취하며 TIME_OUTjavax.swing.Timer을 사용합니다. JOptionPane 버튼은 편리하지만 은 필요하지 않습니다..

enter image description here

import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.WindowEvent; 
import java.beans.PropertyChangeEvent; 
import java.beans.PropertyChangeListener; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.Timer; 

/** 
* @see https://stackoverflow.com/a/12451673/230513 
*/ 

public class JOptionTimeTest implements ActionListener, PropertyChangeListener { 

    private static final int TIME_OUT = 10; 
    private int count = TIME_OUT; 
    private final Timer timer = new Timer(1000, this); 
    private JDialog dialog = new JDialog(); 
    private final JOptionPane optPane = new JOptionPane(); 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      public void run() { 
       new JOptionTimeTest().createGUI(); 
      } 
     }); 
    } 

    private void createGUI() { 
     JFrame frame = new JFrame("Title"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLocationRelativeTo(null); 
     timer.setCoalesce(false); 
     optPane.setMessage(message()); 
     optPane.setMessageType(JOptionPane.INFORMATION_MESSAGE); 
     optPane.setOptionType(JOptionPane.DEFAULT_OPTION); 
     optPane.addPropertyChangeListener(this); 
     dialog.add(optPane); 
     dialog.pack(); 
     frame.add(new JLabel(frame.getTitle(), JLabel.CENTER)); 
     frame.pack(); 
     frame.setVisible(true); 
     dialog.setLocationRelativeTo(frame); 
     dialog.setVisible(true); 
     timer.start(); 
    } 

    public void propertyChange(PropertyChangeEvent e) { 
     String prop = e.getPropertyName(); 
     if (JOptionPane.VALUE_PROPERTY.equals(prop)) { 
      thatsAllFolks(); 
     } 
    } 

    public void actionPerformed(ActionEvent e) { 
     count--; 
     optPane.setMessage(message()); 
     if (count == 0) { 
      thatsAllFolks(); 
     } 
     timer.restart(); 
    } 

    private String message() { 
     return "Closing in " + count + " seconds."; 
    } 

    private void thatsAllFolks() { 
     dialog.setVisible(false); 
     dialog.dispatchEvent(new WindowEvent(
      dialog, WindowEvent.WINDOW_CLOSING)); 
    } 
} 
+0

참고 자료 [튜토리얼 섹션] (http://docs.oracle.com/javase) /tutorial/uiswing/components/dialog.html#stayup). – trashgod