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

📄 sourcecode.txt

📁 张孝祥的《Java就业培训教程》书中源代码
💻 TXT
📖 第 1 页 / 共 5 页
字号:
import java.awt.*;
import java.awt.event.*;
public class TestCheckbox 
{
	Checkbox cb1=new Checkbox("你喜欢我吗?",true);
	CheckboxGroup cbg=new CheckboxGroup();
	Checkbox cb2=new Checkbox("喜欢",cbg,true);
	Checkbox cb3=new Checkbox("不喜欢",cbg,false);
	
	public void init()
	{		
		Frame f=new Frame("TestCheckBox");
		//创建FlowLayout布局管理器,关于布局管理器,本章后面有专门的讲解,
		看不明白//的读者暂时可以不去下面两句代码的作用。
		FlowLayout fl=new FlowLayout();
		f.setLayout(fl);
		 
		f.add(cb1);
		f.add(cb2);
		f.add(cb3);
		cb1.addItemListener(new CbItemListener());
		cb2.addItemListener(new CbItemListener());
		cb3.addItemListener(new CbItemListener());
		f.setBounds(0,0,300,100);
		f.setVisible(true);
			f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
				{
					System.exit(0);
				}
			});
	}
	class CbItemListener implements ItemListener
	{
		public void itemStateChanged(ItemEvent e)
		{
			Checkbox cb = (Checkbox)e.getItemSelectable();
			if(cb.getLabel().equals("你喜欢我吗?"))
			{
				if(cb.getState() == true)
					System.out.println("我很高兴");
				else
					System.out.println("我很伤心");
			}
			/*else if(cb.getLabel().equals("喜欢"))
			{
				if(e.getStateChange() == ItemEvent.SELECTED)
					System.out.println("我也喜欢你");
				else
					System.out.println("我也不喜欢你");
			}*/
			else
			{
				Checkbox cbx =cbg.getSelectedCheckbox();
				if(cbx != null)
					System.out.println(cbx.getLabel());
			}
		}
	}
	public static void main(String[] args)
	{
		new TestCheckbox().init();		
	}
}
《Java就业培训教程》P321源码
程序清单:TestChoice.java
import java.awt.*;
import java.awt.event.*;
public class TestChoice 
{
	Choice ch=new Choice(); //创建Choice对象
	TestChoice()
	{
		ch.add("choice1"); //用add方法向列表里加入选项
		ch.add("choice2"); //用add方法向列表里加入选项
		ch.add("choice3"); //用add方法向列表里加入选项
		FlowLayout fl=new FlowLayout();
		Frame f=new Frame("TestChoice");
		f.setLayout(fl);
		f.add(ch); //把列表加入到窗口
		f.setBounds(0,0,200,100);
		f.setVisible(true);
		f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
				{
					System.exit(0);
				}
			});
	}
	public static void main(String[] args)
	{
		new TestChoice();		
	}
}
《Java就业培训教程》P323源码
程序清单:TestMenuBar.java
import java.awt.*;
import java.awt.event.*;
public class TestMenuBar 
{
	MenuBar menubar=new MenuBar(); //创建菜单条对象
	Menu fileM=new Menu("File"); //创建各菜单
	Menu editM=new Menu("Edit"); //创建各菜单
	Menu toolsM=new Menu("Tools"); //创建各菜单
	Menu helpM=new Menu("Help"); //创建各菜单
		
	MenuItem fileMI1=new MenuItem("New"); //创建各菜单项
	MenuItem fileMI2=new MenuItem("Open"); //创建各菜单项
	MenuItem fileMI3=new MenuItem("Save"); //创建各菜单项
	CheckboxMenuItem fileMI5=new CheckboxMenuItem("Quit",true);
	 //创建各菜单项
	 
    	Menu filePrint = new Menu("print");//创建子菜单
    	MenuItem printM1 = new MenuItem("preview");
    	MenuItem printM2 = new MenuItem("setting");
    	
