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
| import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FenetreTest extends JFrame{
private JPanel Conteneur = new JPanel();
private JButton boutonC = new JButton("C");
private JButton bouton1 = new JButton("1");
private JButton bouton2 = new JButton("2");
private JButton bouton3 = new JButton("3");
private PanneauAffichage panAffichage = new PanneauAffichage();
public FenetreTest(){
this.setTitle("Test");
this.setSize(300, 200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
GridLayout gl1 = new GridLayout(1, 3);
gl1.setHgap(10);
JPanel panBoutons1 = new JPanel();
panBoutons1.setLayout(gl1);
panBoutons1.add(bouton1);
panBoutons1.add(bouton2);
panBoutons1.add(bouton3);
boutonC.addActionListener(new boutonOperatorListener());
bouton1.addActionListener(new boutonDigitListener(1));
bouton2.addActionListener(new boutonDigitListener(2));
bouton3.addActionListener(new boutonDigitListener(3));
Conteneur.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
//Posittionnement du panAffichage contenant le JLabel et le rectangle
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 1;
gbc.weightx = 3;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.insets = new Insets(5, 5, 5, 5);
Conteneur.add(panAffichage, gbc);
//Positionnement du panBoutons1 contenant les 3 premiers boutons de chiffres
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 3;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
gbc.weightx = 3;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(0, 5, 10, 10);
Conteneur.add(panBoutons1, gbc);
//Positionnement du boutonC
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 3;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
gbc.weightx = 1;
gbc.fill= GridBagConstraints.BOTH;
gbc.insets = new Insets(0, 0, 10, 5);
Conteneur.add(boutonC, gbc);
boutonC.setForeground(Color.red);
boutonC.setFont(new Font("Dialog", Font.BOLD, 20));
//Définition du ContentPane de la fenêtre
this.setContentPane(Conteneur);
this.setVisible(true);
}
public void NumberDisplay(int p_intDigit){
//Vérification si un chiffre (Digit) a été cliqué
panAffichage.setTextLabel(String.valueOf("bouton " + p_intDigit + " cliqué"));
}
class boutonDigitListener implements ActionListener{
int intDigit = -1;
public boutonDigitListener(){
}
public boutonDigitListener(int p_intDigit){
this.intDigit = p_intDigit;
}
@Override
public void actionPerformed(ActionEvent arg0) {
NumberDisplay(intDigit);
}
}
class boutonOperatorListener implements ActionListener{
public boutonOperatorListener(){
}
@Override
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource() == boutonC){
panAffichage.setTextLabel(String.valueOf(0));
}
}
}
} |
Partager