[java]passage de donnée pour action
Résolu/Fermé
hatonjan
Messages postés
7
Date d'inscription
mardi 28 août 2007
Statut
Membre
Dernière intervention
31 août 2007
-
30 août 2007 à 16:29
TSniper Messages postés 778 Date d'inscription lundi 14 janvier 2008 Statut Membre Dernière intervention 15 novembre 2016 - 8 avril 2008 à 14:52
TSniper Messages postés 778 Date d'inscription lundi 14 janvier 2008 Statut Membre Dernière intervention 15 novembre 2016 - 8 avril 2008 à 14:52
A voir également:
- [java]passage de donnée pour action
- Tubidy film d'action telecharger - Télécharger - TV & Vidéo
- Waptrick java football - Télécharger - Jeux vidéo
- Cette action ne peut pas être réalisée car le fichier est ouvert dans system - Guide
- Java apk - Télécharger - Langages
- Waptrick java ✓ - Forum Mobile
7 réponses
Chacual
Messages postés
230
Date d'inscription
jeudi 28 juin 2007
Statut
Membre
Dernière intervention
15 octobre 2007
150
30 août 2007 à 16:31
30 août 2007 à 16:31
Tu n'arrives pas à récupérer le contenu de tes JTextField, c'est ça ?
hatonjan
Messages postés
7
Date d'inscription
mardi 28 août 2007
Statut
Membre
Dernière intervention
31 août 2007
1
30 août 2007 à 16:59
30 août 2007 à 16:59
Exactement car a priori dans la partie actionPerformed j''arrive pas à lui passer les argument dans les textfields.
S'il faut je mettrait le code complet ça se trouve j'ai une mauvaise approche de la chose
S'il faut je mettrait le code complet ça se trouve j'ai une mauvaise approche de la chose
Chacual
Messages postés
230
Date d'inscription
jeudi 28 juin 2007
Statut
Membre
Dernière intervention
15 octobre 2007
150
30 août 2007 à 17:02
30 août 2007 à 17:02
Tes TextFields doivent être déclarés en variables de classe de ton UI.
A partir de là, comme ta fonction actionPerformed est définie dans cette classe, tu as juste à utiliser les TextFields dans ta fonction en faisant textField.getText()
A partir de là, comme ta fonction actionPerformed est définie dans cette classe, tu as juste à utiliser les TextFields dans ta fonction en faisant textField.getText()
hatonjan
Messages postés
7
Date d'inscription
mardi 28 août 2007
Statut
Membre
Dernière intervention
31 août 2007
1
30 août 2007 à 17:27
30 août 2007 à 17:27
Je vois l'idée mais tu aimerais faire comment plus exactement?
Car là j'admets que mon neurone ne suit plus trop :s
ci joint le code actuel :
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
class BoxLayoutFrame extends JFrame {//implements ActionListener
public BoxLayoutFrame ()
{
setTitle("BoxLayout Test");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
JLabel longueurLabel = new JLabel("longueur : ");
JTextField longueurText = new JTextField(10);
longueurText.setMaximumSize(longueurText.getPreferredSize());
Box hBox1 = Box.createHorizontalBox();
hBox1.add(longueurLabel);
hBox1.add(Box.createHorizontalStrut(5));
hBox1.add(longueurText);
JLabel largeurLabel = new JLabel("largeur : ");
JTextField largeurText = new JTextField(10);
largeurText.setMaximumSize(largeurText.getPreferredSize());
Box hBox2 = Box.createHorizontalBox();
hBox2.add(largeurLabel);
hBox2.add(Box.createHorizontalStrut(5));
hBox2.add(largeurText);
JLabel TELabel = new JLabel("tirant d'eau : ");
JTextField TEText = new JTextField(10);
TEText.setMaximumSize(TEText.getPreferredSize());
Box hBox3 = Box.createHorizontalBox();
hBox3.add(TELabel);
hBox3.add(Box.createHorizontalStrut(5));
hBox3.add(TEText);
JLabel longitudeLabel = new JLabel("longitude : ");
JTextField longitudeText = new JTextField(10);
longitudeText.setMaximumSize(longitudeText.getPreferredSize());
Box hBox4 = Box.createHorizontalBox();
hBox4.add(longitudeLabel);
hBox4.add(Box.createHorizontalStrut(5));
hBox4.add(longitudeText);
JLabel latitudeLabel = new JLabel("latitude : ");
JTextField latitudeText = new JTextField(10);
latitudeText.setMaximumSize(latitudeText.getPreferredSize());
Box hBox5 = Box.createHorizontalBox();
hBox5.add(latitudeLabel);
hBox5.add(Box.createHorizontalStrut(5));
hBox5.add(latitudeText);
Box hBox6 = Box.createHorizontalBox();
JButton boutonOK = new JButton("OK");
boutonOK.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
hBox6.add(boutonOK);
hBox6.add(Box.createGlue());
JButton boutonAnnuler = new JButton("Annuler");
hBox6.add(boutonAnnuler);
boutonAnnuler.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){System.exit(0);}});
Box vBox = Box.createVerticalBox();
vBox.add(hBox1);
vBox.add(hBox2);
vBox.add(hBox3);
vBox.add(hBox4);
vBox.add(hBox5);
vBox.add(Box.createGlue());
vBox.add(hBox6);
Container c = getContentPane();
c.add(vBox,BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
static final int DEFAULT_WIDTH = 200;
static final int DEFAULT_HEIGHT = 350;
}
Car là j'admets que mon neurone ne suit plus trop :s
ci joint le code actuel :
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
class BoxLayoutFrame extends JFrame {//implements ActionListener
public BoxLayoutFrame ()
{
setTitle("BoxLayout Test");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
JLabel longueurLabel = new JLabel("longueur : ");
JTextField longueurText = new JTextField(10);
longueurText.setMaximumSize(longueurText.getPreferredSize());
Box hBox1 = Box.createHorizontalBox();
hBox1.add(longueurLabel);
hBox1.add(Box.createHorizontalStrut(5));
hBox1.add(longueurText);
JLabel largeurLabel = new JLabel("largeur : ");
JTextField largeurText = new JTextField(10);
largeurText.setMaximumSize(largeurText.getPreferredSize());
Box hBox2 = Box.createHorizontalBox();
hBox2.add(largeurLabel);
hBox2.add(Box.createHorizontalStrut(5));
hBox2.add(largeurText);
JLabel TELabel = new JLabel("tirant d'eau : ");
JTextField TEText = new JTextField(10);
TEText.setMaximumSize(TEText.getPreferredSize());
Box hBox3 = Box.createHorizontalBox();
hBox3.add(TELabel);
hBox3.add(Box.createHorizontalStrut(5));
hBox3.add(TEText);
JLabel longitudeLabel = new JLabel("longitude : ");
JTextField longitudeText = new JTextField(10);
longitudeText.setMaximumSize(longitudeText.getPreferredSize());
Box hBox4 = Box.createHorizontalBox();
hBox4.add(longitudeLabel);
hBox4.add(Box.createHorizontalStrut(5));
hBox4.add(longitudeText);
JLabel latitudeLabel = new JLabel("latitude : ");
JTextField latitudeText = new JTextField(10);
latitudeText.setMaximumSize(latitudeText.getPreferredSize());
Box hBox5 = Box.createHorizontalBox();
hBox5.add(latitudeLabel);
hBox5.add(Box.createHorizontalStrut(5));
hBox5.add(latitudeText);
Box hBox6 = Box.createHorizontalBox();
JButton boutonOK = new JButton("OK");
boutonOK.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
hBox6.add(boutonOK);
hBox6.add(Box.createGlue());
JButton boutonAnnuler = new JButton("Annuler");
hBox6.add(boutonAnnuler);
boutonAnnuler.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){System.exit(0);}});
Box vBox = Box.createVerticalBox();
vBox.add(hBox1);
vBox.add(hBox2);
vBox.add(hBox3);
vBox.add(hBox4);
vBox.add(hBox5);
vBox.add(Box.createGlue());
vBox.add(hBox6);
Container c = getContentPane();
c.add(vBox,BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
static final int DEFAULT_WIDTH = 200;
static final int DEFAULT_HEIGHT = 350;
}
Chacual
Messages postés
230
Date d'inscription
jeudi 28 juin 2007
Statut
Membre
Dernière intervention
15 octobre 2007
150
30 août 2007 à 17:32
30 août 2007 à 17:32
Je te conseille de créer ta fonction qui fait ton algo dans une autre classe (car rien à voir avec l'interface) et de faire :
boutonOK.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String longueur = longueurText.getText();
String largeur = largeurText.getText();
String TE = TEText.getText();
...
MaClasse.claculeMonAlgo(longueur, largeur, TE, ...); //Pas besoin d'instancier d'objet de type MaClasse si la fonction calculeMonAlgo est "static"
}
});
boutonOK.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String longueur = longueurText.getText();
String largeur = largeurText.getText();
String TE = TEText.getText();
...
MaClasse.claculeMonAlgo(longueur, largeur, TE, ...); //Pas besoin d'instancier d'objet de type MaClasse si la fonction calculeMonAlgo est "static"
}
});
hatonjan
Messages postés
7
Date d'inscription
mardi 28 août 2007
Statut
Membre
Dernière intervention
31 août 2007
1
30 août 2007 à 19:08
30 août 2007 à 19:08
Justement le problème est là :
Il refuse de prendre longueurText largeuText etc. dans l'actionPerformed :
erreur à la compilation :
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot refer to a non-final variable longueurText inside an inner class defined in a different method
at BoxLayoutFrame.<init>(BoxLayoutFrame.java:61)
at aide_decision.main(aide_decision.java:27)
Et je ne vois aps comment recuperer ces données justement :s
Il refuse de prendre longueurText largeuText etc. dans l'actionPerformed :
erreur à la compilation :
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot refer to a non-final variable longueurText inside an inner class defined in a different method
at BoxLayoutFrame.<init>(BoxLayoutFrame.java:61)
at aide_decision.main(aide_decision.java:27)
Et je ne vois aps comment recuperer ces données justement :s
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
Essaye de déclarer tes JTextField en variables globales, pour qu'elles soient accessibles par les classes des actionslisteners.
}
static final int DEFAULT_WIDTH = 200;
static final int DEFAULT_HEIGHT = 350;
JTextField longueurText ;
JTextField latitudeText ;
JTextField largeurText ;
JTextField longitudeText;
}
attention dans ton code quand tu initialisera les variables avec leur valeur, ne remet pas le type d'objet, au risque de les rendres locales.
}
static final int DEFAULT_WIDTH = 200;
static final int DEFAULT_HEIGHT = 350;
JTextField longueurText ;
JTextField latitudeText ;
JTextField largeurText ;
JTextField longitudeText;
}
attention dans ton code quand tu initialisera les variables avec leur valeur, ne remet pas le type d'objet, au risque de les rendres locales.
hatonjan
Messages postés
7
Date d'inscription
mardi 28 août 2007
Statut
Membre
Dernière intervention
31 août 2007
1
31 août 2007 à 15:28
31 août 2007 à 15:28
Bien vu cela fonctionne désormais.
Merci encore de cette contribution =)
Merci encore de cette contribution =)
TSniper
Messages postés
778
Date d'inscription
lundi 14 janvier 2008
Statut
Membre
Dernière intervention
15 novembre 2016
157
8 avril 2008 à 14:52
8 avril 2008 à 14:52
merci moi ausssi