Put a name depending on the array number

Asked

Viewed 433 times

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.

  • 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?

  • I have already provided the complete code. It should be on the team 2 because the array draw is random.

2 answers

2

A 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 name array ex:

if vc access the label "equip2" the 2 result of the array, which is 3 and by name just do:

nomes_equipes[arr[2]]

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

2


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

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:

inserir a descrição da imagem aqui


It is worth mentioning the recommendation below:

Avoid using absolute layout, unless it is of extreme necessity and you know the consequences of it, because absolute layout makes it difficult to maintain the screen and makes your application look different depending on the monitor and resolution that is running.

There are several layouts so you don’t have to worry about positioning or manually organizing components. Not to mention that using layouts makes your code easier to maintain than inserting a lot of setbounds, and if you need to change the position of any component, in the absolute layout, you will have to reposition all manually.

To distribute the result points, I used GridLayout, that gives you freedom to organize items in a grids layout (similar to excel), and as it comes to 4 Jlabels in quadratic form, it was the best and simplest option for the case.

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

  • Yes I know it will burst but can exemplify this part of defining the Jlabel ?

  • @Ricardolopes labels[indice] = new JLabel(); you can do this within a loop, and go positioning it on your screen.

  • I’m sorry but I still don’t understand how to do..

  • Can better illustrate ?

  • @Ricardolopes see the edition.

  • I understand your recommendation, my teacher only taught me in school how to work with absolute layout and I have tried to use others and I think it is too difficult recommend some tutorial ?

  • @Ricardolopes executed the code of my answer? It is already functional. And on links, see the recommendation in the reply, there are links of official documentation teaching to tinker with each.

  • Yes I ran the code and it’s working perfectly with what was intended.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.