infojava
Messages postés
35
Date d'inscription
vendredi 16 décembre 2005
Statut
Membre
Dernière intervention
30 mai 2006
16 déc. 2005 à 11:02
bonjour j ai la reponse a votre question dejas pret
enregistrer le code suivant dans un fichier "file.java"
compile puis execute.j espere que tu emprofitera.
parcontre moi je veut creer le menu edition(copier,coller,couper...)
je ne trouve pas commemt!!!!!!!!!
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.util.Date;
import java.io.*;
import java.util.*;
import java.lang.*;
import javax.swing.tree.*;
class fenetre extends Frame implements ActionListener{
TextArea zone = new TextArea("",5,40,TextArea.SCROLLBARS_BOTH);
TextArea zone1 = new TextArea("",5,40,TextArea.SCROLLBARS_BOTH);
FileDialog openFileDialog1 = new FileDialog(this, "Open",FileDialog.LOAD);
//les declarations de toolbar
JToolBar jToolBar1 = new JToolBar();
JButton tool_open = new JButton("",new ImageIcon("open2.jpg"));
JButton tool_save = new JButton("",new ImageIcon("save2.jpg"));
JButton tool_new = new JButton("",new ImageIcon("new2.jpg"));
public fenetre()
{
//-----------------
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { System.exit(0); }});
//------------------
aspects= UIManager.getInstalledLookAndFeels();
try{UIManager.setLookAndFeel( aspects[2].getClassName());
SwingUtilities.updateComponentTreeUI(this);
}catch(Exception e) {e.printStackTrace(); }
//---------------
//Les propriétés de la fenétre
setBounds (5,5,720,560);
setTitle(" ");
setBackground(color_back);
setLayout(null);
//-------------------------------------------------------------------
//-----------------------
//les menus
//---------------------------------------------------------
MenuBar mb = new MenuBar();
Menu m1 = new Menu(" File ");
m1.add("New");
m1.add("Open");
m1.add("Save");
m1.add("Save As...");
m1.addSeparator();
m1.add("Exit");
mb.add(m1);
setMenuBar(mb);
//----------------------------
// un toolbar
//-------------------------
jToolBar1.setBounds(new Rectangle(1,50,500,40));
jToolBar1.setBackground(color_back);
jToolBar1.add(tool_new, null);init_button(tool_new,"New");
jToolBar1.add(tool_open, null);init_button(tool_open,"Open");
jToolBar1.add(tool_save, null);init_button(tool_save,"Save");
");
jToolBar1.setFloatable(false);
add(jToolBar1, null);
//---------------------
//Les zones de textes
//----------------------------------------------------------------------
zone.setBackground(SystemColor.window);
zone.setForeground(SystemColor.windowText);
zone1.setBackground(SystemColor.window);
zone1.setForeground(SystemColor.windowText);
zone.setBounds(200,100,500,310);
zone1.setBounds(200,420,500,130);
zone1.setEditable(false);
add(zone);add(zone1);
}//fin du constructeur
//--------------------
//initiation des boutons
//------------------------------------------------------------
public void init_button(JButton b,String s)
{//
b.addActionListener(this);
b.setToolTipText(s);b.setBackground(Color.white);
b.setSize(40,40);
}//
//------------------------------------------------------------
//---------------------------------------------------------------------------------------
// Les actions des boutons:
//----------------------------
public void actionPerformed(ActionEvent e)
{//+
if ( e.getSource() == tool_open ) {//
FileDialog fd = new FileDialog(_getFrame(this),"Choose a file ", FileDialog.LOAD);
fd.show();
if (fd.getFile() == null ) return; // user pressed Cancel
File theFile = new File(fd.getDirectory(),fd.getFile());
String theText = "";
try {
FileReader fr = new FileReader(theFile);
char charar[] = new char[(int)theFile.length()];
fr.read(charar);
theText = String.valueOf(charar);
fr.close();
}
catch ( FileNotFoundException excpt0 ) {}
catch ( IOException excpt1 ) {}
zone.setText(String.valueOf(theText));
}//
//-------------------------------
if ( e.getSource() == tool_new ) {zone.setText("");zone1.setText("");}
//-------------------------------
if ( e.getSource() == tool_save){//
FileDialog fd = new FileDialog(_getFrame(this),
"Donner un nom de fichier", FileDialog.SAVE);
fd.show();
if ( fd.getFile() == null ) return; // user pressed Cancel
File theFile = new File(fd.getDirectory(),fd.getFile());
try {
FileWriter fw = new FileWriter(theFile);
fw.write(zone.getText());
fw.close();
}
catch ( IOException excpt0 ) {}
}//
}//+
//--------------------
//les actions du menu
//----------------------------------------------------------------------------------
public boolean action(Event event, Object arg) {//+
if (event.target instanceof MenuItem) {//
String label = (String) arg;
//-------
if (label.equalsIgnoreCase("Open")) {
Open_Action(event);
return true;
} else
//---------
if (label.equalsIgnoreCase("Save As...")) {
Save_Action(event);
return true;
} else
//---------
if (label.equalsIgnoreCase("Exit")) {
Exit_Action(event);
return true;
}else
//----------
if (label.equalsIgnoreCase("New")) {
zone.setText("");zone1.setText("");
return true;
}
//-----------
}//
return super.action(event, arg);
}//+
//-----------------------------------------------------------------------------
void Open_Action(Event event) {
FileDialog fd = new FileDialog(_getFrame(this),
"Choose a file", FileDialog.LOAD);
fd.show();
if( fd.getFile() == null ) return; // user pressed Cancel
File theFile = new File(fd.getDirectory(),fd.getFile());
String theText = "";
try {
FileReader fr = new FileReader(theFile);
char charar[] = new char[(int)theFile.length()];
fr.read(charar);
theText = String.valueOf(charar);
fr.close();
}
catch ( FileNotFoundException excpt0 ) {
}
catch ( IOException excpt1 ) {
}
zone.setText(String.valueOf(theText));
}
//--------------------------------------
void Save_Action(Event event)
{//
FileDialog fd = new FileDialog(_getFrame(this),
"Donner un nom de fichier", FileDialog.SAVE);
fd.show();
if( fd.getFile() == null ) return; // user pressed Cancel
File theFile = new File(fd.getDirectory(),fd.getFile());
try {
FileWriter fw = new FileWriter(theFile);
fw.write(zone.getText());
fw.close();
}
catch ( IOException excpt0 ) {}
}//
//------------------------------------
void Exit_Action(Event event) {(new QuitDialog(this, true)).show();
}
//-----------------------------------------------------------------------------------------
public static Frame _getFrame(Component comp)
{
Component c = comp;
try {
while( !(c instanceof Frame) ) c = c.getParent();
return (Frame)c;
}
catch ( NullPointerException e ) {return null;}
}
//-----------------------------------------------------------------
private UIManager.LookAndFeelInfo aspects[];
private static Container contenu,co;
//color
Color color_back = new Color(240, 225, 220);
// fonts
Font police = new Font("SansSerif",Font.BOLD, 18);
Font f1 = new Font("SansSerif",Font.BOLD, 12);
Font f = new Font("TimesRoman",Font.BOLD,48);
}
//99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
public class file extends Frame
{ public static void main (String[] args)
{ fenetre f=new fenetre();
f.setVisible(true);
f.setResizable(true);
}
}