Como generar numeros aleatorios
Publicado por C3LLY (3 intervenciones) el 07/05/2020 16:34:52
Necesito crear una pantalla que genere una primitiva de forma aleatoria. Cada número no puede repetirse con ninguno de los anteriores. Los números se generan al pulsar el botón de “Generar”.
He generado este código pero me repite los números en los diferentes JTextField y no se pueden repetir.
He generado este código pero me repite los números en los diferentes JTextField y no se pueden repetir.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package p5_t6;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.Font;
//The main of this exercise is Principal2
public class EX2 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextField Texto1;
private JTextField Texto2;
private JButton Boton2;
private JButton Boton1 ;
public EX2() {
setBounds(100,100,641,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
Texto1 = new JTextField();
Texto1.setBounds(52, 73, 146, 26);
getContentPane().add(Texto1);
Texto1.setColumns(10);
JLabel Etiqueta1 = new JLabel("Derecho");
Etiqueta1.setBounds(52, 41, 69, 20);
getContentPane().add(Etiqueta1);
JLabel Etiqueta2 = new JLabel("Rev\u00E9s");
Etiqueta2.setBounds(307, 41, 69, 20);
getContentPane().add(Etiqueta2);
Texto2 = new JTextField();
Texto2.setBounds(301, 73, 146, 26);
getContentPane().add(Texto2);
Texto2.setColumns(10);
Boton2 = new JButton(">");
Boton2.setFont(new Font("Tempus Sans ITC", Font.BOLD, 16));
Boton2.setBounds(213, 122, 73, 48);
Boton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e ){
String aux=Texto1.getText();
Texto2.setText(aux);
Texto1.setText("");
}
});
getContentPane().add(Boton2);
Boton1 = new JButton("<");
Boton1.setBounds(213, 65, 73, 43);
Boton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String aux=Texto1.getText();
Texto2.setText(aux);
Texto1.setText("");
}
});
getContentPane().add(Boton1);
}
}
//Clase principal
package p5_t6;
public class Principal2 {
public static void main(String[] args) {
EX2 window = new EX2();
window.setVisible(true);
}
}
Valora esta pregunta


0