FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Notepad in java

 


pollux1er
I have download a Notepad made with java but it does not works rapidly. It's too slow, and i am wodering if my system is not working well with Jdk6.

This is the code:

Moteur.java

import java.awt.Event;
import java.awt.event.*;
import java.io.*;
import javax.swing.filechooser.*;
import javax.swing.*;
import java.awt.*;

//classe Moteur contenant les choses a faire lors des evenements
public class Moteur implements ActionListener{

Notepad parent;

//Constructeur
Moteur(Notepad parent)
{
this.parent = parent;
}

//Quand on appuie sur un bouton
public void actionPerformed(ActionEvent eve)
{
//recupere la source
Object source = eve.getSource();
//si la source est le bouton enregistrer de la classe Notepad
if(source == parent.enregistrer)
{
//on ouvre une boite de dialogue
JFileChooser fichier = new JFileChooser();
fichier.setFileFilter(null);
int val = fichier.showOpenDialog(null);
if(val == JFileChooser.APPROVE_OPTION)
{
RandomAccessFile fic = null;
try
{
//on ecrit dans un fichier ce qui est ecrit dans le champ "texte" de
//la classe Notepad
fic = new RandomAccessFile(fichier.getSelectedFile(), "rw");
fic.writeBytes(parent.texte.getText());
parent.setTitle(fichier.getSelectedFile().getName()+" - Bloc-Note -- JAVA");
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try
{
fic.close();
}
catch(IOException e1)
{
e1.printStackTrace();
}
}
}
}
//Si le bouton cliqué est "ouvrir"
else if(source == parent.ouvrir)
{
//on ouvre une boit de dialogue
JFileChooser fichier = new JFileChooser();
fichier.setFileFilter(null);
int val = fichier.showOpenDialog(null);
if(val == JFileChooser.APPROVE_OPTION)
{
RandomAccessFile fic = null;
try
{
fic = new RandomAccessFile(fichier.getSelectedFile(),"rw");
String lignes = "";
while(true)
{
if(fic.readLine() == "null")
{
break;
}
else
{
//on lit une lign du fcihier en rajoutant un assag a la ligne apres
lignes += fic.readLine() + "\n";
}
}
parent.texte.setText(lignes);
parent.setTitle(fichier.getSelectedFile().getName()+" - Bloc-Note -- JAVA");
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try
{
fic.close();
}
catch(IOException e1)
{
e1.printStackTrace();
}
}
}
}
}
}

Notepad.java

import java.awt.Event;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import javax.swing.filechooser.*;
import javax.swing.*;

public class Notepad extends JFrame{


JPanel bas = new JPanel();
GridLayout dispoBas = new GridLayout(1,2);
JTextArea texte = new JTextArea(null,"",0,0);
JButton ouvrir = new JButton("Ouvrir un fichier...");
JButton enregistrer = new JButton("Enregistrer sous...");


Notepad()
{

this.setTitle("Bloc-Note -- JAVA");
bas.setLayout(dispoBas);

bas.add(ouvrir);
bas.add(enregistrer);

this.getContentPane().add(texte,"Center");
this.getContentPane().add(bas,"South");

Moteur mot = new Moteur(this);

ouvrir.addActionListener(mot);
enregistrer.addActionListener(mot);

}


public static void main(String[] args)
{
Notepad programme = new Notepad();
programme.setBounds(0,0,800,400);
programme.setVisible(true);
}
}

Can you see where is the problem with this code or it is my computer. Test to tell me what's wrong.
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.