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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
public class LotoUI extends JFrame{
private static final long serialVersionUID = 1L; //Pour l'extension de JFrame
private Generateur g; //Le générateur de nombre aléatoire
final JLabel nombre; //Le nombre affiché dans la JFrame
final JTextArea tBorne; //La borne maximum, renseignée par l'utilisateur
// Classe privée contenant l'instance unique (pattern singleton)
private static class LotoUIHolder
{
/** Instance unique non préinitialisée */
private final static LotoUI instance = new LotoUI();
}
/** Point d'accès pour l'instance unique du singleton */
public static LotoUI getInstance()
{
return LotoUIHolder.instance;
}
private LotoUI()
{
//Initialisation des variables de classe
g=new Generateur();
//Paramètres de la fenêtre
this.setName("Jeu de loterie");
this.setSize(1300,650);
this.setDefaultCloseOperation(LotoUI.DISPOSE_ON_CLOSE);
this.setUndecorated(true);
//Définition des éléments à ajouter
JLabel lBorne=new JLabel("Borne supérieure :");
tBorne= new JTextArea("100",1,6);
tBorne.setEditable(true);
tBorne.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
JScrollPane scrollPane = new JScrollPane(tBorne);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
nombre=new JLabel(" ");
nombre.setPreferredSize(new Dimension(1000,1000));
Font font = new Font("Arial",Font.BOLD,300);
nombre.setFont(font);
//Boutons :
JButton lancer = new JButton("Lancer");
JButton hide = new JButton("Poursuivre");
JButton quit = new JButton("Quitter");
//Placement des éléments dans une grille
JPanel monPanel= new JPanel();
monPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(9, 8, 0, 10);
gbc.anchor = GridBagConstraints.BASELINE;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx=0;
monPanel.add(lBorne,gbc);
gbc.fill = GridBagConstraints.VERTICAL;
gbc.gridx = 2;
gbc.gridy = 0;
gbc.gridheight = 1;
gbc.weightx=100000000;
monPanel.add(tBorne,gbc);
gbc.gridx = 5;
gbc.gridy = 0;
gbc.gridheight = 1;
monPanel.add(lancer,gbc);
gbc.gridx = 7;
gbc.gridy = 0;
gbc.gridheight = 1;
monPanel.add(quit,gbc);
gbc.gridx = 4;
gbc.gridy = 0;
gbc.gridheight = 1;
monPanel.add(hide,gbc);
gbc.gridx = 4;
gbc.gridy = 6;
gbc.gridheight = 100;
gbc.weighty=0.5;
gbc.ipady = 0;
monPanel.add(nombre,gbc);
Color maCouleur = new Color((float)0.2,(float)0.6,1.F,1.F);
monPanel.setBackground(maCouleur);
this.add(monPanel);
quit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
System.exit(0);
}
});
lancer.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
new Thread(new Runnable() {
public void run() {
anime();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
int tmp=g.joue(Integer.parseInt(tBorne.getText()));
if(tmp!=-1)
nombre.setText(Integer.toString(tmp));
else
nombre.setText("Fin !");
}
});
}
}).start();
}
});
// Ici, le bouton qui cache la fenêtre
hide.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
LotoUI.getInstance().setVisible(false);
}
});
}
// Cette méthode sert juste à animer le tirage au sort
private void anime()
{
int i=0;
for(i=0;i<70;i++)
{
nombre.setText(Integer.toString(g.tire(Integer.parseInt(tBorne.getText()))));
//repaint();
try{
Thread.sleep(60);
}catch(InterruptedException e){}
}
}
} |
Partager