2013-09-25 4 views
0

나는 자바 스윙을 공부하고 그리고 내가 읽고있다이 간단한 코드 자습서와 관련된 몇 가지 의문이 있습니다일부 의심

package com.andrea.execute; 

import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 

/* An istance of a SimpleExample class is a JFrame object: a top level container */ 
public class SimpleExample extends JFrame { 

    /* Create a new SimpleExample object (that is a JFrame object) and set some property */ 
    public SimpleExample() { 
     setTitle("Simple example"); 
     setSize(300, 200); 
     setLocationRelativeTo(null);    // Center the window on the screen. 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
    } 

    public static void main(String[] args) { 

     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       SimpleExample ex = new SimpleExample(); 
       ex.setVisible(true); 
      } 
     }); 
    } 
} 

을 논리는 매우 간단합니다. SimpleExample에서 상속받은 클래스 JFrame Swing 클래스입니다. 따라서 SimpleExample은 최상위 컨테이너가됩니다.

이 클래스는 주() 방법을 포함하고 지금은 2 의심이 :

1) 당사에서 main() 메소드가 실행이 코드입니다 :

SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       SimpleExample ex = new SimpleExample(); 
       ex.setVisible(true); 
      } 
     }); 

그것을 SwingUtilities 클래스에서 invokeLater() 메서드를 호출하고 새 Runnable 객체를 전달합니다.

내가 아는 설명서를 읽는 것이 응용 프로그램을 스윙 이벤트 대기열에 배치하는 방법입니다. 모든 UI 업데이트의 동시성을 보장하는 데 사용됩니다.

내가 이해해야 할 몇 가지 문제가있는 것은 구현 방법입니다. 입력 매개 변수가이 "물건"을 통과 invokeLater() 방법의 내부

:

new Runnable() { 
     @Override 
     public void run() { 
      SimpleExample ex = new SimpleExample(); 
      ex.setVisible(true); 
     } 
    }); 

는이 물건은 무엇입니까? 무엇을 나타 냅니까? 어떻게 작동합니까?

TNX

안드레아

+1

Runnable 인터페이스를 구현하는 익명 클래스입니다. –

+2

아마도 의심 할 여지없이 의심의 여지가 있음을 의미합니다. – Mikeb

답변

4

가이 링크를 참조하십시오.

당신은 같은 익명 클래스없이 사용할 수 있습니다

class SimpleExample extends JFrame { 

    public SimpleExample() { 
     setTitle("Simple example"); 
     setSize(300, 200); 
     setLocationRelativeTo(null);     
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new App()); 
    } 
} 

class App implements Runnable { 

    public void run() { 
     SimpleExample ex = new SimpleExample(); 
     ex.setVisible(true); 
    } 
} 

그러나 익명 클래스는 더 편리합니다.