Java: Fenetre qui se ferme

elrking096 Messages postés 1118 Date d'inscription   Statut Membre Dernière intervention   -  
 réponse -
Bonjour,
pourquoi dans mon code source j'ai bien: this.setVisible(true); , et ma fenêtre se ferme quand même toute seule à peine je la lance ?

voici mon code source:

main.java

import java.awt.Color; 
import java.awt.Graphics; 
  
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import java.lang.*; 
  
public class main extends JFrame { 
  
        public main(){ 
                 
                this.setTitle("Ma première fenêtre java"); 
                this.setSize(100, 150); 
                this.setLocationRelativeTo(null);                
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
                this.setContentPane(new panneau()); 
                 
                this.setVisible(true); 
        } 
         
}





panneau.java

import java.awt.Color; 
import java.awt.Font; 
import java.awt.GradientPaint; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Image; 
import java.io.File; 
import java.io.IOException; 
  
import javax.imageio.ImageIO; 
import javax.swing.JPanel; 
  
public class panneau extends JPanel { 
  
        public void paintComponent(Graphics g){ 
                 
                Graphics2D g2d = (Graphics2D)g;          
                GradientPaint gp = new GradientPaint(0, 0, Color.RED, 30, 30, Color.cyan, true);                 
                g2d.setPaint(gp); 
                g2d.fillRect(0, 0, this.getWidth(), this.getHeight()); 
                 
        }                
} 







Merci d'avance


A voir également:

5 réponses

choubaka Messages postés 39442 Date d'inscription   Statut Modérateur Dernière intervention   2 105
 
bonjour

essaye ceci

public main(){

super();
this.setTitle("Ma première fenêtre java");
this.setSize(100, 150);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(new panneau());

this.setVisible(true);
}

}
0
elrking096 Messages postés 1118 Date d'inscription   Statut Membre Dernière intervention   49
 
La fenetre se ferme aussi toute seule
0
elrking096 Messages postés 1118 Date d'inscription   Statut Membre Dernière intervention   49
 
Je précise qu'en bas dans la console d'eclipse j'ai:
Exception in thread "main" java.lang.NoSuchMethodError: main
0
choubaka Messages postés 39442 Date d'inscription   Statut Modérateur Dernière intervention   2 105
 
ok, je te propose de changer le nom de ta JFrame, "main" étant déjà utilisé par java, essaye par exemple avec MyJFrame...

MyJFrame.java

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.lang.*;

public class MyJFrame extends JFrame {

public MyJFrame(){

super();
this.setTitle("Ma première fenêtre java");
this.setSize(100, 150);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(new panneau());

this.setVisible(true);
}

}
0
elrking096 Messages postés 1118 Date d'inscription   Statut Membre Dernière intervention   49
 
et oui, mais maintenant dans ma console j'ai

Caused by: java.lang.ClassNotFoundException: main
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
0
choubaka Messages postés 39442 Date d'inscription   Statut Modérateur Dernière intervention   2 105
 
ok

MyJFrame.java

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.lang.*;

public class MyJFrame extends JFrame {

public static void main(String args[]){

MyJFrame theJFrame = nex MyJFrame();

}

public MyJFrame(){

super();
this.setTitle("Ma première fenêtre java");
this.setSize(100, 150);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(new panneau());

this.setVisible(true);
}

}
0
elrking096 Messages postés 1118 Date d'inscription   Statut Membre Dernière intervention   49
 
Une erreur sur: MyJFrame theJFrame = nex MyJFrame();
alors je l'enleve
et:

Toujours pareil :(

la fenetre se ferme automatiquement
0
choubaka Messages postés 39442 Date d'inscription   Statut Modérateur Dernière intervention   2 105
 
bizarre

on va essayer autre chose

MyJFrame.java

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.lang.*;

public class MyJFrame extends JFrame {

public static void main(String args[]){

new MyJFrame();

}

public MyJFrame(){

super();
this.setTitle("Ma première fenêtre java");
JLabel hello = new JLabel("Hello World");
add(hello);
this.setSize(100, 150);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(new panneau());

this.setVisible(true);
}

}
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
réponse
 
Dans ton dossier eula tu remplace false par true (dernière ligne)
0