السلام عليكم و صح فطوركم
أضع بين أيديكم برنامج مطور بلغة جافا مشابه لبرنامج مفكرة ويندوز
فمن منطلق التعاون بين مبرمجين العرب على دعم برمجيات الحرة
فقمت بصناعة برنامج يعالج النصوص البسيطة
فقد إستغرق لي هذا البرنامج حولي أسبوع من التطوير
فأرجو من أصحاب الخبرة في جافا ان يقيمو هذا البرنامج لأني مبتدئ في عالم جافا
موصفات البرنامج
فتح ملف نصي
حفظ ملف نصي
طباعة ملف نصي
بحث في وسط ملف نصي
و إضافة الوان و تغير حجم خط النص
و عمليات بيسطة كالقص و لصق و نسخ و......
كود البرامج
/*
* @author fekair mohammed el lamine
* @version 1.00 2008/9/15
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.print.*;
import java.io.*;
import java.util.*;
import javax.swing.border.*;
import javax.swing.filechooser.*;
import java.awt.print.Printable;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
class reche extends JFrame implements ActionListener
{
JPanel pannoRech,inter2,boutons;
JLabel labRech;
JTextField jtf;
JButton ok,annuler,visu;
JList resultat;
JScrollPane scrollRech;
Vector res;
File fichier;
BufferedReader bfr;
int numero = 0;
public reche(File f)
{
fichier = f;
setTitle("Rechercher...");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLocation(300, 150);
pannoRech = new JPanel(new BorderLayout(5, 5));
inter2 = new JPanel(new FlowLayout());
labRech = new JLabel("Rechercher : ");
jtf = new JTextField(20);
jtf.addActionListener ( this );
boutons = new JPanel(new GridLayout(3, 1, 10, 5));
ok = new JButton("Ok");
ok.addActionListener ( this );
annuler = new JButton("Annuler");
annuler.addActionListener ( this );
visu = new JButton("Visualiser");
visu.setEnabled(false);
res = new Vector();
resultat = new JList(res);
scrollRech = new JScrollPane(resultat);
inter2.add(labRech);
inter2.add(jtf);
boutons.add(ok);
boutons.add(annuler);
boutons.add(visu);
pannoRech.add(inter2, BorderLayout.CENTER);
pannoRech.add(boutons, BorderLayout.EAST);
getContentPane().setLayout(new BorderLayout(5, 5));
getContentPane().add(pannoRech, BorderLayout.NORTH);
getContentPane().add(scrollRech, BorderLayout.CENTER);
pack();
}
public void actionPerformed ( ActionEvent e )
{
Object src = e.getSource();
if (src==annuler){
setVisible(false);
}
if (src==ok) {
res.removeAllElements();
numero = 0;
resultat.updateUI();
try
{
if(!jtf.getText().equals(""))
rechercherUneChaine(jtf.getText(), fichier);
}
catch(FileNotFoundException fnfe)
{
System.err.print("filenotfound prob !!");
}
catch(IOException ioe)
{
System.err.print("io prob !!");
}
}
if (src==jtf) {
res.removeAllElements();
numero = 0;
resultat.updateUI();
try
{
if(!jtf.getText().equals("")) rechercherUneChaine(jtf.getText(), fichier);
}
catch(FileNotFoundException fnfe)
{
System.err.print("filenotfound prob !!");
}
catch(IOException ioe)
{
System.err.print("io prob !!");
}
}
}
public void rechercherUneChaine(String s, File f) throws IOException, FileNotFoundException
{
setTitle("Recherche de \" "+s+" \"");
bfr = new BufferedReader(new FileReader(f));
String ligne = bfr.readLine();
int index;
try
{
while( ligne != "" )
{
index = ligne.indexOf(s);
numero += 1;
if(index != -1)
{
res.addElement(numero+","+index+" : "+ligne);
}
ligne = bfr.readLine();
}
}
catch(NullPointerException npe) {}
jtf.selectAll();
jtf.setText("");
if (!res.isEmpty())
visu.setEnabled(true);
else
visu.setEnabled(false);
resultat.updateUI();
boutons.updateUI();
}
}
class editee extends JFrame implements ActionListener
{
static JFrame frame;
static JFileChooser fc;
static File file;
JTextField fonjtf,fonjtf1,fonjtf2;
JToolBar toolbar;
Font fo;
PrintPane printpane;
JLabel label,compte,compte1,compte2;
JTextPane jtextpane;
JButton browse,print,cutbutton,cutbutton1,cutbutton2,cutbutton3,
cutbuttonl,cutbuttonr,cutbuttonb ,cutbuttonc,cutbuttonm;
JPanel p;
JMenuBar p1;
JMenu s1,s2,s3,s4,s5;
JMenuItem s11,s12,s13,s14,s15,s16,s21,s22,s23,s24,s25,s26,s41,s51,s52;
JCheckBoxMenuItem s31;
String ch1,ch2;
public editee ()
{
setTitle("Bloc-note-java");
setSize ( 600, 400 );
setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
setLocationRelativeTo ( null );
p1=new JMenuBar();
s1=new JMenu("fechier");
s2=new JMenu("edition");
s3=new JMenu("format");
s4=new JMenu("affichage");
s5=new JMenu("aide");
s11=new JMenuItem("nouveau");
s11.addActionListener ( this );
s12=new JMenuItem("ouvrir");
s12.addActionListener ( this );
s13=new JMenuItem("enregistrer");
s13.addActionListener ( this );
s14=new JMenuItem("enregistrer sous");
s14.addActionListener ( this );
s15=new JMenuItem("imprimer");
s15.addActionListener ( this );
s16=new JMenuItem("Quitter");
s16.addActionListener ( this );
s21=new JMenuItem("couper");
s21.addActionListener ( this );
s22=new JMenuItem("copier");
s22.addActionListener ( this );
s23=new JMenuItem("coller");
s23.addActionListener ( this );
s24=new JMenuItem("supprimer");
s24.addActionListener ( this );
s25=new JMenuItem("recherche");
s25.addActionListener ( this );
s26=new JMenuItem("sélectionner tout");
s26.addActionListener ( this );
s31=new JCheckBoxMenuItem("Routeur automatique a la ligne");
s31.addActionListener ( this );
s41=new JMenuItem ("Barre d' état");
s41.addActionListener ( this );
s51=new JMenuItem("aide");
s51.addActionListener ( this );
s52=new JMenuItem("A propos du Bloce-note-java");
s52.addActionListener ( this );
p1.add(s1);
p1.add(s2);
p1.add(s3);
p1.add(s4);
p1.add(s5);
s1.add(s11);
s1.add(s12);
s1.add(s13);
s1.add(s14);
s1.add(s15);
s1.addSeparator();
s1.add(s16);
s2.add(s21);
s2.add(s22);
s2.add(s23);
s2.add(s24);
s2.add(s25);
s2.add(s26);
s3.add(s31);
s4.add(s41);
s5.add(s51);
s5.add(s52);
toolbar = new JToolBar("Toolbar", JToolBar.HORIZONTAL);
cutbutton = new JButton("G");
cutbutton.setPreferredSize ( new Dimension ( 45 , 20 ) );
cutbutton.addActionListener ( this );
toolbar.add(cutbutton);
cutbutton1 = new JButton("I");
cutbutton1.setPreferredSize ( new Dimension ( 45 , 20 ) );
cutbutton1.addActionListener ( this );
toolbar.add(cutbutton1);
cutbutton2 = new JButton("N");
cutbutton2.setPreferredSize ( new Dimension ( 45 , 20 ) );
cutbutton2.addActionListener ( this );
toolbar.add(cutbutton2);
compte = new JLabel ( "Taille de la police" );
toolbar.add(compte);
fonjtf=new JTextField( 5 );
fonjtf.addActionListener ( this );
toolbar.add(fonjtf);
fonjtf.setText ("14");
cutbuttonr= new JButton("red");
cutbuttonr.setPreferredSize ( new Dimension ( 45 , 20 ) );
cutbuttonr.addActionListener ( this );
toolbar.add(cutbuttonr);
cutbuttonb= new JButton("blue");
cutbuttonb.setPreferredSize ( new Dimension ( 45 , 20 ) );
cutbuttonb.addActionListener ( this );
toolbar.add(cutbuttonb);
cutbuttonl= new JButton("black ");
cutbuttonl.setPreferredSize ( new Dimension ( 45 , 20 ) );
cutbuttonl.addActionListener ( this );
toolbar.add(cutbuttonl);
compte1 = new JLabel ( "line" );
toolbar.add(compte1);
fonjtf1=new JTextField( 5 );
fonjtf1.addActionListener ( this );
fonjtf1.setEditable ( false );
toolbar.add(fonjtf1);
compte2 = new JLabel ( "colne" );
toolbar.add(compte2);
fonjtf2=new JTextField( 5 );
fonjtf2.addActionListener ( this );
fonjtf2.setEditable ( false );
toolbar.add(fonjtf2);
printpane = new PrintPane();
fc=new JFileChooser();
this.setLayout(new BorderLayout(5, 5));
JScrollPane jsp = new JScrollPane(printpane );
jsp.setBorder(BorderFactory.createLineBorder(Color.darkGray, 3));
this.add(jsp, BorderLayout.CENTER);
this.add(p1, BorderLayout.NORTH);
this.add(toolbar,BorderLayout.SOUTH);
}
public void actionPerformed ( ActionEvent e )
{
if ( e.getSource () instanceof JMenuItem )
{
if ( e.getActionCommand ().equals ( "Quitter" ) )
{
System.exit ( 0 );
}
if ( e.getActionCommand ().equals ( "nouveau" ) )
{
printpane.selectAll();
printpane.replaceSelection("");
setTitle("sanstitre.txt");
file = new File("sanstitre.txt");
}
if ( e.getActionCommand ().equals ( "ouvrir" ) )
{
int choix = fc.showOpenDialog(frame);
if(choix == fc.APPROVE_OPTION)
{
file = fc.getSelectedFile(); //récupère le fichier sélectionné
ouverture(file);
}
}
if ( e.getActionCommand ().equals ( "copier" ) )
{
printpane.copy();
}
if ( e.getActionCommand ().equals ( "couper" ) )
{
printpane.cut();
}
if ( e.getActionCommand ().equals ( "coller" ) )
{
printpane.paste();
printpane.repaint();
}
if ( e.getActionCommand ().equals ( "sélectionner tout" ) )
{
printpane.selectAll();
}
if ( e.getActionCommand ().equals ( "Routeur automatique a la ligne" ) )
{
if (s31.getState() == true)
printpane.setLineWrap(true);
else
printpane.setLineWrap(false);
}
if ( e.getActionCommand ().equals ( "supprimer" ) )
{
printpane.replaceSelection("");
}
if ( e.getActionCommand ().equals ( "Barre d' état" ) )
{
ch1=String.valueOf(printpane.getRows());
ch2=String.valueOf(printpane.getColumns());
fonjtf1.setText(ch1);
fonjtf2.setText(ch2);
}
if ( e.getActionCommand ().equals ( "A propos du Bloce-note-java" ) )
{
JOptionPane.showMessageDialog(null, "Bloc note 1.1\nEditeur de texte en Java\nVersion 1.1 18/09/2008 3:05 \nAuthor :fekair mohammed el lamine", "Bloc note en java",-1);
}
if ( e.getActionCommand ().equals ( "recherche" ) )
{
reche ede = new reche(file );
ede.setVisible ( true );
}
if ( e.getActionCommand ().equals ( "enregistrer" ) )
{
if (file == null)
{
int choix = fc.showSaveDialog(frame); //affichage d'une fenêtre "de fermeture"
if(choix == fc.APPROVE_OPTION)
{
File fichier = fc.getSelectedFile(); //récupère le fichier sélectionné
sauvegarde(fichier);
}
}
else sauvegarde(file);
}
if ( e.getActionCommand ().equals ( "enregistrer sous" ) )
{
int choix = fc.showSaveDialog(frame); //affichage d'une fenêtre "de fermeture"
if(choix == fc.APPROVE_OPTION)
{
File fichier = fc.getSelectedFile(); //récupère le fichier sélectionné
sauvegarde(fichier);
}
}
if ( e.getActionCommand ().equals ( "imprimer" ) )
{
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(printpane);
if(printJob.printDialog())
{
try
{
printJob.print();
}
catch (Exception pe)
{
System.out.println(pe);
}
}
}
}
if (e.getSource()==cutbutton)
{
fo = new Font("TimesRoman", Font.BOLD,Integer.parseInt(fonjtf.getText()));
printpane.setFont(fo);
}
if (e.getSource()==cutbutton1)
{
fo = new Font("TimesRoman", Font.ITALIC,Integer.parseInt(fonjtf.getText()));
printpane.setFont(fo);
}
if (e.getSource()==cutbutton2)
{
fo = new Font("TimesRoman", Font.PLAIN,Integer.parseInt(fonjtf.getText()));
printpane.setFont(fo);
}
if (e.getSource()==cutbutton3)
{
fo = new Font("TimesRoman", Font.PLAIN,Integer.parseInt(fonjtf.getText()));
printpane.setFont(fo);
}
if (e.getSource()==cutbuttonr)
{
printpane.setForeground(Color.red);
}
if (e.getSource()==cutbuttonb)
{
printpane.setForeground(Color.blue);
}
if (e.getSource()==cutbuttonl)
{
printpane.setForeground(Color.black);
}
}
public void ouverture(File f)
{
//efface la zone de texte
printpane.selectAll();
printpane.replaceSelection("");
try
{
//tampon de lecture
BufferedReader bfreader = new BufferedReader(new FileReader(f));
//lecture d'une ligne
String s = bfreader.readLine();
while(s != null)
{
//affichage de la ligne lue
printpane.append(s+"\n");
//lire une nouvelle ligne
s = bfreader.readLine();
}
//fermeture du tampon
bfreader.close();
}
catch(Exception e)
{
System.err.println("ba ya blême");
}
}
public void sauvegarde(File f)
{
try
{
//tampon d'écriture
BufferedWriter bfwriter = new BufferedWriter(new FileWriter(f));
/* écriture du texte (jta.getText()) à partir du rang 0
jusqu'à (jta.getText().length()), longueur du texte à copier */
bfwriter.write(printpane.getText(), 0, printpane.getText().length());
//fermeture du tampon d'écriture
bfwriter.close();
}
catch(Exception e)
{
System.err.println("ba ya blême");
}
}
}
public class edite
{
public static void main(String[] args)
{
editee ed = new editee ();
ed.setVisible ( true );
}
}و هذه كلاس تقوم بعملية الطباعة
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.print.*;
import java.io.*;
import java.util.*;
public class PrintPane extends JTextArea implements Printable,Serializable
{
public PrintPane()
{
}
public int print(Graphics g,PageFormat pf,int pi) throws PrinterException
{
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.black);
Dimension d = this.getSize(); //get size of document
double panelWidth = d.width; //width in pixels
double panelHeight = d.height; //height in pixels
double pageHeight = pf.getImageableHeight(); //height of printer page
double pageWidth = pf.getImageableWidth(); //width of printer page
double scale = pageWidth/panelWidth;
int totalNumPages = (int)Math.ceil(scale * panelHeight / pageHeight);
//make sure not print empty pages
if(pi >= totalNumPages)
{ return Printable.NO_SUCH_PAGE;
}
//shift Graphic to line up with beginning of print-imageable region
g2.translate(pf.getImageableX(), pf.getImageableY());
//shift Graphic to line up with beginning of next page to print
g2.translate(0f, - pi * pageHeight);
//scale the page so the width fits...
g2.scale(scale, scale);
this.paint(g2); //repaint the page for printing
return Printable.PAGE_EXISTS;
}
}أرجو التقيم من الخبراء
أعتدر لأعضاء عن لغة عربية الضعيفة لدي فتعلمون أن الجزائريين يتكلون لغة فرنسية كثير
و تجدون في هذا المرفقات البرنامج
و صح رمضانكم

