Enregister sous, Ouvrir, ...

Résolu
alonsyl Messages postés 348 Date d'inscription mardi 6 avril 2004 Statut Membre Dernière intervention 6 novembre 2008 - 13 déc. 2005 à 21:22
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:20
bonjour,

existe t'il en java 1 classe qui genere automatiquement les boites "Enregister sous", "Ouvrir", ... ?

merci a vous,

alonsyl

3 réponses

cs_ducheseb Messages postés 344 Date d'inscription mardi 18 mai 2004 Statut Membre Dernière intervention 23 juin 2006 9
13 déc. 2005 à 21:37
C'est JFileChooser

"A game is a series of interesting choices." Sid Meier
3
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);

}

}
0
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:20
et voici la classe QuitDialog qu on appel si l utilisateur clic sur exit
dans le menu file
il faut la compiler dans le bin avant de compiler la classe file
Bon courage a vous.

import java.awt.*;


public class QuitDialog extends Dialog {

void yesButton_Clicked(Event event)
{
//getParent().handleEvent(new Event(this, Event.WINDOW_DESTROY, null));
System.exit(0);
}

void noButton_Clicked(Event event) {

// Clicked from noButton Hide the Dialog
hide();

}

public QuitDialog(Frame parent, boolean modal)
{
super(parent, modal);
setLayout(null);
// addNotify();
resize(insets().left + insets().right + 337,insets().top + insets().bottom + 135);
yesButton = new java.awt.Button(" Yes ");
yesButton.reshape(insets().left + 72,insets().top + 80,79,22);
yesButton.setFont(new Font("Dialog", Font.BOLD, 12));
add(yesButton);
noButton = new java.awt.Button(" No ");
noButton.reshape(insets().left + 185,insets().top + 80,79,22);
noButton.setFont(new Font("Dialog", Font.BOLD, 12));
add(noButton);
label1 = new java.awt.Label("Do you really want to quit?");
label1.reshape(insets().left + 85,insets().top +40,180,23);
add(label1);
setTitle("Quitter l'application ");
setResizable(false);
}

public QuitDialog(Frame parent, String title, boolean modal) {
this(parent, modal);
setTitle(title);
}

public synchronized void show() {
Rectangle bounds = getParent().bounds();
Rectangle abounds = bounds();

move(bounds.x + (bounds.width - abounds.width)/ 2,
bounds.y + (bounds.height - abounds.height)/2);

super.show();
}

public boolean handleEvent(Event event) {
if(event.id == Event.WINDOW_DESTROY) {
hide();
return true;
}
if (event.target noButton && event.id Event.ACTION_EVENT) {
noButton_Clicked(event);
}
if (event.target yesButton && event.id Event.ACTION_EVENT) {
yesButton_Clicked(event);
}
return super.handleEvent(event);
}

//Déclarations
Button yesButton;
Button noButton;
Label label1;

}
0
Rejoignez-nous