2014-11-30 6 views
0

JMenu 팝업과 관련된 문제가 있습니다. 때때로 만 작동합니다. 때때로 나는 자바 팝업을 전혀 얻지 못한다. 가끔 내 파일 및 수정 옵션이 완전히 누락되었습니다. 이것이 내 코드의 모습입니다.JMenu 팝업은 가끔씩 만 작동합니다.

import javax.swing.*; 

public class menu { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     // TODO code application logic here 
     JFrame f = new JFrame(); 
     f.setVisible(true); 
     f.setSize(400,400); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setLocationRelativeTo(null); 

     JMenuBar mb = new JMenuBar(); 

     JMenu file = new JMenu ("File"); 
     mb.add(file); 
     JMenu edit = new JMenu("Edit"); 
     mb.add(edit); 

     f.setJMenuBar(mb); 
    } 

} 

답변

3

전화는 핵심 UI를 초기화 한 후에 만 ​​JFramesetVisible ... 또한

JFrame f = new JFrame(); 
// Don't call this here... 
//f.setVisible(true); 
f.setSize(400,400); 
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
f.setLocationRelativeTo(null); 

JMenuBar mb = new JMenuBar(); 

JMenu file = new JMenu ("File"); 
mb.add(file); 
JMenu edit = new JMenu("Edit"); 
mb.add(edit); 

f.setJMenuBar(mb); 
// Call it here 
f.setVisible(true); 

, 당신이/만들 때만 이벤트 파견의 컨텍스트 내에서 UI를 업데이트 할 수 있도록 스레드, 자세한 내용은 Initial Threads 참조

+0

트릭을 해 주셔서 감사합니다 :). – cokedude

+0

나는 Event Dispatching Thread를 올바르게 사용하고 있습니까? 테스트를 위해 이것을 사용해야합니까? http://docs.oracle.com/javase/8/docs/api/javax/swing/SwingUtilities.html#isEventDispatchThread-- – cokedude

+0

아니요 (EDT의 컨텍스트에서 UI를 초기화하는) 아니며 그렇습니다. SwingUtilites.inokeLater' 또는'EventQueue.invokeLater'를 사용할 필요가 있습니다. 둘 다 똑같은 일을합니다. – MadProgrammer

관련 문제