Put a name depending on the number of the array

1

Let's say I have 4 variables:

 - Porto = 1   
 - Benfica = 2  
 - Sporting = 3  
 - Braga = 4

I have this code that makes me a shuffle of numbers in 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 let's imagine that I get the result: [1, 3, 2, 4];

Now that I have this result I have 4 labels (team1, team2, team3, team4);

I want to put in the label "equipo1" the 1 result of the array and put the name of the team corresponding to that number in this case PORTO.

Another example ..

I want to put in the label "team2" the 2 result of the array, which is 3, so I want to put in this label the team corresponds to number 3, in this case Sporting ..

Can you explain how can I do this?

To run the program you 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);

    }


}
    
asked by anonymous 16.03.2018 / 18:49

2 answers

2

To facilitate distribution, create an array of JLabel with the same number of indexes as your team array, and distribute the teams after shuffle as follows:

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Collections;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

public class J2 extends JFrame {

    private JPanel contentPane;
    private JLabel[] equipas = new JLabel[4];


    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++) {
            equipas[i].setText(String.valueOf(arr[i]));
        }

        System.out.println(Arrays.toString(arr));
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new J2().setVisible(true));
    }


    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);

        JPanel equipaPanel = new JPanel(new GridLayout(2,2));

        for(int i = 0; i < equipas.length; i++) {
            equipas[i] =  new JLabel();
            equipas[i].setHorizontalAlignment(SwingConstants.CENTER);
            equipaPanel.add(equipas[i]);
        }

        equipaPanel.setBounds(20, 36, 149, 50);
        contentPane.add(equipaPanel);

    }
}

See working:

Notetherecommendationbelow:

  

Avoidusingabsolutelayoutunlessyouareindireneedandknowtheconsequences,becauseabsolutelayoutmakesitdifficulttomaintainthescreenandmakeyourapplicationlookdifferentdependingonthemonitorandresolutionbeingexecuted.

    

Thereareseveral layouts managers for you to you do not have to worry about manual positioning or organization of components. Not to mention that the use of layouts makes your code easier to maintain than inserting a lot of setbounds , and in case you need to change the position of any component, in the absolute layout, you will have to reposition them all manually.

In order to distribute the points of the result, I used GridLayout , which gives you freedom to organize items in a grids layout (similar to excel), and because it deals with 4 JLabels in quadratic form, it was the best and simpler option for the case.

As for the absolute layout of the main panel, I did not remove it as I would have to rewrite your screen. But the recommendation above is up to you.

    
16.03.2018 / 19:05
2

One solution would be to make an array with the name of the teams;

String [] nomes_equipes = {"Porto","Benfica","Sporting","Braga"};

Then, just access the position (team) of the array of names ex:

If you access the label "equipo2" the 2 result of the array, which is 3 and for the name just do:

nomes_equipes[arr[2]]

It will access position 2 of the array (that value is 3) and the position 3 of team_name (Sporting)

    
16.03.2018 / 18:59