Moteur de recherche en swing

Description

Voilà, je présente un code destiné à facilité la recherche sur le Web! En gros vous n'avez qu'a selectionner le site à partir duquel vous voulez faire votre recherche et taper le ou les mot-clés ;-)

J'espère avoir assez documenté ma source :-p

Source / Exemple :


/*
    @auteur: FasteX
    Date de création: 17/09/04
    Dernière modif: 18/09/04

  • /
import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; import javax.swing.*; import javax.swing.event.*; public class MoteurRecherche extends JFrame implements ActionListener { private String currentSrc; private String strEnginez[] = {"Google Search", "Google Images", "Yahoo Search", "Yahoo Images", "AltaVista Search", "AltaVista Images", "AltaVista MP3/Audio", "AltaVista Video" }; private String urlEnginez[] = { "http://www.google.com/search?q=", // Google Search "http://www.google.com/images?q=", // Google Images "http://search.yahoo.com/search?p=", // Yahoo! Search "http://images.search.yahoo.com/search/images?p=", // Yahoo! Images "http://www.altavista.com/web/results?q=", // AltaVista Search "http://www.altavista.com/image/results?q=", // AltaVista Images "http://www.altavista.com/audio/results?q=", // AltaVista MP3/Audio "http://www.altavista.com/video/results?q=" }; // AltaVista Video private String strProg[] = { "Java FR", "Codes-Sources" }; private String urlProg[] = { "http://www.javafr.com/gma/tout/", // Java FR "http://www.codes-sources.com/gma/tout/" }; // Codes-Sources private String strGamez[] = { "JeuxVideo.com", "Gamez.com" }; private String urlGamez[] = { "http://www.jeuxvideo.com/schr.htm?textfield=", // JeuxVideo.com "http://filefan.com/sage/games.x?p=&ogs=&process=+Search+&k=" }; // Gamez.com private String strDlz[] = { "ZDNet", "FilePlanet" }; private String urlDlz[] = { "http://downloads-zdnet.com.com/3120-20-0.html?qt=", // ZDNet "http://www.fileplanet.com/search.aspx?q=" }; // FilePlanet private JRadioButtonMenuItem[] MI_ENGINEZ, MI_PROG, MI_GAMEZ, MI_DLZ; private JTextField txKeywords; private JButton btnSearch; public MoteurRecherche() { super.setTitle(":: Moteur de recherche ::"); JMenuBar barre = new JMenuBar(); // Menu 1: [ À partir de... ] JMenu M_FROM = new JMenu("À partir de..."); ButtonGroup group = new ButtonGroup(); // Sous-menu 1: [ Moteurs ] JMenu SM_MTR = new JMenu("Moteurs"); MI_ENGINEZ = new JRadioButtonMenuItem[strEnginez.length]; // création des options du sous-menu «Moteurs» for (int i = 0; i < strEnginez.length; i++) { MI_ENGINEZ[i] = new JRadioButtonMenuItem(strEnginez[i]); MI_ENGINEZ[i].addActionListener(this); group.add(MI_ENGINEZ[i]); SM_MTR.add(MI_ENGINEZ[i]); } currentSrc = strEnginez[0]; // Assigne à "currentSrc" une valeur par défaut MI_ENGINEZ[0].setSelected(true); // Selectionne une option par défaut M_FROM.add(SM_MTR); // Sous-menu 2: [ Programmation ] JMenu SM_PRG = new JMenu("Programmation"); MI_PROG = new JRadioButtonMenuItem[strProg.length]; // Création des options du sous-menu «Programmation» for (int i = 0; i < strProg.length; i++) { MI_PROG[i] = new JRadioButtonMenuItem(strProg[i]); MI_PROG[i].addActionListener(this); group.add(MI_PROG[i]); SM_PRG.add(MI_PROG[i]); } M_FROM.add(SM_PRG); // Sous-menu 3: [ Jeux ] JMenu SM_GZ = new JMenu("Jeux"); MI_GAMEZ = new JRadioButtonMenuItem[strGamez.length]; for (int i = 0; i < strGamez.length; i++) { MI_GAMEZ[i] = new JRadioButtonMenuItem(strGamez[i]); MI_GAMEZ[i].addActionListener(this); group.add(MI_GAMEZ[i]); SM_GZ.add(MI_GAMEZ[i]); } M_FROM.add(SM_GZ); // Sous-menu 4: [ Téléchargements ] JMenu SM_DL = new JMenu("Téléchargements"); MI_DLZ = new JRadioButtonMenuItem[strDlz.length]; for (int i = 0; i < strDlz.length; i++) { MI_DLZ[i] = new JRadioButtonMenuItem(strDlz[i]); MI_DLZ[i].addActionListener(this); group.add(MI_DLZ[i]); SM_DL.add(MI_DLZ[i]); } M_FROM.add(SM_DL); barre.add(M_FROM); // Assigne "barre" comme barre de menus setJMenuBar(barre); Container c = getContentPane(); // Panneau d'affichage (au centre) CenterPanel panel = new CenterPanel(); c.add(panel, BorderLayout.CENTER); // Panneau de recherche (en bas) JPanel p = new JPanel(); p.setLayout( new FlowLayout(FlowLayout.CENTER) ); txKeywords = new JTextField(15); p.add(txKeywords); btnSearch = new JButton("Rechercher"); btnSearch.addActionListener(this); p.add(btnSearch); c.add(p, BorderLayout.SOUTH); // Récupérer la résolution de l'écran (hauteur) Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); int screenHeight = d.getSize().height; setSize(300, 125); setResizable(false); setLocation(0, screenHeight - 152); show(); } public void actionPerformed(ActionEvent e) { if (btnSearch == e.getSource() ) { /* Normalement, je mettrait ici la vérification des options du sous-menu «Moteurs», mais je doit la mettre à la fin de la condition pour que tout fonctionne, je n'ai aucune idée pourquoi...
  • /
// Vérifie si option du sous-menu «Programmation» est selectionnée for (int i = 0; i < urlProg.length; i++) { if ( MI_PROG[i].isSelected() ) affichePage( urlProg[i] + txKeywords.getText() ); } // Vérifie si option du sous-menu «Jeux» est sélectionnée for (int i = 0; i < urlGamez.length; i++) { if ( MI_GAMEZ[i].isSelected() ) affichePage( urlGamez[i] + txKeywords.getText() ); } // Vérifie si option du sous-menu «Téléchargements» est sélectionnée for (int i = 0; i < urlDlz.length; i++) { if ( MI_DLZ[i].isSelected() ) affichePage( urlDlz[i] + txKeywords.getText() ); } // Vérifie si option du sous-menu «Moteurs» est sélectionnée for (int i = 0; i < urlEnginez.length; i++) { if ( MI_ENGINEZ[i].isSelected() ) affichePage( urlEnginez[i] + txKeywords.getText() ); } } // événements des options des sous-menus if (e.getSource() instanceof JRadioButtonMenuItem) { currentSrc = e.getActionCommand(); repaint(); } } private void affichePage(String urlName) { String iedir = "C:\\Program Files\\Internet Explorer\\IExplore"; // Répertoir d'Internet Explorer, modifier si ce n'est pas le chemin du navigateur que vous voulez utiliser. try { Runtime r = Runtime.getRuntime(); r.exec(iedir + " " + urlName); } catch(MalformedURLException e) {} catch(FileNotFoundException e) {} catch(IOException e) {} } public static void main(String[] args) { MoteurRecherche app = new MoteurRecherche(); app.setDefaultCloseOperation(EXIT_ON_CLOSE); } // Panneau private class CenterPanel extends JPanel { public void paint(Graphics g) { g.setColor( new Color(150, 0, 0) ); g.setFont( new Font("Ms Serif", Font.BOLD, 20) ); g.drawString(currentSrc, 20, 20); } } }

Conclusion :


C'est à vous modifier ce code selon vos préférences, c'est pas tout le monde qui aime les mêmes sites :-P

Si vous ne savez pas comment, demandez-le moi :-)

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.