当前位置: 主页 > 科技导读 >申博代理网址_java小程序设计,设计一个窗口程序 >

申博代理网址_java小程序设计,设计一个窗口程序

java小程序设计,设计一个窗口程序


import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
public class FrameTest {
private JFrame jf;
private JButton yellow,blue,green;
private JPanel panel1;
public FrameTest(){
jf = new JFrame("Frame");
yellow = new JButton("yellow");
blue= new JButton("blue");
green = new JButton("green");
panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
jf.add(panel1,BorderLayout.CENTER);
jf.setVisible(true);
jf.setSize(500,600);
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
blue.setForeground(Color.blue);
panel1.add(blue);
blue.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
panel1.setBackground(Color.blue);
}
});
yellow.setForeground(Color.yellow);
panel1.add(yellow);
yellow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
panel1.setBackground(Color.yellow);
}
});
green.setForeground(Color.green);
panel1.add(green);
green.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
panel1.setBackground(Color.green);
}
});

}
public static void main(String args[]){
FrameTest ft = new FrameTest();
}
}本回答被提问者采纳先设3个按钮,在每个按钮上设置相关的颜色,这个可以看API,还要实现按钮的事件侦听器才能实现按钮的功能.在页面里写上javascript代码
常用的java小程序都有哪些?
太多了建议你去看看java的相关书籍,java编程思想等
用JAVA编写一个小程序,不限类型好玩点的。
控制台戏 class YZDEL
{
public static void main(String[] args)
{
java.util.Random Shiji = new java.util.Random();
java.util.Scanner Shuru = new java.util.Scanner(System.in);
java.util.Scanner Nandu = new java.util.Scanner(System.in);
int YingxHP = 20;
int YingxMP = 0;
int MogHP = 20;
int MogMP = 0;
int MogDo = 0;
int fangyu = 0;
System.out.println("┏━━━━╮┏━━┣┣╮ ┓  ┏━┳━┳┓  ┓━┓ ");
System.out.println("┏┣━━━━━┣┣┓   ┃  ┏ ┃ ┃┓  ┃ ╯ ");
System.out.println("┣┣━━━┫╭━━┻╯  ╮ ┃  ╰┓┃ ┃╯┏━┣━━┓");
System.out.println("┣┣━━━┫┏━━━━┓╭━━┣━┛┗━┻━┻┛  ┃┃  ");
System.out.println("┏┣━━━┓┣━━━━┫   ┃  ┃┃┗╮╮┃  ┃┣━╯");
System.out.println(" ┛  ┗╯┗━━━━╯  ┗╯  ╰╰━━┛┛┗━╯╰━┛");
System.out.println();
System.out.println("作者:天狐空幻");
System.out.println();
System.out.println("说明:勇者的操作方式为以下所示:");
System.out.println("使用攻击需消耗1MP 伤害1HP");
System.out.println("使用蓄力可增加1MP 伤害0HP");
System.out.println("使用躲闪需消耗0MP 伤害0HP躲避攻击");
System.out.println("使用重击需消耗3MP 伤害2HP防御无效");
System.out.println("恶龙攻击力高, 注意防御是取胜的关键");
System.out.println("恶龙MP达到4时可能会放出火焰无法躲避");
System.out.println("准备说明完毕,那么让我们来挑战恶龙吧!");
System.out.println("==================================");
//难度选择
System.out.println("请选择难度");
System.out.println("1.娱乐 2.挑战 3.噩梦");
int ND = Nandu.nextInt();
System.out.println("==================================");
while(true)
{
//HP,MP的显示
System.out.print("勇者:");
System.out.print(" HP ");
for(int x=YingxHP;x>0;x--) System.out.print("*");
System.out.print(" "+YingxHP);
System.out.println();
System.out.print("");
System.out.print(" MP ");
for(int x=YingxMP;x>0;x--) System.out.print("*");
System.out.print(" "+YingxMP);
System.out.println();
System.out.print("恶龙:");
System.out.print(" HP ");
for(int y=MogHP;y>0;y--) System.out.print("*");
System.out.print(" "+MogHP);
System.out.println();
System.out.print("");
System.out.print(" MP ");
for(int y=MogMP;y>0;y--) System.out.print("*");
System.out.print(" "+MogMP);
System.out.println();
System.out.println("==================================");
//胜利判定
if(YingxHP<1) {System.out.println();System.out.println("勇者HP为0! 不..不可能..我怎么会..勇者倒下了。再接再厉吧!~");System.out.println();break;}
if(MogHP<1) {System.out.println();System.out.println("恶龙HP为0! 恶龙绝望的哀鸣中倒了下去。勇者胜利了。恭喜你挑战成功!!");System.out.println();break;}

//角色输入判定
System.out.println("你要做什么:");
System.out.println("1.攻击 2.蓄力");
System.out.println("3.躲闪 4.重击");
System.out.println("____________________");
int Do = Shuru.nextInt();
//敌人输入判定
//娱乐难度
if(ND==1){
for(int i=1;i<3;i++) MogDo = Shiji.nextInt(4);}
//挑战难度
if(ND==2){
if(YingxMP==0&&MogMP==0) {MogDo = 0;}
else if(MogMP==0){for(int i=1;i<3;i++) MogDo = Shiji.nextInt(2);}
else if(YingxMP>2&&MogMP>2) MogDo = 2;
else if(MogMP<4) {for(int i=1;i<4;i++) MogDo = Shiji.nextInt(3);}
else MogDo = 3;}
//噩梦难度
if(ND==3){
if(Do==1) MogDo=1;
if(Do==2&&MogMP>0) MogDo=2;
if(Do==2&&MogMP==0) MogDo=0;
if(Do==3&&MogMP<4) MogDo=0;
if(Do==3&&MogMP>=4) MogDo=3;}//变态判定。。
//战斗分析
//防御
if(Do==3) {fangyu=1;System.out.println("你灵巧的躲避攻击!");}
if(MogDo==1) {fangyu=1;System.out.println("恶龙进行防御!");}
//角色判定
if(Do==1&&YingxMP==0) {System.out.println("MP不足!");}
if(Do==1&&YingxMP>0) {
if(fangyu==0) {MogHP=MogHP-1;YingxMP=YingxMP-1;System.out.println("你发动攻击!");}
if(fangyu==1) {YingxMP=YingxMP-1;System.out.println("你的攻击被格挡!");}}
if(Do==2) {YingxMP=YingxMP+1;System.out.println("你进行蓄力!");}
if(Do==4&&YingxMP<3) {System.out.println("MP不足!");}
if(Do==4&&YingxMP>2) {MogHP=MogHP-2;YingxMP=YingxMP-3;System.out.println("你发动重击!");}
if(Do> 4) System.out.println("你不知所措...");
//敌人判定
if(MogDo==2&&MogMP==0) {System.out.println("恶龙在发呆!");}
if(MogDo==2&&MogMP>0) {
if(fangyu==0) {YingxHP=YingxHP-2;MogMP=MogMP-1;System.out.println("恶龙发动攻击!");}
if(fangyu==1) {MogMP=MogMP-1;System.out.println("恶龙的攻击被躲开了!");}}
if(MogDo==0) {MogMP=MogMP+1;System.out.println("恶龙进行蓄力!");}
if(MogDo==3&&MogMP<4) {System.out.println("恶龙在发呆!");}
if(MogDo==3&&MogMP>3) {YingxHP=YingxHP-4;MogMP=MogMP-4;System.out.println("恶龙发动火焰吐吸!躲避不能!");}
//结束
fangyu = 0;
MogDo = 0;
System.out.println("____________________");
System.out.println();
System.out.println("==================================");}
}
}控制台小游 class YZDEL
{
public static void main(String[] args)
{
java.util.Random Shiji = new java.util.Random();
java.util.Scanner Shuru = new java.util.Scanner(System.in);
java.util.Scanner Nandu = new java.util.Scanner(System.in);
int YingxHP = 20;
int YingxMP = 0;
int MogHP = 20;
int MogMP = 0;
int MogDo = 0;
int fangyu = 0;
System.out.println("┏━━╮┏━━┣┣┓ ┓ ━┳━┳┓  ┓━┓ ");
System.out.println("┏┣━━━┓┏━━┣┣┓   ┃  ┏ ┃ ┃┓  ┃ ╯ ");
System.out.println("┣┣━━━┫╭━━┻╯  ╮ ┃  ╰┓┃ ┃╯┏━┣━━┓");
System.out.println("┣┣━━━┫┏━━━━┓╭━━┣━┛┗━┻━┻┛  ┃┃  ");
System.out.println("┏┣━━━┓┣━━━━┫   ┃  ┃┃┗╮╮┃  ┃┣━╯");
System.out.println(" ┛  ┗╯┗━━━━╯  ┗╯  ╰╰━━┛┛┗━╯╰━┛");
System.out.println();
System.out.println("作者:天狐空幻");
System.out.println();
System.out.println("说明:勇者的操作方式为以下所示:");
System.out.println("使用攻击需消耗1MP 伤害1HP");
System.out.println("使用蓄力可增加1MP 伤害0HP");
System.out.println("使用躲闪需消耗0MP 伤害0HP躲避攻击");
System.out.println("使用重击需消耗3MP 伤害2HP防御无效");
System.out.println("恶龙攻击力高, 注意防御是取胜的关键");
System.out.println("恶龙MP达到4时可能会放出火焰无法躲避");
System.out.println("准备说明完毕,那么让我们来挑战恶龙吧!");
System.out.println("==================================");
//难度选择
System.out.println("请选择难度");
System.out.println("1.娱乐 2.挑战 3.噩梦");
int ND = Nandu.nextInt();
System.out.println("==================================");
while(true)
{
//HP,MP的显示
System.out.print("勇者:");
System.out.print(" HP ");
for(int x=YingxHP;x>0;x--) System.out.print("*");
System.out.print(" "+YingxHP);
System.out.println();
System.out.print("");
System.out.print(" MP ");
for(int x=YingxMP;x>0;x--) System.out.print("*");
System.out.print(" "+YingxMP);
System.out.println();
System.out.print("恶龙:");
System.out.print(" HP ");
for(int y=MogHP;y>0;y--) System.out.print("*");
System.out.print(" "+MogHP);
System.out.println();
System.out.print("");
System.out.print(" MP ");
for(int y=MogMP;y>0;y--) System.out.print("*");
System.out.print(" "+MogMP);
System.out.println();
System.out.println("==================================");
//胜利判定
if(YingxHP<1) {System.out.println();System.out.println("勇者HP为0! 不..不可能..我怎么会..勇者倒下了。再接再厉吧!~");System.out.println();break;}
if(MogHP<1) {System.out.println();System.out.println("恶龙HP为0! 恶龙绝望的哀鸣中倒了下去。勇者胜利了。恭喜你挑战成功!!");System.out.println();break;}

//角色输入判定
System.out.println("你要做什么:");
System.out.println("1.攻击 2.蓄力");
System.out.println("3.躲闪 4.重击");
System.out.println("____________________");
int Do = Shuru.nextInt();
//敌人输入判定
//娱乐难度
if(ND==1){
for(int i=1;i<3;i++) MogDo = Shiji.nextInt(4);}
//挑战难度
if(ND==2){
if(YingxMP==0&&MogMP==0) {MogDo = 0;}
else if(MogMP==0){for(int i=1;i<3;i++) MogDo = Shiji.nextInt(2);}
else if(YingxMP>2&&MogMP>2) MogDo = 2;
else if(MogMP<4) {for(int i=1;i<4;i++) MogDo = Shiji.nextInt(3);}
else MogDo = 3;}
//噩梦难度
if(ND==3){
if(Do==1) MogDo=1;
if(Do==2&&MogMP>0) MogDo=2;
if(Do==2&&MogMP==0) MogDo=0;
if(Do==3&&MogMP<4) MogDo=0;
if(Do==3&&MogMP>=4) MogDo=3;}//变态判定。。
//战斗分析
//防御
if(Do==3) {fangyu=1;System.out.println("你灵巧的躲避攻击!");}
if(MogDo==1) {fangyu=1;System.out.println("恶龙进行防御!");}
//角色判定
if(Do==1&&YingxMP==0) {System.out.println("MP不足!");}
if(Do==1&&YingxMP>0) {
if(fangyu==0) {MogHP=MogHP-1;YingxMP=YingxMP-1;System.out.println("你发动攻击!");}
if(fangyu==1) {YingxMP=YingxMP-1;System.out.println("你的攻击被格挡!");}}
if(Do==2) {YingxMP=YingxMP+1;System.out.println("你进行蓄力!");}
if(Do==4&&YingxMP<3) {System.out.println("MP不足!");}
if(Do==4&&YingxMP>2) {MogHP=MogHP-2;YingxMP=YingxMP-3;System.out.println("你发动重击!");}
if(Do> 4) System.out.println("你不知所措...");
//敌人判定
if(MogDo==2&&MogMP==0) {System.out.println("恶龙在发呆!");}
if(MogDo==2&&MogMP>0) {
if(fangyu==0) {YingxHP=YingxHP-2;MogMP=MogMP-1;System.out.println("恶龙发动攻击!");}
if(fangyu==1) {MogMP=MogMP-1;System.out.println("恶龙的攻击被躲开了!");}}
if(MogDo==0) {MogMP=MogMP+1;System.out.println("恶龙进行蓄力!");}
if(MogDo==3&&MogMP<4) {System.out.println("恶龙在发呆!");}
if(MogDo==3&&MogMP>3) {YingxHP=YingxHP-4;MogMP=MogMP-4;System.out.println("恶龙发动火焰吐吸!躲避不能!");}
//结束
fangyu = 0;
MogDo = 0;
System.out.println("____________________");
System.out.println();
System.out.println("==================================");}
}
}
求一个JAVA小程序
图形界面,自续

