📄 sourcecode.txt
字号:
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
《Java就业培训教程》P263源码
程序清单:Serializatioan.java
import java.io.*;
public class serialization
{
public static void main(String args[])
throws IOException,ClassNotFoundException
{
Student stu=new Student(19,"dintdding",50,"huaxue");
FileOutputStream fos=new FileOutputStream("mytext.txt");
ObjectOutputStream os=new ObjectOutputStream(fos);
try
{
os.writeObject(stu);
os.close();
}catch(IOException e)
{
System.out.println(e.getMessage());
}
stu=null;
FileInputStream fi=new FileInputStream("mytext.txt");
ObjectInputStream si=new ObjectInputStream(fi);
try
{
stu=(Student)si.readObject();
si.close();
}catch(IOException e)
{
System.out.println(e.getMessage());
}
System.out.println("ID is:"+stu.id);
System.out.println("name is:"+stu.name);
System.out.println("age is:"+stu.age);
System.out.println("department is:"+stu.department);
}
}
class Student implements Serializable
{
int id;
String name;
int age;
String department;
public Student(int id,String name,int age,String department)
{
this.id=id;
this.name=name;
this.age=age;
this.department=department;
}
}
《Java就业培训教程》P275源码
import java.io.*;
public class CharDecoder
{
public static void main(String [] args) throws Exception
{
System.getProperties().put("file.encoding","iso8859-1");
System.out.println("please enter a Chinese String");
byte [] buf=new byte[1024];
int ch=0;
int pos=0;
String strInfo=null;
while(true)
{
ch =System.in.read();
System.out.println(Integer.toHexString(ch));
switch(ch)
{
case '\r':
break;
case '\n':
strInfo= new String(buf,0,pos);
for(int i=0;i<strInfo.length();i++)
{
System.out.println(Integer.toHexString
((int)strInfo.charAt(i)));
}
System.out.println(strInfo);
for(int i=0;i<pos;i++)
System.out.write(buf[i]);
System.out.println();//想想为什么要这一句
return;
default:
buf[pos++]=(byte)ch;
}
}
}
}
《Java就业培训教程》P282源码
程序清单: TestInOut.java
import java.io.*;
public class TestInOut implements Runnable
{
Process p=null;
public TestInOut()
{
try
{
p=Runtime.getRuntime().exec("java MyTest");
new Thread(this).start();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public void send()
{
try
{
OutputStream ops=p.getOutputStream();
while(true)
{
ops.write("help\r\n".getBytes());
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public static void main(String [] args)
{
TestInOut tio=new TestInOut ();
tio.send();
}
public void run()
{
try
{
InputStream in = p.getInputStream();
BufferedReader bfr=new BufferedReader(
new InputStreamReader(in));
while(true)
{
String strLine=bfr.readLine();
System.out.println(strLine);
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
class MyTest
{
public static void main(String [] args) throws IOException
{
while(true)
{
System.out.println("hi:"+
new BufferedReader(new InputStreamReader(System.in)).readLine());
}
}
}
--------------------------------------------------------------------------------
《Java就业培训教程》 作者:张孝祥 书中源码
《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);
}
}
--------------------------------------------------------------------------------
《Java就业培训教程》 作者:张孝祥 书中源码
《Java就业培训教程》P316源码
程序清单:TestStopWatch.java
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.SimpleDateFormat;
class StopWatch extends Canvas implements Runnable
{
private long startTime = 0;
private long endTime = 0;
private boolean bStart = false;
public StopWatch()
{
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
setSize(80,30);
}
protected void processMouseEvent(MouseEvent e)
{
if(e.getID() == MouseEvent.MOUSE_PRESSED)
{
/*鼠标按下时,启动计时线程,并让起始时间变量和终止时间变量都等于当前时间*/
bStart = true;
startTime = endTime = System.currentTimeMillis();
repaint();
new Thread(this).start();
}
else if(e.getID() == MouseEvent.MOUSE_RELEASED)
{
/*鼠标释放时,终止计时线程,并重绘窗口表面上的内容*/
bStart = false;
repaint();
}
super.processMouseEvent(e);
}
public void paint(Graphics g)
{
/*时间值的小时、分钟、秒、都用两位数字显示,
不足两位的部分前面加0,即"HH:mm:ss"这种的格式。*/
SimpleDateFormat sdf= new SimpleDateFormat("HH:mm:ss");
/*最刚开始编写这个程序的时候,直接使用elapsedTime.setTime(endTime-startTime);
语句设置elapsedTime时间对象的数字值,从运行结果上发现,即使endTime-startTime等于0,
但elapsedTime显示的时间却不是"00:00:00",而是"08:00:00"。我们曾经讲过,时间在计算机
内存中也是用一个长整数表示的,在这里,我们又发现,即使这个内存中的长整数等于0时,由于
Date类考虑了本地时区问题,所以,其表示的时间就不一定为"零点:零分:零秒"。这里不需要
考虑时区问题,只是借助Date类来帮我们生成"HH:mm:ss"这种时间表示格式。明白这个问题后,
我们就不难想像出,可以先求出显示时间为"00:00:00"的时间对象在内存中对应的那个长整数,
然后在这个基础上加上计时器所记下的时间值,最后就可以显示出我们想要的结果。*/
Date elapsedTime =null;
try
{
elapsedTime= sdf.parse("00:00:00");
}catch(Exception e){}
elapsedTime.setTime(endTime - startTime +
elapsedTime.getTime());
String display = sdf.format(elapsedTime);
g.drawRect(0,0,78,28);
g.fill3DRect(2,2,75,25,true);
g.setColor(Color.WHITE);
g.drawString(display,10,20);
}
public void run()
{
while(bStart)
{
try
{
Thread.sleep(500);
}catch(Exception e){e.printStackTrace();}
endTime = System.currentTimeMillis();
repaint();
}
}
}
public class TestStopWatch
{
public static void main(String [] args)
{
Frame f =new Frame("StopWatch");
f.add(new StopWatch());
f.setSize(200,200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
《Java就业培训教程》P319源码
程序清单:TestCheckbox.java
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -