Java - Buscar la posición de un click en un arreglo de botónes

 
Vista:

Buscar la posición de un click en un arreglo de botónes

Publicado por Sebastián (1 intervención) el 24/11/2022 14:42:58
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//Tablero
package batlleship;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
 
public class Tablero extends JFrame implements ActionListener{
     int i,j,a,b;
     JLabel p1, p2, np1, np2, h, v, t1, t2;
     JButton celda, colocar, jugar, ori;
     Tablero_2 tablero_2;
     public Tablero(String nombre1, String nombre2) {
 
        setResizable(true);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(3000,1000);
        setLocationRelativeTo(null);
        setTitle("Tablero");
 
        Container contentPane;
        contentPane = getContentPane();
        contentPane.setLayout(null);
        contentPane.setBackground(Color.white);
 
       p1 = new JLabel("jugador 1");
       p1.setBounds(10, 10, 70, 20);
       contentPane.add(p1);
 
       np1 = new JLabel(""+nombre1);
       np1.setBounds(100, 10, 200, 20);
       contentPane.add(np1);
 
       p2 = new JLabel("jugador 2");
       p2.setBounds(10, 50, 70, 20);
       contentPane.add(p2);
 
       np2 = new JLabel(""+nombre2);
       np2.setBounds(100, 50, 200, 20);
       contentPane.add(np2);
 
       for(i=1;i<11;i++){
           for(j=1;j<11;j++){
 
               celda = new JButton();
               celda.setBounds(200+(40*i),150+(40*j),30,30);
               this.add(celda);
               celda.addActionListener(this);
               celda.setBackground(Color.blue);
 
 
               /*for(a=0;a<10;a++){
                   for(b=0;b<10;b++){
                       b=0;
                       celda.setName(" "+a+" "+b);
                   }
               }*/
           }
       }
       for(i=1;i<11;i++){
           for(j=1;j<11;j++){
 
               celda = new JButton();
               celda.setBounds(860+(40*i),150+(40*j),30,30);
               this.add(celda);
               celda.addActionListener(this);
               celda.setBackground(Color.blue);
 
               a=0;
               b=0;
               celda.setName(""+a+""+b);
               a++;
               b++;
           }
       }
       colocar = new JButton("Colocar");
       colocar.setBounds(10, 100, 100, 20);
       this.add(colocar);
       celda.addActionListener(this);
 
       jugar = new JButton("jugar");
       jugar.setBounds(10, 140, 100, 20);
       this.add(jugar);
       jugar.addActionListener(this);
 
       ori = new JButton("Cambiar orientación");
       ori.setBounds(10, 180, 150, 20);
       this.add(ori);
       ori.addActionListener(this);
 
       h = new JLabel("horizontal");
       h.setBounds(10, 210, 200, 20);
       contentPane.add(h);
 
       v = new JLabel("vertical");
       v.setBounds(10, 240, 200, 20);
       contentPane.add(v);
 
       t1 = new JLabel("Tablero tiro");
       t1.setBounds(400, 100, 200, 20);
       contentPane.add(t1);
 
       t2 = new JLabel("Tablero jugador");
       t2.setBounds(1050, 100, 200, 20);
       contentPane.add(t2);
     }
 
 
    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
    }
 
}

soy un novato en programación y me gustaria poder obtener la posición del arreglo de botónes para poder cambiar el colór de ambos arreglos si alguien me pudiera ayudar se lo agradeceria mucho
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder