[Java] cteni pole tlacitkem

C++, C#, Visual Basic, Delphi, Perl a ostatní

Moderátor: Moderátoři Živě.cz

Odeslat příspěvekod Azbas 10. 9. 2006 22:25

cau, potreboval bych, aby mi v apletu tlacitko cetlo jinde nadefinovane pole a nemohu na to zaboha prijit :-(

mam tlacitkove pole s jedinym posluchacem a potrebuju aby se na zmacknutem tlacitku zmenil popisek podle obsahu int pole definovane v jine tride (index urciteho tlacitka mam)

zatim mam tohle:
Frame2.java:

Kód: Vybrat vše
public class Frame2 extends JFrame {
  int miny[];
  JPanel jPanel1 = new JPanel();
  JButton jButton1 = new JButton();
  public JButton[] pole = new JButton[10];


  public Frame2() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
        }
    catch(Exception e) {
      e.printStackTrace();
    }
   new hra();
  }
  public static void main(String[] args) {
    Frame2 frame2 = new Frame2();
  }
  private void jbInit() throws Exception {
    this.getContentPane().add(jPanel1, BorderLayout.CENTER);
    Frame2_jButton1_actionAdapter jAd = new Frame2_jButton1_actionAdapter(this);
    for (int i = 1; i <= 6; i++) {
      pole[i] = new JButton();
      pole[i].setText("" + i);
      pole[i].addActionListener(jAd);
      jPanel1.add(pole[i]);
    }
    pole[0]=jButton1;
    this.setSize(new Dimension (500,300));
    this.setVisible(true);

  }

protected void processWindowEvent(WindowEvent e){
  super.processWindowEvent(e);
  if (e.getID()== WindowEvent.WINDOW_CLOSING)
   System.exit(1);
   }


  void jButton1_actionPerformed(ActionEvent e) {
    JButton jb = (JButton) e.getSource();
     int index = getIndex(jb);
     pole[index].setBackground(Color.RED);

     String s="nejde";
    s = Integer.toString([color=red]hra.get(index)[/color]);  - tohle nefunguje
     pole[index].setText(s);
  }


public int getIndex(JButton jb) {    // zjistuju index zmacknuteho tlacitka
      for (int i = 0; i < pole.length; i++) {
        if (pole[i] == jb) {
          return i;
        } }
      return -1; }
}

class Frame2_jButton1_actionAdapter implements java.awt.event.ActionListener {
  Frame2 adaptee;

  Frame2_jButton1_actionAdapter(Frame2 adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton1_actionPerformed(e);
  }
}


-----------------------------------------------
a hra.java-

Kód: Vybrat vše
public class hra {
static int miny[];
  public hra(){}
public static void main(String[] args) {
   hra hra1= new hra();
   int poc=6;
   int miny[]= new int [poc+1];
  for (int i = 1; i < poc; i++) {
   miny[i]= (i+50);
}
  }


public static int get (int index) {
int a=-1;
   a=miny[index];
return a;  }
}


problem bude nekde u toho pole, protoze kdzy ho vyradim a necham tam pouze cislo (int get(index){int a=4; return a;} ) tak to tu ctyrku na tlacitko prenese
poradite mi nekdo? :-)

//christian: přidán [code] tag
Azbas
Junior

Odeslat příspěvekod Imjagpul 11. 9. 2006 19:02

Jména tříd v Javě piš vždycky s velkým počátečním písmenem.

Hra.miny bylo null, protože se nikde nenastavuje (jenom v Hra.main(), ale to se z Frame2 nevolá).

Nevím, proč používáš v třídě hra statické proměnné a metody. Myslím, že chceš spíš tohle:
Kód: Vybrat vše
//Hra.java

public class Hra {
    private int miny[];
   
    public Hra(int pocet) {
        //při volání new Hra(pocet) se vytvoří to pole miny se zadanou délkou
        miny=new int[pocet];
    }
       
    public int get(int index) {
        return miny[index-1];
    }
}[/code
]//Frame2.java

public class Frame2 extends JFrame {
// int miny[];

//čísla bych radši odděloval do konstant, lépe se to mění
private static final int POCET=6;

private JPanel jPanel1 = new JPanel();
// JButton jButton1 = new JButton();
private JButton[] pole = new JButton[POCET];

//v tomhle bude ulozena instance hry vytvořené v konstruktoru
private Hra hra;

public Frame2() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
} catch(Exception e) {
e.printStackTrace();
}

//vytvořit instanci hry se zadaným počtem
hra=new Hra(POCET);
}
public static void main(String[] args) {
Frame2 frame2 = new Frame2();
}
private void jbInit() throws Exception {
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
Frame2_jButton1_actionAdapter jAd = new Frame2_jButton1_actionAdapter(this);
for (int i = 0; i < POCET; i++) {
pole[i] = new JButton();
pole[i].setText("" + i);
pole[i].addActionListener(jAd);
jPanel1.add(pole[i]);
}

//proč tady odděluješ zvlášt první button? IMHO je to zbytečné
//předtím si tu měl o jeden button víc než čísel ve Hra, to asi byla chyba, ne?
// pole[0]=jButton1;


this.setSize(new Dimension(500,300));
this.setVisible(true);

}

protected void processWindowEvent(WindowEvent e){
super.processWindowEvent(e);
if (e.getID()== WindowEvent.WINDOW_CLOSING)
System.exit(1);
}


void jButton1_actionPerformed(ActionEvent e) {
JButton jb = (JButton) e.getSource();
int index = getIndex(jb);
pole[index].setBackground(Color.RED);

String s="nejde";
s = Integer.toString(hra.get(index)); //tady se odkazuji na lokální proměnnou hra
pole[index].setText(s);
}


public int getIndex(JButton jb) { // zjistuju index zmacknuteho tlacitka
for (int i = 0; i < pole.length; i++) {
if (pole[i] == jb) {
return i;
}
}
return -1; }
}

class Frame2_jButton1_actionAdapter implements java.awt.event.ActionListener {
Frame2 adaptee;

Frame2_jButton1_actionAdapter(Frame2 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}[/code]
Imjagpul
Junior
Uživatelský avatar

Odeslat příspěvekod Azbas 11. 9. 2006 20:49

diky moc... ono to funguje :-) ja se s tim morim a ono to je takhle... jeste se toho o tom musim hodne naucit hlavne se musim podivat na pravidla pouzivani class :-)
jeste jednou diky
pokud nekdo chcete videt vysledek tak tady se muzete podivat :-)
Azbas
Junior


Kdo je online

Uživatelé procházející toto fórum: Žádní registrovaní uživatelé a 0 návštevníků