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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| // importation des classes du package
// Abstract Windowing Toolkit
import java.awt.*;
// importation des classes du projet Swing
import javax.swing.*;
// importation des classes event
import java.awt.event.*;
public class myFrame extends JFrame implements WindowListener,ActionListener {
// Define global component
JMenuItem quitter = null;
// Define constructor
myFrame(){
// Set initial parameters
setSize(300,300);
setTitle("My first frame =)");
setLocationRelativeTo(null);
// Define item menu for Fichier Menu
JMenuItem quitter = new JMenuItem("Quitter");
quitter.addActionListener(this);
JMenu fichier = new JMenu("Fichier");
fichier.add(quitter);
// Define item menu for Apparence Menu
JMenuItem change = new JMenuItem("Changer");
change.addActionListener(this);
JMenu apparence = new JMenu("Apparence");
apparence.add(change);
// Define the menu Bar
JMenuBar mBar = new JMenuBar();
mBar.add(apparence);
mBar.add(fichier);
setJMenuBar(mBar);
// Add windowListener
addWindowListener(this);
// Display the frame
setVisible(true);
}
public static void main(String Argv[]){
myFrame exo1 = new myFrame();
}
// Redefine methods from ActionListener
public void actionPerformed(ActionEvent ae){
if (ae.getActionCommand() == "Quitter") {
ConfirmExit();
}
else if (ae.getActionCommand() == "Changer") {
ChooseFigure figure = new ChooseFigure(this,"Modifier la figure",true,200,100);
}
}
// Redefine methods from WindowListener
public void windowActivated(WindowEvent we){
}
public void windowClosed(WindowEvent we){
}
public void windowClosing(WindowEvent we){
ConfirmExit();
}
public void windowDeactivated(WindowEvent we){
}
public void windowDeiconified(WindowEvent we){
}
public void windowIconified(WindowEvent we){
}
public void windowOpened(WindowEvent we){
}
public void ConfirmExit() {
int choix;
choix = JOptionPane.showConfirmDialog(this,"Etes-vous sûr de vouloir quitter ?","Attention !",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
setVisible(true);
// Get the value selected by the user
if ( choix == JOptionPane.OK_OPTION)
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
else
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
// Define a class which display a Jdialog to choose Menu
public class ChooseFigure extends JDialog implements ItemListener {
JComboBox fig = null;
String choixFigure = null;
// Define constructor
ChooseFigure(JFrame owner, String titre, boolean affichage, int largueur, int hauteur) {
// Initialise JDialog
super(owner,titre,affichage);
setSize(largueur,hauteur);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
// Define component
JLabel lblChoix = new JLabel("Choisissez votre figure:",JLabel.CENTER);
add(lblChoix);
String[] liste = {"", "Triangle", "Croix", "Rond", "Carré"};
JComboBox fig = new JComboBox(liste);
fig.addItemListener(this);
add(fig);
setVisible(true);
}
// Redefine methods from ActionListener
public void itemStateChanged(ItemEvent ie){
// Test selected figure and close the Dialog
if (fig.getSelectedIndex() == 1) {
choixFigure = "Triangle";
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
else if (fig.getSelectedIndex() == 2) {
choixFigure ="Croix";
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
else if (fig.getSelectedIndex() == 3) {
choixFigure ="Rond";
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
else {
choixFigure ="Carré";
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
// Method which permit to get the selected Figure
public String getSelectedFigure(){
return choixFigure;
}
}
} |
Partager