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