	TestMenuBar()
	{
		FlowLayout fl=new FlowLayout();
		
		Frame f=new Frame("TestMenuBar");
		f.setLayout(fl);
		
		menubar.add(fileM); //将菜单加入菜单条
		menubar.add(editM); 
		menubar.add(toolsM); 
		menubar.add(helpM); 
		
		fileM.add(fileMI1); //将菜单项加入file菜单中
		fileM.add(fileMI2); 
		fileM.add(fileMI3);
		
		filePrint.add(printM1);//将菜单项加入print菜单中
		filePrint.add(printM2);
		fileM.add(filePrint);
 //将print菜单作为一个菜单项加入file菜单中
		
		fileM.addSeparator(); //将一条分割线加入菜单中
		fileM.add(fileMI5); //将菜单项加入菜单中
		f.setMenuBar(menubar); //把整个菜单系统显示在窗口中
		f.setBounds(0,0,250,200);
		f.setVisible(true);
		f.addWindowListener(new WindowAdapter()
{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
	}
	
	public static void main(String[] args)
	{
		new TestMenuBar();
	}
}
《Java就业培训教程》P327源码
程序清单:TestDialog.java
import java.awt.*; 
import java.awt.event.*;
public class TestDialog
{
	TextField tf = new TextField(10);
	Button b1=new Button("模态显示");
	Button b2=new Button("非模态显示");
	Frame f=new Frame("TestDialog");
	

	Button b3=new Button("确定");
        Dialog dlg = new Dialog(f, "Dialog Title", true);
        FlowLayout fl=new FlowLayout();
	TestDialog()
	{
	f.setLayout(fl);
	f.add(tf);
	f.add(b1);
	f.add(b2);
	b1.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e)
		{
			dlg.setModal(true);
			dlg.setVisible(true);
			tf.setText("www.it315.org");
		}
		});
	b2.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e)
		{
			dlg.setModal(false);
			dlg.setVisible(true);
			tf.setText("www.it315.org");
		}
		});	
	f.setBounds(0,0,400,200);
	f.setVisible(true);
	f.addWindowListener(new WindowAdapter(){
		public void windowClosing(WindowEvent e)
		{
			System.exit(0);
		}
		});

	
	dlg.setLayout(fl);
	dlg.add(b3);
	b3.addActionListener(new ActionListener()
	{
		public void actionPerformed(ActionEvent e)
		{
			dlg.dispose();
		}
	});

	dlg.setBounds(0,0,200,150);
		
	}
	public static void main(String[] args)
	{
	new TestDialog();		
	}
}
《Java就业培训教程》P330源码
程序清单:TestPane.java 
import java.awt.*; 
import java.awt.event.*;
public class TestPane
{

	TestPane()
	{
		Frame f=new Frame("TestDialog");
		ScrollPane sp = new ScrollPane();
		TextArea ta = new TextArea("",10,50,TextArea.SCROLLBARS_NONE);
		sp.add(ta);
		
		f.add(sp);
		f.setSize(200,200);
		f.setVisible(true);
		f.addWindowListener(new WindowAdapter(){
		public void windowClosing(WindowEvent e)
		{
			System.exit(0);
			}
		});
	}
	public static void main(String[] args)
	{
		new TestPane();		
	}
}
《Java就业培训教程》P336源码
程序清单:TestCardLayout.java
import java.awt.*;
import java.awt.event.*;
public class TestCardLayout
{
	CardLayout cl = new CardLayout();
	Panel plCenter = new Panel();
	public static void main(String [] args)
	{
		new TestCardLayout().init();
	}
	public void init()
	{
		Frame f=new Frame("布局管理器");
		Panel plWest = new Panel();
		f.add(plWest,"West");
		f.add(plCenter);
		
		plWest.setLayout(new GridLayout(3,1));
		Button btnPrev = new Button("prev");
		plWest.add(btnPrev);
		Button btnNext = new Button("next");
		plWest.add(btnNext);
		Button btnThree = new Button("three");
		plWest.add(btnThree);
		
		plCenter.setLayout(cl);
		plCenter.add(new Button("One"),"1");
		plCenter.add(new Button("two"),"2");
		plCenter.add(new Button("three"),"3");
		plCenter.add(new Button("four"),"4");
		plCenter.add(new Button("five"),"5");

		class MyActionListener implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
			{
				if(e.getActionCommand().equals("prev"))
					cl.previous(plCenter);
				else if(e.getActionCommand().equals("next"))
					cl.next(plCenter);
				else if(e.getActionCommand().equals("three"))
					cl.show(plCenter,"3");
			}
		}
		MyActionListener ma = new MyActionListener();
		btnPrev.addActionListener(ma);
		btnNext.addActionListener(ma);
		btnThree.addActionListener(ma);
				
		f.setSize(300,300);
		f.setVisible(true);
	}
}
《Java就业培训教程》P343源码
程序清单:Calculator.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator implements ActionListener
{
    JFrame jf = new JFrame("Calculator");
    JTextField tf = new JTextField();
    public void init()
    {
     	Container c = jf.getContentPane(); 
     	tf.setHorizontalAlignment(JTextField.RIGHT);  
        c.add(tf,"North");
        
        JPanel pnl=new JPanel();
        c.add(pnl,"Center");
 
        pnl.setLayout(new GridLayout(4,4));
        JButton b=new JButton("1");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("2");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("3");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("+");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("4");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("5");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("6");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("-");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("7");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("8");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("9");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("*");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("0");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton(".");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("=");
        b.addActionListener(this);
        pnl.add(b);
        b=new JButton("\\");
        b.addActionListener(this);
        pnl.add(b);
        
        jf.setSize(200,300);
        jf.setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        tf.setText(tf.getText()+e.getActionCommand());
    }
    public static void main(String [] args)
    {
	new Calculator().init();
    }
}
 
   
--------------------------------------------------------------------------------
 
