⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 《java就业培训教程》_张孝祥_书内源码_08.txt

📁 配合网上流行的张孝祥JAVA就业培训视频
💻 TXT
字号:
《Java就业培训教程》 作者:张孝祥 书中源码 
 网址:www.itcast.cn
《Java就业培训教程》P290源码
程序清单:TestFrame.java
import java.awt.*;
import java.awt.event.*;
public class TestFrame
{
	public static void main(String [] args)
	{
		Frame f=new Frame("IT资讯交流网");
		f.setSize(300,300);
		f.setVisible(true);
		f.addWindowListener(new MyWindowListener());
	}
}
class MyWindowListener implements WindowListener
{
	public void windowClosing(WindowEvent e)
	{
		e.getWindow().setVisible(false);
		((Window)e.getComponent()).dispose();
		System.exit(0);
	}
	public void windowActivated(WindowEvent e){}
	public void windowClosed(WindowEvent e){}
	public void windowDeactivated(WindowEvent e){}
	public void windowDeiconified(WindowEvent e){}
	public void windowIconified(WindowEvent e){}
	public void windowOpened(WindowEvent e){}
}
《Java就业培训教程》P295源码
import java.awt.*;
import java.awt.event.*;
public class TestFrame implements ActionListener
{
		Frame f=new Frame("IT资讯交流网");
		public static void main(String [] args)
		{
			TestFrame tf=new TestFrame();
			tf.init();
		}
		public void init()
		{
			Button btn=new Button("退出");
			btn.addActionListener(new TestFrame());
			f.add(btn);
			f.setSize(300,300);
			f.setVisible(true);
		}
		public void actionPerformed(ActionEvent e)
		{
			f.setVisible(false);
			f.dispose();
			System.exit(0);
		}
}
《Java就业培训教程》P296源码
import java.awt.*;
import java.awt.event.*;
class TestFrame
{
	Frame f=new Frame("IT资讯交流网");
	public static void main(String [] args)
	{
		new TestFrame().init();
	}
	public void init()
	{
	    Button btn=new Button("退出");
	    btn.addActionListener(new ActionListener()
		{
		        public void actionPerformed(ActionEvent e)
		        {
			        f.setVisible(false);
			        f.dispose();
			        System.exit(0);
		        }	
		});
		f.add(btn);
		f.setSize(300,300);
		f.setVisible(true);
		
	}
}
《Java就业培训教程》P300源码
 程序清单:TestMyButton.java
