Ex1_4.java
/**
* Liste Box
*
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Ex1_5 implements ActionListener {
// Déclaration des composants
JFrame fListe;
JComboBox lstListe;
JPanel pnlListe,pnl2;
JTextField txtTexte;
JLabel lblTexte;
JButton btnAjouter,btnSupprimer;
// Création et affichage de la fenêtre
public Ex1_5() {
//Création et dimensionnement de la fenêtre.
fListe = new JFrame("Liste");
fListe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fListe.setSize(new Dimension(120, 40));
//Création et dimensionnement d'un cadre.
pnlListe = new JPanel();
pnl2 = new JPanel(new GridLayout(3,0));
composants();
fListe.add(pnlListe, BorderLayout.WEST);
fListe.add(pnl2, BorderLayout.EAST);
/* NORTH, WEST, EAST, SOUTH, CENTER, permet de positionner un composant
dans une des 5 régions, chaque région ne pouvant contenir plus d'un composant
*/
//Affichage de la fenêtre.
fListe.pack();
fListe.setVisible(true);
}
/**
* Création des composants.
*/
private void composants() {
txtTexte=new JTextField();
btnAjouter=new JButton("Ajouter");
btnSupprimer=new JButton("Supprimer");
btnAjouter.addActionListener(this);
btnSupprimer.addActionListener(this);
String[] tab={"El1","El2"};
lstListe=new JComboBox(tab);
//Ajout des composants à l'objet panel.
pnlListe.add(lstListe);
pnl2.add(txtTexte);
pnl2.add(btnAjouter);
pnl2.add(btnSupprimer);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==btnAjouter){
lstListe.addItem(txtTexte.getText());
}
else if( e.getSource()==btnSupprimer){
lstListe.removeItemAt(lstListe.getSelectedIndex());
}
}
private static void afficher() {
// Ajouter des éléments de décoration à la fenêtre
JFrame.setDefaultLookAndFeelDecorated(true);
Ex1_5 app = new Ex1_5();
}
public static void main(String[] args) {
afficher();
}
}