《Java就业培训教程》 作者:张孝祥 书中源码 
《Java就业培训教程》P347源码
<script language="vbscript">
<!--
option explicit
private strStatus
private intSpace
private intDir

sub window_onload()
	strStatus="www.it315.org"
	intSpace=0
	intDir=1
	window.SetTimeout "Scroll",100
end sub
sub Scroll()
	dim strTemp
	intSpace=intSpace+1*intDir
	if intSpace>40 or intSpace<=0 then
		intDir=-1*intDir
	end if
	strTemp=string(intSpace," ")
	window.status=strTemp & strStatus
	window.SetTimeout "Scroll",100
end sub
-->
</script>
《Java就业培训教程》P354源码
import java.applet.*;
import java.awt.*;
import java.net.*;
public class MyApplet extends Applet implements Runnable
{
	Image [] imgs=new Image[10];
	int index=0;	
	public void init()
	{
		try{
			for(int i=0;i<10;i++)
			{
				imgs[i]=getImage(new URL(getCodeBase(),"img\\T" +
					(i+1) +".gif"));
				/*imgs[i]=getToolkit().getImage(new URL(getCodeBase(),
				"img\\T" +(i+1) +".gif"));*/
}
			new Thread(this).start();
		}
		catch(Exception e){e.printStackTrace();}
	}
	public void paint(Graphics g)
	{
		g.drawImage(imgs[index],0,0,this);
/*下面设置的字体必须是你的计算机中存在的字符,打开记事本程序的字体设置对话框,
从其中可选的字体中复制一个到这里就行了。*/
		g.setFont(new Font("隶书",Font.ITALIC|Font.BOLD,30));
		index=(index+1)%10;
		g.drawString("" + index,0,50);
	}
	
	public void run()
	{
		while(true)
		{
			repaint();
			try
{
Thread.sleep(100);
}catch(Exception e){} 	
		}
	}
}
《Java就业培训教程》P364源码
import java.applet.*;
import java.awt.*;
public class Parameters extends Applet
{
	private String toDisplay;
	private int speed;
	public void init() 
	{
 		String pv;
	 	pv = getParameter("speed");
 		if (pv == null)
		{
 			speed = 10;
 		} 
		else 
		{
 			speed = Integer.parseInt (pv);
 		}
	 	toDisplay = "Speed Parameter: " + speed;
	}
	public void paint(Graphics g)
	{
		g.drawString(toDisplay, 25, 25);
	}
}
《Java就业培训教程》P367源码
程序清单: MyApplet.java
import java.applet.*;
import java.awt.*;
public class MyApplet extends Applet
{
	static int count=0;
	static int count1=0;
	public MyApplet()
	{
		count1++;
	}
	public void init()
	{
		count++;
	}
	public void paint(Graphics g)
	{
		g.drawString(count +"," +count1,50,60);
	}
}
 
   
--------------------------------------------------------------------------------
 
《Java就业培训教程》 作者:张孝祥 书中原代码 
《Java就业培训教程》P374的Java原代码
发送程序:UdpSend.java
import java.net.*;
public class UdpSend
{
	public static void main(String [] args) throws Exception
	{
		DatagramSocket ds=new DatagramSocket();
		String str="hello world";
		DatagramPacket dp=new DatagramPacket(str.getBytes(),str.length(),
InetAddress.getByName("192.168.0.213"),3000);
		ds.send(dp);
		ds.close();
	}
}

接收程序:UdpRecv.java
import java.net.*;
public class UdpRecv
{
	public static void main(String [] args) throws Exception
	{
		DatagramSocket	ds=new DatagramSocket(3000);
		byte [] buf=new byte[1024];
		DatagramPacket dp=new DatagramPacket(buf,1024);
		ds.receive(dp);
		String strRecv=new String(dp.getData(),0,dp.getLength()) +
		" from " + dp.getAddress().getHostAddress()+":"+dp.getPort(); 
		System.out.println(strRecv);
		ds.close();
	}
}
《Java就业培训教程》P378的Java原代码
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class Chat 
{
	Frame f=new Frame("我的聊天室");
	TextField tfIP=new TextField(15);
	List lst=new List(6);
	DatagramSocket ds;
/*由于DatagramSocket的构造函数声明可能抛出异常,我们的程序需要用try…catch语句
进行异常捕获处理,所以不能直接在这里调用DatagramSocket的构造函数对ds进行初始化,
我们需要将ds的初始化放在Chat类的构造函数中去完成。*/
	public Chat()
	{
	        try
	        {
	            ds=new DatagramSocket(3000);
	        }catch(Exception ex){ex.printStackTrace();}
	}
	public static void main(String [] args)
	{
		Chat chat=new Chat();
		chat.init();
	}
	public void init()
	{
		f.setSize(300,300);
		f.add(lst);

		Panel p=new Panel();
		p.setLayout(new BorderLayout());
		p.add("West",tfIP);
		TextField tfData=new TextField(20);
		p.add("Ea

⌨️ 快捷键说明

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