第1个以这
public class App_555 {class Person{public String name;public Person (String name){this.name=name;System.out.println("the Person of Java");}}class Student extends Person{int age;public Student(String name,int age){super(name);this.age=age;}public void show(){System.out.println("the Student of person");}}public static void main(String[] args) {new App_555().test();}public void test() {Person per=new Person(" m lz");Student stu=new Student("m l z", 14);stu.show();}}

//ClockAnimation.java
packagep1;
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
publicclassClockAnimationextendsJFrame
{
privateStillClockclock=newStillClock();
publicClockAnimation()
{
add(clock);
Timertimer=newTimer(1000,newTimerListener());
timer.start();
}
privateclassTimerListenerimplementsActionListener
{
publicvoidactionPerformed(ActionEvente)
{
clock.setCurrentTime();
clock.repaint();
}
}
publicstaticvoidmain(String[]args)
{
JFrameframe=newClockAnimation();
frame.setTitle("DisplayClock");
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

//StillClock.java
packagep1;
importjava.util.*;
importjavax.swing.*;
importjava.awt.*;
publicclassStillClockextendsJPanel
{
privateinthour;
privateintminute;
privateintsecond;
publicStillClock()
{
setCurrentTime();
}
publicStillClock(inthour,intminute,intsecond)
{
this.hour=hour;
this.minute=minute;
this.second=second;
}
publicintgetHour()
{
returnhour;
}
publicvoidsetHour(inthour)
{
this.hour=hour;
repaint();
}
publicintgetMinute()
{
returnminute;
}
publicvoidsetMinute(intminute)
{
this.minute=minute;
repaint();
}
publicintgetSecond()
{
returnsecond;
}
publicvoidsetSecond(intsecond)
{
this.second=second;
repaint();
}
protectedvoidpaintComponent(Graphicsg)
{
super.paintComponent(g);
intclockRadius=(int)(Math.min(getWidth(),getHeight())*0.3);
intxCenter=getWidth()/2;
intyCenter=getHeight()/2;
g.setColor(Color.black);
g.drawRect(20,20,getWidth()-70,getHeight()-70);
g.setColor(Color.BLACK);
g.drawOval(xCenter-clockRadius,yCenter-clockRadius,2*clockRadius,2*clockRadius);
g.drawString("12",xCenter-5,yCenter-clockRadius+12);
g.drawString("9",xCenter-clockRadius+3,yCenter+5);
g.drawString("3",xCenter+clockRadius-10,yCenter+3);
g.drawString("6",xCenter-3,yCenter+clockRadius-3);
intsLength=(int)(clockRadius*0.8);
intxSecond=(int)(xCenter+sLength*Math.sin(second*(2*Math.PI/60)));
intySecond=(int)(yCenter-sLength*Math.cos(second*(2*Math.PI/60)));
g.setColor(Color.red);
g.drawLine(xCenter,yCenter,xSecond,ySecond);
intmLength=(int)(clockRadius*0.65);
intxMinute=(int)(xCenter+mLength*Math.sin(minute*(2*Math.PI/60)));
intyMinute=(int)(yCenter-mLength*Math.cos(minute*(2*Math.PI/60)));
g.setColor(Color.blue);
g.drawLine(xCenter,yCenter,xMinute,yMinute);
inthLength=(int)(clockRadius*0.5);
intxHour=(int)(xCenter+hLength*Math.sin((hour%12+minute/60.0)*(2*Math.PI/12)));
intyHour=(int)(yCenter-hLength*Math.cos((hour%12+minute/60.0)*(2*Math.PI/12)));
g.setColor(Color.green);
g.drawLine(xCenter,yCenter,xHour,yHour);
g.setColor(Color.black);
g.drawRect(20,getHeight()-35,getWidth()-70,30);
g.drawString("Hour",60,getHeight()-15);
g.setColor(Color.white);
g.fillRect(90,getHeight()-25,30,15);
g.setColor(Color.black);
g.drawString(""+hour,95,getHeight()-15);
g.drawString("Minute",135,getHeight()-15);
g.setColor(Color.white);
g.fillRect(180,getHeight()-25,30,15);
g.setColor(Color.black);
g.drawString(""+minute,190,getHeight()-15);
g.drawString("Second",230,getHeight()-15);
g.setColor(Color.white);
g.fillRect(280,getHeight()-25,30,15);
g.setColor(Color.black);
g.drawString(""+second,290,getHeight()-15);

}
publicvoidsetCurrentTime()
{
Calendarcalendar=newGregorianCalendar();
this.hour=calendar.get(Calendar.HOUR_OF_DAY);
this.minute=calendar.get(Calendar.MINUTE);
this.second=calendar.get(Calendar.SECOND);
}
publicDimensiongetPreferredSize()
{
returnnewDimension(200,200);
}
}//这是我前段时间写的一个时钟动态显示的程序!不懂的地方就直接追问吧!


上一篇:
下一篇:

中国科学技术馆|科技馆|中国科技馆
科技内涵|科技企业|科技园区|科技导读|网站地图