Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 8 years ago

java coding JTextField trouble?

setting the test move as m1, which is ice beam, it doesnt seem to want to either claim it being a hit or a miss :/

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.util.Random;

public class Teaser extends JApplet implements ActionListener {

ImageIcon yp1, ep1;

Image yp11, ep11;

JButton b1, b2;

JPanel p1;

JButton m1,m2,m3,m4;

Random r;

JTextField r1,r2;

String yt1, et1;

String mt, mc;

int mp, ma, mh;

int yh = 404;

int ya = 328;

int yd = 306;

int ysa = 438;

int ysd = 416;

int eh = 404;

int ea = 438;

int ed = 416;

int esa = 328;

int esd = 306;

int stab, td, damage, randdam;

public void init(){

setLayout(new GridLayout(5,0));

Random r = new Random();

yt1="water";

et1="ground";

setupButtons();

}

public void setupButtons(){

yp11 = getImage(getCodeBase(), "383.GIF");

yp1 = new ImageIcon (yp11);

b1 = new JButton ( yp1 );

add(b1);

b1.addActionListener( this );

ep11 = getImage(getCodeBase(), "382.png");

ep1 = new ImageIcon (ep11);

b2 = new JButton ( ep1 );

add(b2);

b2.addActionListener( this );

r1 = new JTextField(20);

r2 = new JTextField(20);

add(r1);

add(r2);

m1 = new JButton ("Ice Beam");

m2 = new JButton ("Ancient Power");

m3 = new JButton ("Hydro Pump");

m4 = new JButton ("Calm Mind");

JPanel p1 = new JPanel();

p1.setLayout(new GridLayout(0,4));

p1.add(m1);

p1.add(m2);

p1.add(m3);

p1.add(m4);

add(p1);

}

public void actionPerformed( ActionEvent ae ){

Object obj = ae.getSource();

if (obj == m1){

mt="ice";

mc="special";

mp=95;

ma=100;

mh=r.nextInt(100);

if (mh < ma){

if (mt=="ice" && et1=="ground");

td=2;

if (mt==yt1);

stab=2;

randdam= r.nextInt(15);

randdam+=85;

damage = (((42*ysa*mp/esd)/50)+2)*stab*td*randdam/100;

eh-=damage;

r1.setText("you hit for" + damage);

}

else if (mh >= ma);

r1.setText("you missed");

}

}

}

2 Answers

Relevance
  • Anonymous
    8 years ago
    Favorite Answer

    You need to add the buttons to actionListener

    m1.addActionListener(this);

    Same for the other m buttons.

    Also be careful when comparing strings in Java.

    == tests for reference.

    You have to use .equal() if you want to test for value equality.

    • Commenter avatarLogin to reply the answers
  • Anonymous
    8 years ago

    does it even get into the first if?? check with a System.out.println("string").

    or put obj=m1 before the first if just to test

    also get rid of the ';' after your if statement

    maybe validate() your frame after setting label

    the last else if, can just be else. these are the first things i noticed

    but probably its just unreachable code, debug with system outs to see if u reach all code

    haha Patrick, how did i not see that :P

    yea u should do m1.addActionListener(this);

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.