1
Let’s say I have four variables:
- Porto = 1 - Benfica = 2 - Sporting = 3 - Braga = 4
I have this code that makes me a shuffle of numbers of an array called "arr".
Integer[] arr = {1,2,3,4};
Collections.shuffle(Arrays.asList(arr));
for (int i = 0; i < arr.length; i++) {
}
Collections.shuffle(Arrays.asList(arr));
When running the program imagine that I got the result : [1, 3, 2, 4];
Now that I have this result I have 4 Labels (equip1, equip2, equip3, equip4);
I want to put on the label "equip1" the 1 result of the array and put the name of the team corresponding to that number in this case PORT.
Another example..
I want to put on the label "equip2" the 2 result of the array, which is 3, so I want to put on this label the team corresponds to number 3, in this case Sporting..
Can you explain to me how I can do this ?
To run the program have the code here below:
public class J2 extends JFrame {
private JPanel contentPane;
JLabel equipa1 = new JLabel("");
JLabel equipa2 = new JLabel("");
JLabel equipa3 = new JLabel("");
JLabel equipa4 = new JLabel("");
public void func(){
int Porto = 1;
int Benfica = 2;
int Sporting = 3;
int SCVitoria = 4;
Integer[] arr = {1,2,3,4};
Collections.shuffle(Arrays.asList(arr));
for (int i = 0; i < arr.length; i++) {
}
Collections.shuffle(Arrays.asList(arr));
System.out.println(Arrays.toString(arr));
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
J2 frame = new J2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public J2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 553, 405);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblJornada = new JLabel("Jornada 1");
lblJornada.setBounds(61, 11, 80, 14);
contentPane.add(lblJornada);
JButton btnNewButton = new JButton("Gerar Jornada");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
func();
}
});
btnNewButton.setBounds(125, 139, 139, 23);
contentPane.add(btnNewButton);
equipa1.setHorizontalAlignment(SwingConstants.CENTER);
equipa1.setBounds(20, 36, 139, 14);
contentPane.add(equipa1);
equipa2.setHorizontalAlignment(SwingConstants.CENTER);
equipa2.setBounds(169, 36, 139, 14);
contentPane.add(equipa2);
equipa3.setHorizontalAlignment(SwingConstants.CENTER);
equipa3.setBounds(20, 86, 139, 14);
contentPane.add(equipa3);
equipa4.setHorizontalAlignment(SwingConstants.CENTER);
equipa4.setBounds(169, 86, 139, 14);
contentPane.add(equipa4);
}
}
Label? Is it graphical interface? If yes, you need to provide one [mcve] of its interface so that it is possible to execute the code.
– user28595
Quero colocar na label "equipa2" o 2 resultado do array, que é 3, então quero colocar nessa label a equipa corresponde ao numero 3, neste caso Sporting..
- this completely diverged from the rest of the question. If Sporting is 3 , because he should be in outfit2?– user28595
I have already provided the complete code. It should be on the team 2 because the array draw is random.
– Ricardo Lopes