car.java

来自「本程序可以实现一个小车可有人工控制其走动」· Java 代码 · 共 84 行

JAVA
84
字号
package accp.car;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Car extends JFrame implements Runnable{
	
	Icon ico=new ImageIcon("image/car.gif");
    MyJLabel mjl = new MyJLabel(ico);
	Container cp = this.getContentPane();
	
	
	//static{
	//	mjl.setText("wangbo");
	//}
	
	public Car(){
	    cp.setLayout(new FlowLayout());
	    cp.add(mjl);
	    
	    mjl.addMouseListener(new MouseListener()
	    {
	    	public void mouseClicked(MouseEvent e){
	    	}
	    	public void mousePressed(MouseEvent e){
	    	}
	    	public void mouseReleased(MouseEvent e){
	    		JOptionPane.showMessageDialog(null,"Car in "+mjl.getLocation().getX()+" "+mjl.getLocation().getY());
	    	}
	    	public void mouseEntered(MouseEvent e){
	    	}
	    	public void mouseExited(MouseEvent e){
	    	}
	    });
	    Thread runner = new Thread(this);
	    this.setSize(900,200);
	    this.show();
	    runner.start();
	}
	
	public void run(){
		Thread t = new Thread(mjl);
		t.start();
	}
	
	public static void main(String[] args){
		new Thread(new Car()).start();
	}
}

class MyJLabel extends JLabel implements Runnable{
	MyJLabel(Icon ico){
		
		super(ico);
	}
	
	/*
	MyJLabel(String[] s){
		super(s);
	}
	*/
	
	public void run(){
		for(int i=0;i<800;i++){
			this.setLocation(30+i,60);
			System.out.println(this.getLocation().getX()+","+this.getLocation().getY());
			try{
				Thread.sleep(100);
				if(this.getLocation().getX()>200){
					//this.invalidate();
					//this.disable();
					//this.show(false);	
					//Thread.currentThread().stop();
					//System.out.println(Thread.currentThread().isInterrupted());
				}
			}catch(Exception e){
			}
		}	
	}
	

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?