Mon interface ne paint() rien!! / debutant
Le
blackiss
Bonjour, j'ai un probleme avec une interface simple qui n'affiche pas ce qui
est dans paint. Quelqu'un peut-il me dire pourquoi?
Voici le code:
import java.awt.*;
import javax.swing.*;
import java.net.*;
public class Interface extends javax.swing.JFrame
{
JLabel titre;
JLabel smiley;
JLabel temps;
JTextField decompte;
JPanel pan;
public Interface()
{
//new Init();//appel classe qui tire au sort
JFrame frame = new JFrame("Démineur");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,400);
frame.getContentPane().setLayout(new BorderLayout());
titre = new JLabel("DEMINEUR");//north
decompte = new JTextField("10");//west
// ImageIcon icon = createImageIcon("icon_smile.gif","");
smiley = new JLabel("");//center
temps = new JLabel("Temps");//east
pan = new JPanel();pan.setPreferredSize(new Dimension(200,200));//south
getContentPane().add(titre, BorderLayout.NORTH);
getContentPane().add(decompte, BorderLayout.WEST);
getContentPane().add(smiley, BorderLayout.CENTER);
getContentPane().add(temps, BorderLayout.EAST);
getContentPane().add(pan, BorderLayout.SOUTH);
//pan.addMouseListener(new Clic());
Graphics g = pan.getGraphics();
frame.setVisible(true);
}//init
public void paint(Graphics g)
{
super.paint(g);
int largeur, hauteur, origineX=0, origineY=0;
largeur = pan.getWidth();
hauteur = pan.getHeight();
setBackground(Color.blue);
g.setColor(Color.red);
for(int i=0; i<; i++)
g.drawLine(origineX, 0, origineX, hauteur);
origineX+ ;
}//for
for(int j=0;j<;j++)
{
g.drawLine(0, origineY, largeur, origineY);
origineY+ ;
}//for
g.drawString("hello",15,15);
}//paint
Avez vous une idée de ce qui bloque?
est dans paint. Quelqu'un peut-il me dire pourquoi?
Voici le code:
import java.awt.*;
import javax.swing.*;
import java.net.*;
public class Interface extends javax.swing.JFrame
{
JLabel titre;
JLabel smiley;
JLabel temps;
JTextField decompte;
JPanel pan;
public Interface()
{
//new Init();//appel classe qui tire au sort
JFrame frame = new JFrame("Démineur");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,400);
frame.getContentPane().setLayout(new BorderLayout());
titre = new JLabel("DEMINEUR");//north
decompte = new JTextField("10");//west
// ImageIcon icon = createImageIcon("icon_smile.gif","");
smiley = new JLabel("");//center
temps = new JLabel("Temps");//east
pan = new JPanel();pan.setPreferredSize(new Dimension(200,200));//south
getContentPane().add(titre, BorderLayout.NORTH);
getContentPane().add(decompte, BorderLayout.WEST);
getContentPane().add(smiley, BorderLayout.CENTER);
getContentPane().add(temps, BorderLayout.EAST);
getContentPane().add(pan, BorderLayout.SOUTH);
//pan.addMouseListener(new Clic());
Graphics g = pan.getGraphics();
frame.setVisible(true);
}//init
public void paint(Graphics g)
{
super.paint(g);
int largeur, hauteur, origineX=0, origineY=0;
largeur = pan.getWidth();
hauteur = pan.getHeight();
setBackground(Color.blue);
g.setColor(Color.red);
for(int i=0; i<; i++)
g.drawLine(origineX, 0, origineX, hauteur);
origineX+ ;
}//for
for(int j=0;j<;j++)
{
g.drawLine(0, origineY, largeur, origineY);
origineY+ ;
}//for
g.drawString("hello",15,15);
}//paint
Avez vous une idée de ce qui bloque?

Poser une question


J'ai pas tout lu complètement en détail mais je pense que tu t'es bien
mélangé les pinceaux. Tu as crée une classe interface qui "extends"
JFrame, ce qui veut dire que tu disposes à son instantiation d'un objet
JFrame. Or, dans le constructeur, tu crées un autre objet JFrame. Es tu
sûr d'avoir besoin de ces deux fenêtres ou bien est ce là ton erreur ?
Parceque du coup, quand tu fais tes getContentPane(), c'est la méthode
getContentPane() de ton objet qui est appelée, ce qui veut dire que tu
ajoutes tes panels à ton instance de Interface. Alors que par contre
quand tu fais frame.xxx() c'est à ton objet "frame" que tu ajoutes des
comportements.
Et si il te manque des choses à l'affichage, c'est parceque tu ajoutes
tous tes panels à ta fenêtre Interface, alors que c'est à ta fenêtre
"frame" que tu fais setVisible(true) ;)
En conclusion, je pense qu'il y'a un objet frame en trop dans ton code.
Tiens moi au courant, bonne chance pour la suite,
Thibaut
A la place, fais une classe qui etend d'un JPanel, et la redefinis la method
"void paint(Graphics g);"
Vincent
"Vincent Cantin"