JAVA应用小程序.docx
- 文档编号:30671916
- 上传时间:2023-08-19
- 格式:DOCX
- 页数:14
- 大小:16.65KB
JAVA应用小程序.docx
《JAVA应用小程序.docx》由会员分享,可在线阅读,更多相关《JAVA应用小程序.docx(14页珍藏版)》请在冰豆网上搜索。
JAVA应用小程序
1、TestBirthday:
publicclassTestBirthday{
publicstaticvoidmain(Stringargs[]){
Birthdayobj1=newBirthday();
obj1.show();
Birthdayobj2=newBirthday(1990,11,12);
obj2.show();
}
}
classBirthday{
intyear,month,day;//分别表示年月日
Birthday(){
}
Birthday(intyear,intmonth,intday){
this.year=year;
this.month=month;
this.day=day;
}
voidshow(){
System.out.println("出生年月:
"+year+"年"+month+"月"+day+"日");
}
}
2、TestFrame:
importjava.awt.*;
importjavax.swing.*;
publicclassTestFrameextendsJFrame{
JTextAreatext;
publicTestFrame(Strings){
super(s);
ContainercontentPane=getContentPane();
contentPane.setLayout(newBorderLayout());
text=newJTextArea("这是一个文本区,用来输入文本。
");
contentPane.add(text,BorderLayout.CENTER);
Toolkittk=getToolkit();
Imageimg=tk.getImage("picl\\a.jpg");
setIconImage(img);
setSize(400,300);
setVisible(true);
}
publicstaticvoidmain(String[]args){
TestFramew=newTestFrame("我的窗口应用程序");
}
}
3、二维数组:
publicclassTwoDimensionArray{
publicstaticvoidmain(Stringargs[]){
intb[][]={{1,2,3},{4,5,6,7,8},{9,10,11,12,13,14,15}};
for(inti=0;i System.out.print("第"+(i+1)+"行的所有元素: "); for(intj=0;j System.out.print(b[i][j]+"\t"); } System.out.println();// } intsum=0; for(inti=0;i for(intj=0;j sum+=b[i][j]; } } System.out.println("二维数组所有元素的和sum="+sum); } } 4、棋盘: importjava.awt.Color; importjava.awt.Graphics; importjava.applet.Applet; publicclassApp2extendsApplet{ publicvoidpaint(Graphicsg){ g.setColor(Color.black); g.drawLine(20,20,100,20); g.drawLine(20,40,100,40); g.drawLine(20,60,100,60); g.drawLine(20,80,100,80); g.drawLine(20,100,100,100); g.drawLine(20,20,20,100); g.drawLine(40,20,40,100); g.drawLine(60,20,60,100); g.drawLine(80,20,80,100); g.drawLine(100,20,100,100); g.fillOval(35,35,10,10); g.fillOval(55,35,10,10); g.fillOval(75,35,10,10); g.fillOval(35,55,10,10); g.fillOval(55,55,10,10); g.fillOval(75,55,10,10); g.fillOval(35,75,10,10); g.fillOval(55,75,10,10); g.fillOval(75,75,10,10); } } 5、画太阳: importjava.awt.Color; importjava.awt.Graphics; importjava.applet.Applet; publicclassApp3extendsApplet{ intx,y; publicvoidinit() { x=300; y=550; } publicvoidpaint(Graphicsg) { for(y=550;y>=50;y=y-30) { g.setColor(Color.red); g.fillOval(x,y,50,50); try{Thread.currentThread().sleep(100);// }catch(Exceptione){} g.setColor(Color.white); g.fillOval(x,y,50,50); } g.setColor(Color.red); g.fillOval(x,y,50,50); } } 6、回文数: publicclassPalindromeNum{ publicstaticvoidmain(Stringargs[]){ intx=1881; inta,b,c,d; d=x/1000; c=(x-d*1000)/100; b=(x-d*1000-c*100)/10; a=x%10; if(a==d&&b==c) System.out.println(x+"是回文数"); else System.out.println(x+"不是回文数"); } } 7、时钟: importjava.awt.Color; importjava.applet.Applet; importjava.text.*; importjava.awt.Graphics; importjava.util.*; publicclassClockAppletextendsAppletimplementsRunnable{ privateThreadathread; privateSimpleDateFormatsdateformat; privateStringshowdate; privateintsecond_lastx=0,second_lasty=0; privateintminute_lastx=0,minute_lasty=0; privateinthour_lastx=0,hour_lasty=0; privateintcenter_x=300,center_y=300,r=100; privateintx0=300,y0=300; privateintsecond_r=80,minute_r=60,hour_r=40; privateintxPos,yPos; privatedoublealfa; publicvoidinit(){ this.setBackground(Color.white); this.athread=null; } publicvoidpaint(Graphicsg){ Calendarrightnow=Calendar.getInstance(); intsecond=rightnow.get(Calendar.SECOND); intminute=rightnow.get(Calendar.MINUTE); inthour=rightnow.get(Calendar.HOUR); g.drawOval(200,200,200,200); r=90; alfa=Math.toRadians(90); xPos=x0+(int)(r*Math.cos(alfa)); yPos=y0-(int)(r*Math.sin(alfa)); g.drawString("12",xPos-5,yPos+5); alfa=Math.toRadians(180); xPos=x0+(int)(r*Math.cos(alfa)); yPos=y0-(int)(r*Math.sin(alfa)); g.drawString("9",xPos-5,yPos+5); alfa=Math.toRadians(270); xPos=x0+(int)(r*Math.cos(alfa)); yPos=y0-(int)(r*Math.sin(alfa)); g.drawString("6",xPos-5,yPos+5); alfa=Math.toRadians(0); xPos=x0+(int)(r*Math.cos(alfa)); yPos=y0-(int)(r*Math.sin(alfa)); g.drawString("3",xPos-5,yPos+5); alfa=Math.toRadians(60); xPos=x0+(int)(r*Math.cos(alfa)); yPos=y0-(int)(r*Math.sin(alfa)); g.drawString("1",xPos-5,yPos+5); alfa=Math.toRadians(30); xPos=x0+(int)(r*Math.cos(alfa)); yPos=y0-(int)(r*Math.sin(alfa)); g.drawString("2",xPos-5,yPos+5); alfa=Math.toRadians(120); xPos=x0+(int)(r*Math.cos(alfa)); yPos=y0-(int)(r*Math.sin(alfa)); g.drawString("11",xPos-5,yPos+5); alfa=Math.toRadians(150); xPos=x0+(int)(r*Math.cos(alfa)); yPos=y0-(int)(r*Math.sin(alfa)); g.drawString("10",xPos-5,yPos+5); alfa=Math.toRadians(210); xPos=x0+(int)(r*Math.cos(alfa)); yPos=y0-(int)(r*Math.sin(alfa)); g.drawString("8",xPos-5,yPos+5); alfa=Math.toRadians(240); xPos=x0+(int)(r*Math.cos(alfa)); yPos=y0-(int)(r*Math.sin(alfa)); g.drawString("7",xPos-5,yPos+5); alfa=Math.toRadians(300); xPos=x0+(int)(r*Math.cos(alfa)); yPos=y0-(int)(r*Math.sin(alfa)); g.drawString("5",xPos-5,yPos+5); alfa=Math.toRadians(330); xPos=x0+(int)(r*Math.cos(alfa)); yPos=y0-(int)(r*Math.sin(alfa)); g.drawString("4",xPos-5,yPos+5); alfa=Math.toRadians(360-(second*6-90)); intsecond_nowx=center_x+(int)(second_r*Math.cos(alfa)); intsecond_nowy=center_y-(int)(second_r*Math.sin(alfa)); alfa=Math.toRadians(360-(hour*30-90)); inthour_nowx=center_x+(int)(hour_r*Math.cos(alfa)); inthour_nowy=center_y-(int)(hour_r*Math.sin(alfa)); alfa=Math.toRadians(360-(minute*6-90)); intminute_nowx=center_x+(int)(minute_r*Math.cos(alfa)); intminute_nowy=center_y-(int)(minute_r*Math.sin(alfa)); g.setColor(this.getBackground()); if(second_nowx! =second_lastx||second_nowy! =second_lasty) { g.drawLine(center_x,center_y,second_lastx,second_lasty); } if(minute_nowx! =minute_lastx||minute_nowy! =minute_lasty){ g.drawLine(center_x,center_y,minute_lastx,minute_lasty); } if(hour_nowx! =hour_lastx||hour_nowy! =hour_lasty){ g.drawLine(center_x,center_y,hour_lastx,hour_lasty); } g.setColor(Color.red); g.drawLine(center_x,center_y,second_nowx,second_nowy); g.setColor(Color.black); g.drawLine(center_x,center_y,minute_nowx,minute_nowy); g.setColor(Color.blue); g.drawLine(center_x,center_y,hour_nowx,hour_nowy); second_lastx=second_nowx; second_lasty=second_nowy; minute_lastx=minute_nowx; minute_lasty=minute_nowy; hour_lastx=hour_nowx; hour_lasty=hour_nowy; } publicvoidstart(){ if(athread==null){ athread=newThread(this); athread.start(); } } publicvoidstop(){ if(athread! =null){ athread.interrupt(); athread=null; } } publicvoidrun(){ while(athread! =null){ repaint(); try{ athread.sleep(1000); } catch(InterruptedExceptione){} } } } 8、太阳上升: importjava.applet.Applet; importjava.awt.Graphics; importjava.awt.Color; importjava.awt.event.KeyEvent; importjava.awt.event.KeyListener; publicclassTestKeyListenerextendsAppletimplementsKeyListener{ intx,y; intcontrol=0; publicvoidpaint(Graphicsg){ if(control==0) {g.setColor(Color.red); g.fillOval(x,y,50,50); } if(control==2) { for(y=550;y>=50;y=y-30) { g.setColor(Color.red); g.fillOval(x,y,50,50); try{Thread.currentThread().sleep(100); }catch(Exceptione){} g.setColor(Color.white); g.fillOval(x,y,50,50); } g.setColor(Color.red); g.fillOval(x,y,50,50); } } publicvoidinit() {x=300; y=550; addKeyListener(this); } publicvoidkeyTyped(KeyEvente){ if(e.getKeyChar()=='r'||e.getKeyChar()=='R') control=2;repaint(); } publicvoidkeyReleased(KeyEvente){} publicvoidkeyPressed(KeyEvente){} } 9、LeapYear: importjava.io.*; publicclassLeapYear{ publicstaticvoidmain(Stringargs[])throwsIOException{ BufferedReaderbuf; buf=newBufferedReader(newInputStreamReader(System.in)); Stringstr; intyear; System.out.println("输入一个年份,回车后判断是否为闰年。 "); System.out.println("输入quit,回车后退出程序运行。 "); while(true){ str=buf.readLine(); if(str.equals("quit")) break; year=Integer.parseInt(str); if(isLeapYear(year)) System.out.println(year+"年是闰年。 "); else System.out.println(year+"不是闰年。 "); } } staticbooleanisLeapYear(intyear){ if((year%4==0)&&(year%100! =0)||(year%400==0)) returntrue; else returnfalse; } }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JAVA 应用 程序
![提示](https://static.bdocx.com/images/bang_tan.gif)