jeudi 17 mars 2016

Random numbers to JTextfield

I am making a game called Who wants to be a zillionaire for a University Assignment. I am using Eclipse IDE. I have this code which gives a percentage for each answer using the random number generater method. Code:

public AskPublic ()
    {

        Random r = new Random ();
        int A = r.nextInt(101);
        int B = r.nextInt(101 - A);
        int C = r.nextInt(101 - A - B);
        int D = 100 - A - B - C;
}

Can someone please let me know how I can add this to a GUI using Window builder. This is some of the code for the GUI Code

    btnDismiss = new JButton("Dismiss");
    btnDismiss.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            dispose();
        }
    });

    btnDismiss.setBounds(275, 187, 89, 23);
    percent.add(btnDismiss);

    lblA = new JLabel("A");
    lblA.setForeground(Color.ORANGE);
    lblA.setFont(new Font("Arial", Font.BOLD, 25));
    lblA.setBounds(88, 67, 23, 35);
    percent.add(lblA);

    lblB = new JLabel("B");
    lblB.setFont(new Font("Arial", Font.BOLD, 25));
    lblB.setForeground(Color.ORANGE);
    lblB.setBounds(209, 70, 31, 29);
    percent.add(lblB);

    lblC = new JLabel("C");
    lblC.setFont(new Font("Arial", Font.BOLD, 25));
    lblC.setForeground(Color.ORANGE);
    lblC.setBounds(333, 70, 31, 29);
    percent.add(lblC);

    lblD = new JLabel("D");
    lblD.setForeground(Color.ORANGE);
    lblD.setBackground(Color.WHITE);
    lblD.setFont(new Font("Arial", Font.BOLD, 25));
    lblD.setBounds(457, 70, 31, 29);
    percent.add(lblD);

    A1 = new JTextField();
    A1.setEditable(false);
    A1.setBounds(113, 67, 86, 32);
    percent.add(A1);
    A1.setColumns(10);
    System.out.println(A);

    B2 = new JTextField();
    B2.setEditable(false);
    B2.setBounds(233, 67, 86, 32);
    percent.add(B2);
    B2.setColumns(10);
    System.out.println(B);

    C3 = new JTextField();
    C3.setEditable(false);
    C3.setBounds(361, 67, 86, 32);
    percent.add(C3);
    C3.setColumns(10);
    System.out.println(C);

    D4 = new JTextField();
    D4.setEditable(false);
    D4.setBounds(485, 67, 86, 32);
    percent.add(D4);
    D4.setColumns(10);
    System.out.println(D);
}       




Aucun commentaire:

Enregistrer un commentaire