Java lottery game efficiency, swing : <p>I believe this is not the kind of question I should be asking here so I apologize in advance. I have an exam in 2 days and the teacher that was supposed to give me a lesson clearing out all of my questions had to cancel, so now I have no one to ask. I am resolving an exam from last year, and one of the questions is to make a little java program where we pretty much simulate a lottery game of 6 numbers. My question is about efficiency, the exam is written on paper and I feel like if I was to write everything I typed on the computer I would not have enough time, we have 90 minutes and this is only one of the problems, swing is not my strong point so that leads me to think that I could write this in much fewer lines. The public instance variable 'key' is there to simulate a method from another class that generates a random key that we do not need to implement. We are only allowed to use FlowLayout, BorderLayout, CardLayout and GridLayout.</p>

<p>public class Grupo2A extends JFrame {</p>

<pre><code>private JPanel panelCont = new JPanel();
private JPanel panelUser = new JPanel();
private JPanel panelResult = new JPanel();
private JPanel p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11;
private JLabel l1, l2, l3, l4, l5, l6, l7, l8, l9;
private JTextField t1, t2, t3, t4, t5, t6;
private JButton b1, b2, b3;
private int[] userKey = new int[6];
private CardLayout cl = new CardLayout();
private GridLayout gl1 = new GridLayout(8, 1);
private GridLayout gl2 = new GridLayout(3, 1);
public int[] key = {12, 13, 16, 22, 33, 40};

private static final int WINDOW_HEIGHT1 = 600;
private static final int WINDOW_HEIGHT2 = 150;
private static final int WINDOW_WIDTH1 = 300;

public Grupo2A() {
    panelCont.setLayout(cl);
    panelCont.add(panelUser, "panelUser");
    panelCont.add(panelResult, "panelResult");
    panelUser.setLayout(gl1);
    panelResult.setLayout(gl2);
    setSize(WINDOW_WIDTH1, WINDOW_HEIGHT1);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    add(panelCont);

    createComponents();
    createPanelUser();
    createPanelResult();
    cl.show(panelCont, "panelUser");
    setVisible(true);
}

private void createComponents() {

    l1 = new JLabel("Lottery");
    l2 = new JLabel("1");
    l3 = new JLabel("2");
    l4 = new JLabel("3");
    l5 = new JLabel("4");
    l6 = new JLabel("5");
    l7 = new JLabel("6");

    t1 = new JTextField(10);
    t2 = new JTextField(10);
    t3 = new JTextField(10);
    t4 = new JTextField(10);
    t5 = new JTextField(10);
    t6 = new JTextField(10);

    b1 = new JButton("Play");
    b1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            int[] userKey = new int[6];
            userKey[0] = Integer.parseInt(t1.getText());
            userKey[1] = Integer.parseInt(t2.getText());
            userKey[2] = Integer.parseInt(t3.getText());
            userKey[3] = Integer.parseInt(t4.getText());
            userKey[4] = Integer.parseInt(t5.getText());
            userKey[5] = Integer.parseInt(t6.getText());
            setUserKey(userKey);
            l8.setText("You got " + correctNumbers() + " numbers");
            l9.setText("The key was: " + Arrays.toString(key));
            cl.show(panelCont, "panelResult");
            setSize(WINDOW_WIDTH1, WINDOW_HEIGHT2);
        }

    });
    b2 = new JButton("Clear");
    b2.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            t1.setText("");
            t2.setText("");
            t3.setText("");
            t4.setText("");
            t5.setText("");
            t6.setText("");
        }

    });

    p1 = new JPanel();
    p2 = new JPanel();
    p3 = new JPanel();
    p4 = new JPanel();
    p5 = new JPanel();
    p6 = new JPanel();
    p7 = new JPanel();
    p8 = new JPanel();
    p9 = new JPanel();
    p10 = new JPanel();
    p11 = new JPanel();

}

private void createPanelUser() {
    p1.add(l1);
    panelUser.add(p1);
    p2.add(l2);
    p2.add(t1);
    panelUser.add(p2);
    p3.add(l3);
    p3.add(t2);
    panelUser.add(p3);
    p4.add(l4);
    p4.add(t3);
    panelUser.add(p4);
    p5.add(l5);
    p5.add(t4);
    panelUser.add(p5);
    p6.add(l6);
    p6.add(t5);
    panelUser.add(p6);
    p7.add(l7);
    p7.add(t6);
    panelUser.add(p7);
    p8.add(b1);
    p8.add(b2);
    panelUser.add(p8);
}

private void createPanelResult() {
    l8 = new JLabel("You got " + correctNumbers() + " numbers");
    l9 = new JLabel("The key was: " + Arrays.toString(key));
    b3 = new JButton("Ok");
    b3.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            cl.show(panelCont, "panelUser");
            setSize(WINDOW_WIDTH1, WINDOW_HEIGHT1);
        }

    });
    p9.add(l8);
    p10.add(l9);
    p11.add(b3);
    panelResult.add(p9);
    panelResult.add(p10);
    panelResult.add(p11);
}

private int correctNumbers() {
    int cont = 0;
    for (int i = 0; i &lt; userKey.length; i++) {
        for (int j = 0; j &lt; key.length; j++) {
            if (userKey[i] == key[j]) {
                cont++;
                break;
            }
        }
    }
    System.out.println(cont);
    return cont;
}

private void setUserKey(int[] userKey) {
    this.userKey = userKey;
}
</code></pre>

<p>}</p>
