帮你做了一个,不知是否满意呢?
import java.applet.Applet
import java.awt.Button
import java.awt.Color
import java.awt.Graphics
import java.awt.TextField
import java.awt.event.ActionEvent
import java.util.Random
import java.util.Vector
public class Lottery extends Applet {
private static final long serialVersionUID = 1L
int w,h
Button ok,out,setup
String[] msg
TextField[] gaiLv
TextField[] jiangPin
int mx,ml
int maxNum
Random ran
Vector<Integer> fist
Vector<Integer> sec
Vector<Integer> third
Vector<Integer> lucky
boolean lot=false
boolean iserr=false
boolean issetup=false
String mesg="输入错误"
String priseMsg="继续努力!"
public void init(){
w=400
h=220
mx=20
ml=40
ran=new Random()
this.setSize(w, h)
this.setLayout(null)
ok=new Button("抽奖")
out=new Button("退出")
setup=new Button("确认设置")
msg=new String[4]
msg[0]="一等奖"
msg[1]="二等奖"
msg[2]="三等奖"
msg[3]="幸运奖"
gaiLv=new TextField[4]
jiangPin=new TextField[4]
for(int i=0i<4i++){
gaiLv[i]=new TextField("0.0"+(i+1))
this.add(gaiLv[i])
gaiLv[i].setBounds(mx+ml, 75+i*26, 60, 18)
jiangPin[i]=new TextField()
this.add(jiangPin[i])
jiangPin[i].setBounds(mx+ml*3, 75+i*26, 80, 18)
}
this.add(ok)
ok.setBounds(260, 180, 60, 28)
ok.addActionListener(new LotButtonAction(this))
this.add(out)
out.setBounds(330, 180, 60, 28)
out.addActionListener(new LotButtonAction(this))
this.add(setup)
setup.setBounds(110, 180, 80, 24)
setup.addActionListener(new LotButtonAction(this))
}
public void paint(Graphics g){
g.setColor(Color.white)
g.fillRect(0, 0, this.getWidth(), this.getHeight())
g.setColor(new Color(230,255,230))
g.fillRect(0, 0, w, 30)
g.setColor(Color.BLUE)
g.drawString("JAVA抽奖系统", 130, 20)
g.setColor(Color.ORANGE)
g.drawRect(10, 40, 230, 170)
g.setColor(Color.BLACK)
g.drawString("设置", mx, 60)
g.drawString("概率", mx+ml, 60)
g.drawString("奖品", mx+ml*3, 60)
for(int i=0i<msg.lengthi++){
g.setColor(new Color(255-(i*30),45,89))
g.drawString(msg[i], 20, 90+i*26)
}
if(lot==true){
g.setColor(new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255)))
if(priseMsg.length()<=7){
g.drawString(priseMsg, 260, 100)
}else{
g.drawString(priseMsg.substring(0, 5), 260, 100)
g.drawString(priseMsg.substring(5), 260, 120)
}
}
if(iserr==true){
g.drawString(mesg, 260, 100)
}
}
public void getLucky(){
float firu=1
float secu=1
float thiu=1
float fouu=1
float minu=1
if(gaiLv[0].getText().trim().length()>1){
firu=Float.parseFloat(gaiLv[0].getText())
if(firu<=0||firu>=1){
iserr=true
return
}
if(firu<minu){
minu=firu
}
}
if(gaiLv[1].getText().trim().length()>1){
secu=Float.parseFloat(gaiLv[1].getText())
if(secu<=0||secu>=1){
iserr=true
return
}
if(secu<minu){
minu=secu
}
}
if(gaiLv[2].getText().trim().length()>1){
thiu=Float.parseFloat(gaiLv[2].getText())
if(thiu<=0||thiu>=1){
iserr=true
return
}
if(thiu<minu){
minu=thiu
}
}
if(gaiLv[3].getText().trim().length()>1){
fouu=Float.parseFloat(gaiLv[3].getText())
if(fouu<=0||fouu>=1){
iserr=true
return
}
if(fouu<minu){
minu=fouu
}
}
if(minu>=1||minu<=0){
iserr=true
return
}
float aNum=1/minu
maxNum=(int)aNum
int count=(int)(firu/minu)
if(firu!=1){
fist=getLotteryVec(maxNum,count)
}else{
fist.removeAllElements()
}
count=(int)(secu/minu)
if(secu!=1){
sec=getLotteryVec(maxNum,count)
}else{
sec.removeAllElements()
}
count=(int)(thiu/minu)
if(thiu!=1){
third=getLotteryVec(maxNum,count)
}else{
third.removeAllElements()
}
count=(int)(fouu/minu)
if(fouu!=1){
lucky=getLotteryVec(maxNum,count)
}else{
lucky.removeAllElements()
}
issetup=true
iserr=false
}
protected Vector<Integer> getLotteryVec(int maxNum,int num){
Vector<Integer> result=new Vector<Integer>()
for(int i=0i<numi++){
result.add(ran.nextInt(maxNum))
}
return result
}
protected int getaNum(){
return ran.nextInt(maxNum)
}
public int isLucky(int pNum){
for(int i=0i<fist.size()i++){
if(fist.get(i)==pNum){
return 1
}
}
for(int i=0i<sec.size()i++){
if(sec.get(i)==pNum){
return 2
}
}
for(int i=0i<third.size()i++){
if(third.get(i)==pNum){
return 3
}
}
for(int i=0i<lucky.size()i++){
if(lucky.get(i)==pNum){
return 4
}
}
return -1
}
public void ButtonActionPerformed(ActionEvent e){
String acName=e.getActionCommand()
if(acName.equals("抽奖")){
if(issetup==false){
priseMsg="请先设置参数!"
lot=true
repaint()
lot=false
return
}
lot=true
priseMsg=getResult(getaNum())
repaint()
// lot=false
}else if(acName.equals("退出")){
this.setVisible(false)
this.stop()
this.destroy()
System.exit(0)
}else if(acName.equals("确认设置")){
lot=false
getLucky()
repaint()
}
}
public String getResult(int num){
int resu=isLucky(num)
String result=""
switch(resu){
case -1:
result="继续努力!"
break
case 1:
result="恭喜你!一等奖!\n获得"+jiangPin[0].getText()
break
case 2:
result="恭喜你!二等奖!\n获得"+jiangPin[1].getText()
break
case 3:
result="恭喜你!三等奖!\n获得"+jiangPin[2].getText()
break
case 4:
result="恭喜你!幸运奖!\n获得"+jiangPin[3].getText()
break
default:
result="继续努力!"
}
return result+num
}
}
class LotButtonAction implements java.awt.event.ActionListener{
Lottery su
public LotButtonAction(Lottery bun){
this.su=bun
}
@Override
public void actionPerformed(ActionEvent e) {
su.ButtonActionPerformed(e)
}
}
控制台小游戏 class YZDEL\r\n{\r\n public static void main(String[] args)\r\n {\r\njava.util.Random Shiji = new java.util.Random()\r\n java.util.Scanner Shuru = new java.util.Scanner(System.in)\r\n java.util.Scanner Nandu = new java.util.Scanner(System.in)\r\n int YingxHP = 20\r\n int YingxMP = 0\r\n int MogHP = 20\r\n int MogMP = 0\r\n int MogDo = 0\r\n int fangyu = 0\r\n System.out.println("┏━━━━_┏━━┣┣┓ _ ┓┏━┳━┳┓┓━┓ ")\r\n System.out.println("┏┣━━━┓┏━━┣┣┓ ┃┏ ┃ ┃┓┃ _ ")\r\n System.out.println("┣┣━━━┫_━━┻__ ┃_┓┃ ┃_┏━┣━━┓")\r\n System.out.println("┣┣━━━┫┏━━━━┓_━━┣━┛┗━┻━┻┛┃┃")\r\n System.out.println("┏┣━━━┓┣━━━━┫ ┃┃┃┗__┃┃┣━_")\r\n System.out.println(" ┛┗_┗━━━━_┗___━━┛┛┗━__━┛")\r\n System.out.println()\r\n System.out.println(" 作者:天狐空幻")\r\n System.out.println()\r\n System.out.println("说明:勇者的 *** 作方式为以下所示:")\r\n System.out.println(" 使用攻击需消耗1MP 伤害1HP")\r\n System.out.println(" 使用蓄力可增加1MP 伤害0HP")\r\n System.out.println(" 使用躲闪需消耗0MP 伤害0HP 躲避攻击")\r\n System.out.println(" 使用重击需消耗3MP 伤害2HP 防御无效")\r\n System.out.println(" 恶龙攻击力高, 注意防御是取胜的关键")\r\n System.out.println(" 恶龙MP达到4时可能会放出火焰无法躲避")\r\n System.out.println(" 准备说明完毕,那么让我们来挑战恶龙吧!")\r\n System.out.println("==================================")\r\n //难度选择\r\n System.out.println("请选择难度")\r\n System.out.println("1.娱乐 2.挑战 3.噩梦")\r\n int ND = Nandu.nextInt()\r\n System.out.println("==================================")\r\n while(true) \r\n { \r\n //HP,MP的显示 \r\n System.out.print("勇者: ")\r\n System.out.print(" HP ")\r\n for(int x=YingxHPx>0x--) System.out.print("*")\r\n System.out.print(" "+YingxHP)\r\n System.out.println()\r\n System.out.print(" ")\r\n System.out.print(" MP ")\r\n for(int x=YingxMPx>0x--) System.out.print("*")\r\n System.out.print(" "+YingxMP)\r\n System.out.println()\r\n System.out.print("恶龙: ")\r\n System.out.print(" HP ")\r\n for(int y=MogHPy>0y--) System.out.print("*")\r\n System.out.print(" "+MogHP)\r\n System.out.println()\r\n System.out.print(" ")\r\n System.out.print(" MP ")\r\n for(int y=MogMPy>0y--) System.out.print("*")\r\n System.out.print(" "+MogMP)\r\n System.out.println()\r\n System.out.println("==================================")\r\n //胜利判定 \r\n if(YingxHP2&&MogMP>2) MogDo = 2\r\n else if(MogMP0) MogDo=2 \r\n if(Do==2&&MogMP==0) MogDo=0\r\n if(Do==3&&MogMP=4) MogDo=3}//变态判定。。 \r\n //战斗分析\r\n //防御 \r\n if(Do==3) {fangyu=1System.out.println("你灵巧的躲避攻击!")}\r\nif(MogDo==1) {fangyu=1System.out.println("恶龙进行防御!")}\r\n //角色判定 \r\n if(Do==1&&YingxMP==0) {System.out.println("MP不足!")}\r\n if(Do==1&&YingxMP>0) {\r\nif(fangyu==0) {MogHP=MogHP-1YingxMP=YingxMP-1System.out.println("你发动攻击!")}\r\nif(fangyu==1) {YingxMP=YingxMP-1System.out.println("你的攻击被格挡!")}} \r\n if(Do==2) {YingxMP=YingxMP+1System.out.println("你进行蓄力!")}\r\n if(Do==4&&YingxMP2) {MogHP=MogHP-2YingxMP=YingxMP-3System.out.println("你发动重击!")}\r\n if(Do>4) System.out.println("你不知所措...")\r\n //敌人判定 \r\n if(MogDo==2&&MogMP==0) {System.out.println("恶龙在发呆!")}\r\n if(MogDo==2&&MogMP>0) {\r\nif(fangyu==0) {YingxHP=YingxHP-2MogMP=MogMP-1System.out.println("恶龙发动攻击!")}\r\nif(fangyu==1) {MogMP=MogMP-1System.out.println("恶龙的攻击被躲开了!")}} \r\n if(MogDo==0) {MogMP=MogMP+1System.out.println("恶龙进行蓄力!")} \r\n if(MogDo==3&&MogMP3) {YingxHP=YingxHP-4MogMP=MogMP-4System.out.println("恶龙发动火焰吐吸!躲避不能!")} \r\n //结束 \r\n fangyu = 0\r\n MogDo = 0\r\n System.out.println("____________________")\r\n System.out.println()\r\n System.out.println("==================================") }\r\n }\r\n}有三个问题吧:import javax.swing.JApplet
是 extends JApplet
应该加一个name.addActionListener(this)
下面是修改后的代码,我运行过了
import java.awt.*
import javax.swing.JApplet
import java.awt.event.*
public class Applet1 extends JApplet implements ActionListener{
TextField name=new TextField(30)
TextField pw=new TextField(30)
TextField pw1=new TextField(30)
Label la1=new Label()
Label la2=new Label()
Label la3=new Label()
public void actionPerformed(ActionEvent e) {
if(e.getSource()==pw)
pw1.setText(pw.getText())
else if(e.getSource()==name)
name.setText("i know :"+name.getText())
}
public void init(){
this.setLayout(new FlowLayout())
la1.setText("姓名:")
this.add(la1)
this.add(name)
la2.setText("密码:")
this.add(la2)
pw.setEchoChar('*')
this.add(pw)
pw.addActionListener(this)
name.addActionListener(this)
la3.setText("显示密码:")
this.add(la3)
this.add(pw1)
}
}
呵呵!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)