2012-11-24 1 views
1

이 프로그램이 작동하지 않는 이유는 무엇입니까? 내가 스레드를 배우려고 노력하고있어스레드가 실행되지 않고 jframe setresizeable이 작동하지 않는 이유

package eu.inmensia.learn; 

import java.awt.Canvas; 
import java.awt.Dimension; 

import javax.swing.JFrame; 

public class Client extends Canvas implements Runnable { 

    private static final long serialVersionUID = 1L; 
    public static final int WIDTH = 300; 
    public static final int HEIGHT = WIDTH/16 * 9; 
    public static final short SCALE = 3; 

    private Thread thread; 
    private JFrame frame = new JFrame(); 
    private boolean running = false; 

    public Client() { 
     Dimension size = new Dimension(WIDTH * SCALE, HEIGHT * SCALE); 
     setPreferredSize(size); 
    } 

    public synchronized void start() { 
     running = true; 
     thread = new Thread("display"); 
     thread.start(); // start the thread 
    } 

    public synchronized void stop() { 
     running = false; 
     try{ 
      thread.join(); // end the thread 
     }catch(InterruptedException e){ e.printStackTrace(); } 
    } 

    public void run() { 
     while(running){ 
      System.out.println("Running..."); 
     } 
    } 

    public static void main(String[] args) { 
     Client client = new Client(); 
     client.frame.setResizeable(false); 
     client.frame.setTitle("Program test"); 
     client.frame.add(client); 
     client.frame.pack(); 
     client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     client.frame.setLocationRelativeTo(null); 
     client.frame.setVisible(true); 

     client.start(); 
    } 
} 

(그것은 "실행 ..."인쇄되지 않습니다), 그리고 가장 열심히, 일이 내가 배운없는 경우는 하나입니다. OOP 난 그냥 당신이 무슨 일을하는지 알고 희망

new Thread(this) 

이의 xD 때문에

new Thread("display"); 

변경 그것의

답변

1

에 불과하다.

+0

초 나를 이길이 도움말을 희망

public synchronized void start() { running = true; thread = new Thread(this); thread.start(); // start the thread } 

을! 1 + –

+1

고마워, 이제 알았어! 그냥 그것을 잊어 버렸습니다. (PS. 새로운 스레드 (이 "디스플레이")로 변경 했으므로 작동합니다. –

0

일반 (읽기 ​​공백) 스레드 객체를 생성했습니다. 클래스로 매개 변수로 전달해야합니다.

thread = new Thread(this); 

이렇게하면 run 메소드가 Thread 객체에 바인딩됩니다. 스레드의 이름은 대개 그렇게 중요하지 않습니다. 전화 할 때 this example

2

를 참조하면 잘못된 방법으로이 작업을 수행 는 client.start(); 그것은 Client 클래스의 기능을 시작하고 그 기능을 당신이 비어있는 기본 run 메소드가 스레드 클래스의 새 인스턴스를 만들 호출

이 코드를 의미합니다 : 당신이

관련 문제