CONNEXION
  • RetourJeux
    • Sorties
    • Hit Parade
    • Les + populaires
    • Les + attendus
    • Soluces
    • Tous les Jeux
    • Gaming
  • RetourActu Gaming
    • News
    • Astuces
    • Tests
    • Previews
    • Toute l'actu gaming
  • RetourBons plans
    • Bons plans
    • Bons plans Smartphone
    • Bons plans Hardware
    • Bons plans Image et Son
    • Bons plans Amazon
    • Bons plans Cdiscount
    • Bons plans Decathlon
    • Bons plans Fnac
    • Tous les Bons plans
  • RetourJVTech
    • Actus High-Tech
    • Intelligence Artificielle
    • Smartphones
    • Mobilité urbaine
    • Hardware
    • Image et son
    • Tutoriels
    • Tests produits High-Tech
    • Guides d'achat High-Tech
    • JVTech
  • RetourCulture
    • Actus Culture
    • Culture
  • RetourVidéos
    • A la une
    • Gaming Live
    • Vidéos Tests
    • Vidéos Previews
    • Gameplay
    • Trailers
    • Chroniques
    • Replay Web TV
    • Toutes les vidéos
  • RetourForums
    • Hardware PC
    • PS5
    • Switch 2
    • Xbox Series
    • Switch
    • Pokemon pocket
    • FC 25 Ultimate Team
    • League of Legends
    • Tous les Forums
  • PC
  • PS5
  • Xbox Series
  • Switch 2
  • PS4
  • One
  • Switch
  • iOS
  • Android
  • MMO
  • RPG
  • FPS
En ce moment Genshin Impact Valhalla Breath of the wild Animal Crossing GTA 5 Red dead 2
Liste des sujets

[JAVA] Récupérer INT dans un JTextField

[Kakuzu]2
[Kakuzu]2
Niveau 34
27 mars 2014 à 22:40:19

Bonsoir,

voici la situation :

J'ai une fenetre avec 2 JFormattedTextField(NumberFormat.getIntegerInstanc
e());

Je rentre 2 nombres dans ces 2 champs et je valide à l'aide d'un bouton. Je veux que les deux valeurs remplacent la taille de la fenetre.
Exemple, je rentre 2 nombres A et B, je veux que ca fasse lorsque je clique sur le bouton "OK" -> cadre.setSize(A, B);

Voici mon programme pour l'instant

package Projet;

import java.awt.*;
import java.awt.event.*;
import java.text.*;

import javax.swing.*;
import javax.swing.border.*;

public class dimension extends JFrame implements ActionListener
{
private JPanel pan = new JPanel();
private JFormattedTextField text = new JFormattedTextField(NumberFormat.getIntegerInstanc
e());

private JFormattedTextField text2 = new JFormattedTextField(NumberFormat.getIntegerInstanc
e());

private JLabel label = new JLabel("Longueur (par défaut : 300)");
private JLabel label2 = new JLabel("Largueur (par défaut : 300)");
private JButton b1 = new JButton ("OK");

public void actionPerformed(ActionEvent e)
{
pan.setBackground(Color.ORANGE);
this.setContentPane(pan);
final JFrame cadre = new JFrame("Snake - Préambule");
cadre.setContentPane(pan);
cadre.setSize(300, 300);
cadre.setLocationRelativeTo(null);
cadre.setResizable(false);
cadre.setVisible(true);
cadre.setDefaultCloseOperation(JFrame.DISPOSE_ON_C
LOSE);

Font police = new Font("Arial", Font.BOLD, 14);
text.setFont(police);
text.setPreferredSize(new Dimension(150, 30));
text.setForeground(Color.BLUE);
text2.setPreferredSize(new Dimension(150, 30));
text2.setForeground(Color.BLUE);
text2.setFont(police);

pan.add(label);
pan.add(text);
pan.add(label2);
pan.add(text2);
pan.add(b1);

b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String valeur = (String) text.getValue();
String valeur2 = (String) text2.getValue();
int dim1 = Integer.parseInt(text);
int dim2 = Integer.parseInt(text2);
cadre.setSize(dim1, dim2);
}
});

}

}

J'ai 2 erreurs lors des dim1 et dim2 que je convertis en int (parseInt).

Multiple markers at this line
- The method parseInt(String) in the type Integer is not applicable for the arguments
(JFormattedTextField)
- Line breakpoint:dimension [line: 50] - actionPerformed(ActionEvent)

Merci d'avance, ça fait quelques jours que je bloque sur ça!

TheVocarooGuy
TheVocarooGuy
Niveau 2
28 mars 2014 à 10:53:14

http://vocaroo.com/i/s01Bf1ddrtHA

Schiffer
Schiffer
Niveau 10
28 mars 2014 à 11:03:43

Utilise un JSpinner au lieu de JTextField
http://stackoverflow.com/questions/15880844/how-to-limit-jspinner

MDA-Hack
MDA-Hack
Niveau 9
28 mars 2014 à 11:48:54

Ton erreur est là :
String valeur = (String) text.getValue();
String valeur2 = (String) text2.getValue();
int dim1 = Integer.parseInt(text);
int dim2 = Integer.parseInt(text2);

Tu déclares 2 strings contenant la valeur entré dans le JTextField, puis tu déclares 2 int en essayant de convertir l'objet JTextField lui-même à la place du String déclaré plus haut.

Coffre
Coffre
Niveau 23
28 mars 2014 à 17:59:26

TheVocarooGuy Voir le profil de TheVocarooGuy
Posté le 28 mars 2014 à 10:53:14 Avertir un administrateur
http://vocaroo.com/i/s01Bf1ddrtHA

:cute:

[Kakuzu]2
[Kakuzu]2
Niveau 34
28 mars 2014 à 19:01:23

Bonjour,

suite à vos conseils j'ai réussi à faire marcher mon programme cependant j'ai un nouveau soucis.

Lorsque je fais marcher mon programme et que je rentre mes valeurs il y a un soucis :

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String
at Projet.dimension$1.actionPerformed(dimension.java:
47)

at javax.swing.AbstractButton.fireActionPerformed(Unk
nown
Source)
at javax.swing.AbstractButton$Handler.actionPerformed
(Unknown
Source)
at javax.swing.DefaultButtonModel.fireActionPerformed
(Unknown
Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe
leased(Unknown
Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(
Unknown
Source)
at java.awt.LightweightDispatcher.processMouseEvent(U
nknown
Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unkno
wn
Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native

Method)
at java.security.ProtectionDomain$1.doIntersectionPri
vilege(Unknown
Source)
at java.security.ProtectionDomain$1.doIntersectionPri
vilege(Unknown
Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native

Method)
at java.security.ProtectionDomain$1.doIntersectionPri
vilege(Unknown
Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter
s(Unknown
Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U
nknown
Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch
y(Unknown
Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

:merci:

[Kakuzu]2
[Kakuzu]2
Niveau 34
28 mars 2014 à 20:33:06

C'est bon, j'ai changé ma méthode pour un JSpinner comme dis plus haut (je ne connaissais pas).
Plus besoin de changer de valeur en string ou autre, je peux l'avoir direct en int. En plus plus de bug pour la suite et ca marche niquel.

Merci pour votre aide!

Sous forums
  • Aide à l'achat Mac
  • Macintosh
  • Création de sites web
  • Création de Jeux
  • Linux
  • Programmation
  • Internet
  • Steam Deck
  • Hardware
La vidéo du moment