import java.awt.*;
import java.awt.event.*;
class MyButton extends Button
{
	private MyButton friend;
	public void setFriend(MyButton friend)
	{
		this.friend = friend;
	}
	public MyButton(String name)
	{
		super(name);
		enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
	}
	protected void processMouseMotionEvent(MouseEvent e)
	{
		setVisible(false);
		friend.setVisible(true);
	}
}
public class TestMyButton
{
	public static void main(String [] args)
	{
		MyButton btn1 =new MyButton("你来抓我呀!");
		MyButton btn2 =new MyButton("你来抓我呀!");
		btn1.setFriend(btn2);
		btn2.setFriend(btn1);
		btn1.setVisible(false);
		Frame f =new Frame("it315");
		f.add(btn1, "North");//将btn1增加到f的北部
		f.add(btn2, "South");//将btn2增加到f的南部
		f.setSize(300,300);
		f.setVisible(true);
		btn1.setVisible(false);	
	}
}
《Java就业培训教程》P301源码
程序清单:DrawLine.java
import java.awt.*;
import java.awt.event.*;
public class DrawLine
{
	Frame f= new Frame("IT资讯交流网");
	public static void main(String [] args)
	{
		new DrawLine().init();	
	}
	public void init()
	{
		f.setSize(300,300);
		f.setVisible(true);
		f.addMouseListener(new MouseAdapter()
        {
	        int orgX;
	        int orgY;
            public void mousePressed(MouseEvent e)
            {
		        orgX=e.getX();
		        orgY=e.getY();
            }
            public void mouseReleased(MouseEvent e)
	        {
				f.getGraphics().setColor(Color.red);
//设置绘图颜色为红色
		    f.getGraphics().drawLine(orgX,orgY,e.getX(),e.getY());
			}	
    	});
	}
}
《Java就业培训教程》P306源码
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
class MyLine
{
	private int x1;
	private int y1;
	private int x2;
	private int y2;
	public MyLine(int x1,int y1,int x2,int y2)
	{
		this.x1=x1;
		this.y1=y1;
		this.x2=x2;
		this.y2=y2;
	}
	public void drawMe(Graphics g)
	{
		g.drawLine(x1,y1,x2,y2);
	}
}
public class RerawAllLine extends Frame
{
	Vector vLines=new Vector();
	public static void main(String [] args)
	{
		RedrawAllLine f=new RedrawAllLine();
		f.init();
	}
	public void paint(Graphics g)
	{
		g.setColor(Color.red);
		Enumeration e=vLines.elements();
		while(e.hasMoreElements())
		{
			MyLine ln=(MyLine)e.nextElement();
			ln.drawMe(g);
		}
	}
	public void init()
	{
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e)
			{
				((Window)e.getSource()).dispose();
				System.exit(0);
			}
			});
		addMouseListener(new MouseAdapter(){
			int orgX;
			int orgY;
			public void mousePressed(MouseEvent e)
			{
				orgX=e.getX();
				orgY=e.getY();
			}
			public void mouseReleased(MouseEvent e)
			{
				Graphics g=e.getComponent().getGraphics();
				g.setColor(Color.red);
				g.drawLine(orgX,orgY,e.getX(),e.getY());
				vLines.add(new MyLine(orgX,orgY,e.getX(),e.getY()));
			}
			});
		this.setSize(300,300);
		setVisible(true);
	}
}
《Java就业培训教程》P311源码
import java.awt.*;
import java.awt.event.*;
public class DrawImage extends Frame
{
	Image img=null;
	public static void main(String [] args)
	{
		DrawImage f= new DrawImage();
		f.init();
	}
	public void init()
	{
		img=this.getToolkit().getImage("c:\\test.gif");
		setSize(300,300);
		setVisible(true);
		this.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
				
	}
	public void paint(Graphics g)
	{
			getGraphics().drawImage(img,0,0,this);	
	}
}
《Java就业培训教程》P312源码
程序清单:DrawLine.java
import java.awt.*;
import java.awt.event.*;
public class DrawLine extends Frame
{
	Image oimg=null;
	Graphics og=null;
	public static void main(String [] args)
	{
		new DrawLine().init();	
	}
	public void init()
	{
		setSize(300,300);
		setVisible(true);
		Dimension d=getSize();
		oimg=createImage(d.width,d.height);
		og=oimg.getGraphics();	
		addMouseListener(new MouseAdapter()
		{
			int orgX;
			int orgY;
			public void mousePressed(MouseEvent e)
			{
				orgX=e.getX();
				orgY=e.getY();
			}
			public void mouseReleased(MouseEvent e)
			{
				Graphics g=getGraphics();
				g.setColor(Color.red);//设置绘图颜色为红色
				g.setFont(new Font("隶书",Font.ITALIC|Font.BOLD,30));
				//设置文本的字体
				g.drawString(new String(orgX +"," +orgY),orgX,orgY);
				//打印鼠标按下时的坐标文本
				g.drawString(new String(e.getX() +"," +e.getY()),
				e.getX(),e.getY());//打印鼠标释放时的坐标文本
				g.drawLine(orgX,orgY,e.getX(),e.getY());
				og.setColor(Color.red);//设置绘图颜色为红色
				og.setFont(new Font("隶书",Font.ITALIC|Font.BOLD,30));
				//设置文本的字体
				og.drawString(new String(orgX +"," +orgY),orgX,orgY);
				//打印鼠标按下时的坐标文本
				og.drawString(new String(e.getX() +"," +e.getY()),
				e.getX(),e.getY());//打印鼠标释放时的坐标文本
				og.drawLine(orgX,orgY,e.getX(),e.getY());
			}
		});
	}
	public void paint(Graphics g)
	{
		if(oimg !=null)
			g.drawImage(oimg,0,0,this);
	}
}

 
  
     

⌨️ 快捷键说明

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