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
| import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Fenetre extends JFrame {
// Conteneurs
private JPanel cont1 = new JPanel( ) ;
private JPanel cont1Label = new JPanel( ) ;
private JPanel cont1BtnOper = new JPanel ( ) ;
private JPanel cont1BtnChiffre = new JPanel ( ) ;
//Nbre de Boutons Chiffre de 0 à 9 inclus + ( , et = ) soit 12
private static int nbreBtn = 12 ;
// Stockage de ces 10 boutons
private JButton[ ] tblBtn = new JButton[ nbreBtn ] ;
// Label pour affichage
private JLabel labelAff = new JLabel ( ) ;
public Fenetre ( ) {
// la fenêtre
this.setTitle( "Calculette Java" ) ;
this.setSize ( 300, 300 ) ;
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) ;
this.setLocationRelativeTo( rootPane ) ;
// pour les tests
this.setBackground( Color.black) ;
// détermination
cont1.setPreferredSize( new Dimension ( 250 ,250 ) ) ;
cont1BtnChiffre.setPreferredSize( new Dimension ( 180 , 180) );
cont1BtnOper.setPreferredSize( new Dimension ( 70 , 180) );
cont1Label.setPreferredSize ( new Dimension ( 150,50 ) ) ;
labelAff.setPreferredSize ( new Dimension ( 140,40 ) ) ;
// le conteneur principal
cont1.setBackground( Color.orange ) ;
cont1.setBorder(BorderFactory.createLineBorder(Color.blue));
// Le cont Label
cont1Label.setBackground( Color.lightGray) ;
cont1Label.setBorder(BorderFactory.createLineBorder(Color.white));
cont1Label.add( labelAff );
// le LabelAff
labelAff.setHorizontalAlignment( JLabel.RIGHT) ;
labelAff.setText( "Bonjour " );
// le cont pour les boutons chiffres
cont1BtnChiffre.setBorder(BorderFactory.createLineBorder(Color.magenta));
cont1BtnChiffre.setLayout( new GridLayout ( 4 , 3 , 4 , 4 ) ) ; // lignes, Colonnes, espacesH , espaceV
cont1BtnChiffre.setBackground( Color.blue ) ;
for ( int i = nbreBtn - 1 ; i >= 0 ; i-- ) {
if ( i == 11 ) tblBtn[ i ] = new JButton ( " ," ) ;
else if ( i == 10 ) tblBtn[ i ] = new JButton ( " =" ) ;
else {
tblBtn[ i ] = new JButton ( " " + i ) ;
cont1BtnChiffre.add( tblBtn[ i ] ) ;
}// de else
}// de for
cont1BtnChiffre.add( tblBtn[ 11 ] ) ;
cont1BtnChiffre.add( tblBtn[ 10 ] ) ;
// le contOpérateur
cont1BtnOper.setBorder(BorderFactory.createLineBorder(Color.blue));
cont1BtnOper.setBackground( Color.red ) ;
cont1BtnOper.setLayout( new GridLayout ( 4 , 1 , 1, 2)) ;
for ( int i = 0; i < 4 ; i++) {
if ( i == 3 ) tblBtn[ i ] = new JButton ( " -" ) ;
if ( i == 2 ) tblBtn[ i ] = new JButton ( " +" ) ;
if ( i == 1 ) tblBtn[ i ] = new JButton ( " *" ) ;
if ( i == 0 ) tblBtn[ i ] = new JButton ( " /" ) ;
//tblBtn[i].setPreferredSize( new Dimension ( 50, 40 ) ) ;
cont1BtnOper.add( tblBtn [ i ] );
}//de for
// le Layout
cont1.setLayout( new BorderLayout ( 5 , 5 ) ) ; // espa Vert , espace horyz
cont1.add(cont1Label, BorderLayout.NORTH ) ;
cont1.add(cont1BtnChiffre , BorderLayout.CENTER ) ;
cont1.add(cont1BtnOper, BorderLayout.EAST );
this.setContentPane( cont1 ) ;
this.setVisible( true ) ;
}// de constructeur
}// de class Fenetre |
Partager