Question

J'ai simplement implémenté une classe qui hérite de Jpanel comme ci-dessous

public class Orpanel extends JPanel {

....
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;
        g2d.setPaint(tpaint);
        g2d.fill(rect); 
    }
....
}

Class Orpanel charge l'image et ajuste sa propre taille.

Voici la question.

Appeler SetContentPane de JFrame (instance d'Orpanel), cela fonctionne bien, mais lorsque j'attache Orpanel à JFrame appelant la méthode Add () au lieu de setContentPane (je sais que SetContentPane n'est pas un sens attaché .. en tout cas), cela ne fonctionne pas.

Enfin, j'ai compris que lorsque j'ai utilisé la méthode Add (), le composant qui a été ajouté à JFrame n'appelle pas la méthode PaintComponent (). Même si j'appelle manuellement la méthode Repainte (), la méthode PaintComponent () n'est pas appelée.

ai-je oublié quelque chose? toute aide serait appréciée!

merci à l'avance. Jaeyong Shin.


J'ai ajouté du code supplémentaire.

public Test(OwPanel op) 
{
    super();
    Dimension monitor = Toolkit.getDefaultToolkit().getScreenSize();
    op.setBackground(Color.white);
    this.setBackground(Color.white);        
    this.setBounds(monitor.width / 2 - 200 , monitor.height / 2 - 200, 400, 400);       
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setTitle("test");      
    this.setLayout(null);
    this.getContentPane().add(op);
    //this.setContentPane(op);
    this.setVisible(true);
    this.validate();
}

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            OwPanel op = new OwPanel("d:\\java_workplace\\img\\img1.jpg", 100, 100);
            OwLabel ol = new OwLabel("d:\\java_workplace\\img\\img2.jpg", 300, 50);
            Test tst =  new Test(op);
            tst.add(ol);
        }
    });

ne fonctionne toujours pas si setContentPane () a remplacé pour getContentPane (). Add (). Ne soyez pas confus. Owpanel et Orpanel est le même :)

Pas de solution correcte

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top