我对Java编码很新,我很擅长。 我一直在努力尝试在这段代码上放置一个重启按钮多年,这真让我烦恼。
所有内容都已初始化并导入,我无法让它工作(对不起复制和粘贴作业很糟糕)
public void init()
{
click=0;
s=0;
setSize(300,400);
buffer = createImage(getSize().width,getSize().height);
bufferg = buffer.getGraphics();
spaceship = getImage(getDocumentBase(),"Boole.fw.png");
shoot = getImage(getDocumentBase(),"wm.fw.png");
eshoot = getImage(getDocumentBase(),"BOIMB.png");
invader = getImage(getDocumentBase(),"Ho Chi Minh.fw.png");
setBackground(Color.black);
enemies = (6 + (3*(level-1)));
left = enemies;
xplace = 75;
yplace = 0;
try {
String str = "SomeMoreTextIsHere";
File newTextFile = new File("C:/thetextfile.txt");
FileWriter fw = new FileWriter(newTextFile);
fw.write(str);
fw.close();
} catch (IOException iox) {
//do stuff with exception
iox.printStackTrace();
}
for (int x = 0; x < enemies; x++)
{
if (x%6 == 0 && x!= 0){xplace = 75; yplace += 25;}
else{}
ex[x] = xplace;
ey[x] = yplace;
goaway[x] = 0;
xplace += 25;
}
if (level == 1)
{
main.start();
}
}
public void run()
{
while (kcthegreat == 0)
{
try
{
main.sleep(80);
}
catch(Exception e){}
if (s==0)
{
for (int y = 0; y < 99999999; y++);
for (int x=0;x<99999999;x++);
}
s++;
if (newstage==1)
{
newstage=0;
shot=0;
click=0;
levelstatus=1;
es=0;
}
else {
for (int x=0;x<shot;x++)
{
shoty[x]= shoty[x] -20;
if (shoty[x] - 15< 0){}
else
{
for (int z=0;z<enemies;z++)
{
if (shotx[x] <= ex[z] && ex[z] <= shotx[x]+25)
{
if (shoty[x] <= ey[z] && ey[z] <= shoty[x]+25)
{
goaway[z] =1;
score+=100;
shotx[x] = 500;
break;
}
else{}
}
else if (shotx[x] <= ex[z]+15 && ex[z]+15 <= shotx[x]+25)
{
if (shoty[x] <= ey[z]+15 && ey[z]+15 <= shoty[x]+25)
{
goaway[z] =1;
score+=100;
shotx[x] = 500;
break;
}
else{}
}
else{}
}
}
}
for (int x=0;x<enemies;x++)
{
int movex =(int)(Math.random()*40);
int movey =(int)(Math.random()*40);
movex-=20;
movey-=20;
ex[x]+=movex;
ey[x]+=movey;
if (ex[x] <0){ex[x] = 0;}
else if (ex[x] >275) {ex[x] = 275;}else{}
if (ey[x] <0){ey[x] = 0;}
else if (ey[x] >375) {ey[x] = 375;}else{}
}
for (int x=0;x<enemies;x++)
{
if (goaway[x] == 0)
{
int att =(int)(Math.random()*10);
if (att==1)
{
eshotx[es] = ex[x];
eshoty[es] = ey[y];
es++;
}
else{}
}
else{}
}
for (int b=0;b<es;b++)
{
eshoty[b]+=15;
if (x <= eshotx[b] && eshotx[b] <=x+25)
{
if (y <= eshoty[b] && eshoty[b] <=y+25)
{
spaceship=getImage(getDocumentBase(),"boom.jpg");
dead=1;
}
}
}
left=enemies;
for (int x=0;x<enemies;x++)
{
if (goaway[x] ==1 ){left--;}
else{}
if (left==0){newstage=1;level++;}else{}
}
repaint();
}
}
}
public void stop()
{
if(main!=null){main.stop();}
}
public void paint(Graphics g)
{
update(g);
}
public void update(Graphics g){
Dimension d=getSize();
Color Ground = new Color(0xFFCC66);
Color Sky = new Color(0x6698FF);
Color Grass = new Color(0x00FF00);
bufferg.setColor(Ground);
bufferg.fillRect(0,0,d.width,d.height);
bufferg.setColor(Sky);
bufferg.fillRect(0, 0, 300, 200);
bufferg.setColor(Grass);
bufferg.fillRect(0, 200, 300, 10);
bufferg.setColor(Color.black);
bufferg.setFont(new Font("Hobo STD",Font.BOLD,10));
bufferg.drawString("Score: "+score,3,395);
bufferg.setColor(Color.black);
bufferg.setFont(new Font("Hobo STD",Font.BOLD,10));
int shotsleft = shot;
bufferg.drawString("Level: "+level,120,395);
bufferg.setColor(Color.black);
bufferg.setFont(new Font("Hobo STD",Font.BOLD,10));
bufferg.drawString("Cannon: "+shotsleft,220,395);
bufferg.drawImage(spaceship,x,y,this);
for (int x=0;x<es;x++)
{
bufferg.drawImage(eshoot,eshotx[x],eshoty[x],this);
}
for (int x=0;x<shot;x++)
{
bufferg.drawImage(shoot,shotx[x],shoty[x],this);
}
for (int x=0;x<enemies;x++)
{
if (goaway[x] == 0)
{
bufferg.drawImage(invader,ex[x],ey[x],this);
}
else{}
}
if (es==200){es=0;}
if (dead==1)
{
Dimension q=getSize();
Color Red2 = new Color(0xF70D1A);
int score1 = 0;
int score2 = 0;
bufferg.setColor(getBackground());
bufferg.fillRect(0,0,q.width,q.height);
bufferg.setColor(Red2);
bufferg.setFont(new Font("Hobo STD",Font.BOLD,25));
bufferg.drawString("HO CHI MINH INVADERS", 10, 25);
bufferg.setFont(new Font("Hobo STD", Font.BOLD, 20));
bufferg.drawString("Game Over!", 100, 50);
bufferg.setFont(new Font("Hobo STD",Font.BOLD,20));
bufferg.setColor(Red2);
bufferg.drawString("Score: ", 110 , 340);
bufferg.drawString("" + score, 110, 360);
bufferg.drawString("Leaderboard", 95, 200);
Button restartbutt;
restartbutt = new Button("Play Again?");
add(restartbutt);
restartbutt.setBackground(Color.BLACK);
restartbutt.setForeground(Red2);
restartbutt.setBounds(200, 350, 85, 40);
restartbutt.setFont(new Font("Hobo STD", Font.BOLD, 13));
es=100000;
}
else{}
g.drawImage(buffer,0,0,this);
}
public boolean mouseDown(Event e, int x, int y)
{
click++;
shotx[shot] = x;
shoty[shot] = y;
if (shot==50)
{shot=0;}
else{shot++; score+=5;}
return true;
}
public boolean mouseMove(Event e, int xone, int yone)
{
x= xone;
y= yone;
if (x>275){xone=275;x=275;}
else if (y>361) {yone=361; y=361;}
else{x=xone;y=yone;}`enter code here`
repaint();
return true;
}
}
非常感谢任何帮助
答案 0 :(得分:1)
您的按钮没有ActionListener,因此每当您按下它时我都看不到任何事情。例如:
JButton showDialogButton = new JButton("Text Button");
showDialogButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// display/center the jdialog when the button is pressed
JDialog d = new JDialog(frame, "Hello", true);
d.setLocationRelativeTo(frame);
d.setVisible(true);
}
});
什么是ActionListener及其工作原理,您可以在此处找到:How does an ActionListener work?