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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
@SuppressWarnings({ "nls", "serial" })
public class Fenetre extends JFrame {
public static void main(String[] args) {
Fenetre f = new Fenetre();
f.setVisible(true);
}
private JPanel ecran = new JPanel();
private JLabel titre = new JLabel("");
private JLabel text1 = new JLabel("Veuillez entrez le nombre");
private JTextField espace1 = new JTextField("");
private JButton bouton_ok = new JButton("OK");
private Random rand = new java.util.Random();
private JLabel text2 = new JLabel("");
private int nbcoups = 0;
private String espace11 = espace1.getText();
private Integer nombre;
public Fenetre() {
this.setTitle("Le Nombre Mystere...");
this.setSize(400, 70);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.ecran.setBackground(Color.WHITE);
this.ecran.setLayout(new BorderLayout());
this.espace1.setPreferredSize(new Dimension(100, 20));
this.bouton_ok.addActionListener(new ok());
// ===========================================================
JOptionPane.showMessageDialog(null, "Bienvenue dans le jeu...",
"Nombre Mystere", JOptionPane.INFORMATION_MESSAGE);
String[] lvl = { "1-100", "1-500", "1-1000" };
String nom = (String) JOptionPane.showInputDialog(null,
"Veuillez Chisir le niveau !", "Niveau",
JOptionPane.QUESTION_MESSAGE, null, lvl, lvl[2]);
// ===========================================================
if (nom.equals("1-100")) {
this.nombre = Integer.valueOf(this.rand.nextInt(100) + 1);
} else if (nom.equals("1-500")) {
this.nombre = Integer.valueOf(this.rand.nextInt(500) + 1);
} else {
this.nombre = Integer.valueOf(this.rand.nextInt(1000) + 1);
}
System.out.println("Nombre aléatoire : " + nombre);
JPanel top = new JPanel();
text1.setForeground(Color.red);
top.add(this.titre);
top.add(this.text1);
top.add(this.espace1);
top.add(this.bouton_ok);
top.add(this.text2);
this.ecran.add(top, BorderLayout.CENTER);
this.setContentPane(this.ecran);
this.setVisible(true);
}
class ok implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
Integer proposition = Integer.valueOf(Integer.parseInt(Fenetre.this.espace1.getText()));
int comparaison = Fenetre.this.nombre.compareTo(proposition);
if (comparaison == 0) {
System.out.println("trouvé");
JOptionPane.showMessageDialog(null, "Bravo ! le nombre c'etait bien "+nombre+"\n vous l'avez trouver en << "+nbcoups+" Coups >>",
"Nombre Mystere", JOptionPane.INFORMATION_MESSAGE);
sauvgarde();
} else if (nombre < proposition) {
System.out.println("trop grand");
JOptionPane.showMessageDialog(null, "le nombre mystere est MOIN que "+proposition,
"Nombre Mystere", JOptionPane.INFORMATION_MESSAGE);
nbcoups++;
} else {
System.out.println("trop petit");
JOptionPane.showMessageDialog(null, "le nombre mystere est PLUS que "+proposition,
"Nombre Mystere", JOptionPane.INFORMATION_MESSAGE);
nbcoups++;
}
} catch(NumberFormatException ex) {
}
}
}
public void sauvgarde() {
FileWriter writer = null;
String texte = "\t niveau :score :" +nbcoups;
try{
writer = new FileWriter("sauvgarde.txt", true);
writer.write(texte,0,texte.length());
writer.close();
}catch(IOException ex){
ex.printStackTrace();
}
}
} |
Partager