Java2实用教程实验指导模版代码.docx

上传人:b****5 文档编号:29745224 上传时间:2023-07-26 格式:DOCX 页数:35 大小:353.20KB
下载 相关 举报
Java2实用教程实验指导模版代码.docx_第1页
第1页 / 共35页
Java2实用教程实验指导模版代码.docx_第2页
第2页 / 共35页
Java2实用教程实验指导模版代码.docx_第3页
第3页 / 共35页
Java2实用教程实验指导模版代码.docx_第4页
第4页 / 共35页
Java2实用教程实验指导模版代码.docx_第5页
第5页 / 共35页
点击查看更多>>
下载资源
资源描述

Java2实用教程实验指导模版代码.docx

《Java2实用教程实验指导模版代码.docx》由会员分享,可在线阅读,更多相关《Java2实用教程实验指导模版代码.docx(35页珍藏版)》请在冰豆网上搜索。

Java2实用教程实验指导模版代码.docx

Java2实用教程实验指导模版代码

 

Java-2实用教程实验指导模版代码

上机实践1初识Java

实验1一个简单的应用程序

程序模板

publicclassHello

{

publicstaticvoidmain(Stringargs[])

{

【代码1】;

import.*;

publicclassFirstAppletextendsApplet

{

publicvoidpaint(Graphicsg)

{

;

【代码1】;

classSunRise

{

publicstaticvoidmain(Stringargs[])

{

SquareEquationequation=newSquareEquation(4,5,1);

();

(-3,4,5);

();

}

}

上机实践4继承与接口

实验1继承

程序模板

classPeople

{

protecteddoubleweight,height;

publicvoidspeakHello()

{

"yayawawa");

}

publicvoidaverageHeight()

{

height=173;

"averageheight:

"+height);

}

publicvoidaverageWeight()

{

weight=70;

"averageweight:

"+weight);

}

}

classChinaPeopleextendsPeople

{

【代码1】;

importDateExample

{

publicstaticvoidmain(Stringargs[])

{

Stringstr=("输入第一个日期的年份:

");

intyearOne=(str);

str=("输入该年的月份:

");

intmonthOne=(str);

str=("输入该月份的日期:

");

intdayOne=(str);

str=("输入第二个日期的年份:

");

intyearTwo=(str);

str=("输入该年的月份:

");

intmonthTwo=(str);

str=("输入该月份的日期:

");

intdayTwo=(str);

Calendarcalendar=【代码1】;

classBigIntegerExample

{

publicstaticvoidmain(Stringargs[])

{

BigIntegern1=newBigInteger("654321"),

n2=newBigInteger("456789"),

result=null;

"n1的二进制表示:

"+

(2));

"n2的八进制表示:

"+(8));

result=【代码1】;

import.*;

publicclassGuessExampleextendsAppletimplementsActionListener

{

intnumber;

TextFieldinputNumber;

LabelfeedBack;

ButtonnuttonGetNumber,buttonEnter;

publicvoidinit()

{

nuttonGetNumber=newButton("得到一个随机数");

feedBack=newLabel("无反馈信息",;

;

inputNumber=newTextField("0",5);

buttonEnter=newButton("确定");

【代码1】;

import.*;

importclassSignalExampleextendsAppletimplementsActionListener

{

ButtonbuttonRed,buttonGreen,buttonYellow;

SignalCanvassignal;

publicvoidinit()

{

buttonRed=【代码1】;

import.*;

import.*;

publicclassBoyextendsAppletimplementsActionListener

{

Labela[]=newLabel[42];

ButtontitleName[]=newButton[7];

Stringname[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};

ButtonnextMonth,previousMonth;

intyear=2005,month=1;

CalendarBeancalendar;

LabelshowMessage=newLabel("",;

publicvoidinit()

{

【代码1】etText(day[i]);

}

nextMonth=newButton("下月");

previousMonth=newButton("上月");

(this);

(this);

PanelpNorth=newPanel(),

pSouth=newPanel();

(previousMonth);

(nextMonth);

(showMessage);

("日历:

"+()+"年"+()+"月");

ScrollPanescrollPane=newScrollPane();

(pCenter);

【代码5】etText(day[i]);

}

}

elseif()==previousMonth)

{

month=month-1;

if(month<1)

month=12;

(month);

Stringday[]=();

for(inti=0;i<42;i++)

{

a[i].setText(day[i]);

}

}

("日历:

"+()+"年"+()+"月");

}

}

classCalendarBean

{

Stringday[];

intyear=2005,month=0;

publicvoidsetYear(intyear)

{

=year;

}

publicintgetYear()

{

returnyear;

}

publicvoidsetMonth(intmonth)

{

=month;

}

publicintgetMonth()

{

returnmonth;

}

publicString[]getCalendar()

{

Stringa[]=newString[42];et(year,month-1,1);

int星期几=日历.get-1;

intday=0;

if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)

{

day=31;

}

if(month==4||month==6||month==9||month==11)

{

day=30;

}

if(month==2)

{

if(((year%4==0)&&(year%100!

=0))||(year%400==0))

{

day=29;

}

else

{

day=28;

}

}

for(inti=星期几,n=1;i<星期几+day;i++)

{

a[i]=(n);

n++;

}

returna;

}

}

上机实践7图形用户界面设计

(2)

程序模板

import.*;

importMyFrameextendsFrameimplementsActionListener

{

MenuBarbar=null;

Menumenu=null;

MenuItemitemRed,itemGreen;

MyFrame(Strings)

{

super(s);

【代码1】;

importFontDialogextendsDialogimplementsItemListener,ActionListener

{

Choicelist;

JTextAreatext;

Fontfont;

Buttonyes,cancel;

Componentcom;

FontDialog(Framef,Stringname,Componentcom)

{

super(f,name);

=com;

【代码1】;

importclassMoveExample

{

publicstaticvoidmain(Stringargs[])

{

newHua_Rong_Road();

}

}

classPersonextendsButtonimplementsFocusListener

{

intnumber;

Colorc;

Person(intnumber,Strings)

{

super(s);

=number;

setFont(newFont("宋体",,14));

setBackground;

【代码1】etBounds(104,54,100,100);

person[1].setBounds(104,154,100,50);

person[2].setBounds(54,154,50,100);

person[3].setBounds(204,154,50,100);

person[4].setBounds(54,54,50,100);

person[5].setBounds(204,54,50,100);

person[6].setBounds(54,254,50,50);

person[7].setBounds(204,254,50,50);

person[8].setBounds(104,204,50,50);

person[9].setBounds(154,204,50,50);

【代码6】idth;

inth=().height;

if(y>h/2)

{

goDown(man);

}

if(y

{

goUp(man);

}

if(x

{

goLeft(man);

}

if(x>w/2)

{

goRight(man);

}

}

publicvoidmouseReleased(MouseEvente){}

publicvoidmouseEntered(MouseEvente){}

publicvoidmouseExited(MouseEvente){}

publicvoidmouseClicked(MouseEvente){}

publicvoidgoDown(Personman)

{

booleanmove=true;

RectanglemanRect=();

intx=().x;

inty=().y;

y=y+50;

(x,y);

RectanglebelowRect=();

for(inti=0;i<10;i++)

{

RectanglepersonRect=person[i].getBounds();

if((personRect))&&!

=i))

{

move=false;

}

}

if(belowRect))

{

move=false;

}

if(move==true)

{

(x,y);

}

}

publicvoidgoUp(Personman)

{

booleanmove=true;

RectanglemanRect=();

intx=().x;

inty=().y;

y=y-50;

(x,y);

RectangleaboveRect=();

for(inti=0;i<10;i++)

{

RectanglepersonRect=person[i].getBounds();

if((personRect))&&!

=i))

{

move=false;

}

}

if(aboveRect))

{

move=false;

}

if(move==true)

{

(x,y);

}

}

publicvoidgoLeft(Personman)

{

booleanmove=true;

RectanglemanRect=();

intx=().x;

inty=().y;

x=x-50;

(x,y);

RectangleleftRect=();

for(inti=0;i<10;i++)

{

RectanglepersonRect=person[i].getBounds();

if((personRect))&&!

=i))

{

move=false;

}

}

if(leftRect))

{

move=false;

}

if(move==true)

{

(x,y);

}

}

publicvoidgoRight(Personman)

{

booleanmove=true;

RectanglemanRect=();

intx=().x;

inty=().y;

x=x+50;

(x,y);

RectanglerightRect=();

for(inti=0;i<10;i++)

{

RectanglepersonRect=person[i].getBounds();

if((personRect))&&!

=i))

{

move=false;

}

}

if(rightRect))

{

move=false;

}

if(move==true)

{

(x,y);

}

}

publicvoidactionPerformed(ActionEvente)

{

removeAll();

init();

validate();

repaint();

}

}

上机实践8多线程

实验1线程的4种状态

程序模板

classTortoiseextendsThread

{

intsleepTime=0,liveLength=0;

Tortoise(intsleepTime,Stringname,intliveLength)

{

=sleepTime;

=liveLength;

setName(name);

}

publicvoidrun()

{

while(true)

{

liveLength--;

"@");

try

{

【代码1】;

importWinextendsFrameimplementsRunnable,ActionListener

{

ThreadmoveOrStop;

Buttonstart,hang,resume,die;

LabelmoveLabel;

booleanmove=false,dead=false;

Win()

{

【代码1】;

inty=().y;

y=y+2;

if(y>=200)y=10;

(x,y);

try{

(200);

}

catch(InterruptedExceptione2)

{

}

if(dead==true)

{

【代码5】;

publicclassSecretExample

{

publicstaticvoidmain(Stringargs[])

{

FilefileOne=newFile(""),

fileTwo=newFile("");

charb[]=newchar[100];

try{

FileReaderin=【代码1】;

publicclassReadExample

{

publicstaticvoidmain(Stringargs[])

{

Filefile=newFile("c:

/1000","");

FiletempFile=newFile("");

try{

FileReaderinOne=【代码1】;

publicclassRandomExample

{

publicstaticvoidmain(Stringargs[]);

import.*;

publicclassObjectExample

{

publicstaticvoidmain(Stringargs[])

{

Calendarcalendar1=();;

import.*;

import.*;

import.*;

publicclassReadFileextendsAppletimplementsActionListener,Runnable

{

Filefile;

Choicechoice;

TextAreatext;

Threadthread;

Stringitem=null;

Buttonbutton;

URLurl;

publicvoidinit()

{

choice=newChoice();

thread=newThread(this);

intN=(getParameter("总数"));

for(inti=1;i<=N;i++)

{

(getParameter(i)));

}

button=newButton("开始读取");

text=newTextArea(12,40);

(this);

add(choice);

add(button);

add(text);

}

publicvoidactionPerformed(ActionEvente)

{

(null);

item=();

item=();

if(!

()))

{

thread=newThread(this);

}

try{

();

}

catch(Exceptionexp)

{

}

}

publicvoidrun()

{

try

{

url=newURL(getCodeBase(),item);

InputStreamin=【代码1】;

import.*;

import.*;

importClientextendsFrameimplementsRunnable,ActionListener

{

Buttonconnection;

Socketsocket=null;

ObjectInputStreamin=null;

ObjectOutputStreamout=null;

Threadthread;

publicClient()

{

socket=newSocket();

connection=newButton("连接服务器,读取文本区对象");

add(connection,;

(this);

thread=newThread(this);

setBounds(100,100,360,310);

setVisible(true);

addWindowListener(newWindowAdapter()

{

publicvoidwindowClosing(WindowEvente)

{

(0);

}

}

);

}

publicvoidrun()

{

while(true)

{

try{

TextAreatext=(TextArea)();

add(text,;

validate();

}

catch(Exceptione)

{

break;

}

}

}

publicvoidactionPerformed(ActionEvente)

{

if()==connection)

{

try

{

if())

{

}

else

{

InetAddressaddress=("");

InetSocketAddresssocketAddress=【代码1】;

import.*;

import.*;

import.*;

publicclassServer

{

publicstaticvoidmain(Stringargs[])

{

ServerSocketserver=null;

ServerThreadthread;

Socketyou=null;

while(true)

{

try{

server=【代码1】tart();;

import.*;

import.*;

classImageCanvasextendsCanvas

{

Imageimage=null;

publicImageCanvas()

{

setSize(200,200);

}

publicvoidpaint(Graphicsg)

{

if(image!

=null)

(image,0,0,getSize().width,getSize().height,this);

}

publicvoidsetImage(Imageimage)

{

=image;

}

}

classClientextendsFram

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1