2017-04-06 1 views
1

Je veux ajouter ceci dans un autre JPanel mais il n'est pas visible là. Mon autre Jpanel s'appelle bottomPanel. Le composant paintComponent est censé apparaître dans le panneau inférieurpaintComponent non visible java

bottomPanel.setLayout(null); 
TestPane tp = new TestPane(); 
bottomPanel.add(tp); 

J'ai étendu le Jpanel.

public class TestPane extends JPanel { 
    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(200, 200); 
    } 


    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     Graphics2D g2d = (Graphics2D) g.create(); 
     int width = getWidth() - 100; 
     int height = getHeight() - 100; 
     int x = (getWidth() - width)/2; 
     int y = (getHeight() - height)/2; 
     g2d.setColor(Color.RED); 
     g2d.drawRect(x, y, width, height); 
     g2d.dispose(); 
    } 

} 
+1

Comment allez-vous ajouter le panneau au conteneur parent? Comment est-ce que cela devient affiché sur l'écran? – MadProgrammer

+0

bottomPanel est un autre panneau dans lequel je veux afficher ceci. BottomPanel.add (new TestPane()); assez pour ça? désolé, je suis nouveau à Java – hello12345678

+1

Nous n'avons pas besoin d'une capture d'écran, nous avons besoin du code qui reproduit votre problème. –

Répondre

3

Le problème commence par:

bottomPanel.setLayout(null); 

Les interfaces graphiques Java doivent fonctionner sur différents systèmes d'exploitation, taille d'écran, résolution d'écran, etc., en utilisant différents PLAF dans différents environnements locaux. En tant que tels, ils ne sont pas propices à la mise en page pixel parfait. Utilisez plutôt les gestionnaires de disposition, ou combinations of them avec le remplissage et les bordures de mise en page pour white space.

Et à l'avenir, un poster MCVE plutôt que plus de 300 lignes de code avec des additifs non pertinentes comme fichier d'E/S, tables, rangée trieuses etc.

2

Works pour moi ...

Working Evidence

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class Test { 

    public static void main(String[] args) { 
     new Test(); 
    } 

    public Test() { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       JFrame frame = new JFrame(); 
       JPanel outer = new JPanel(new BorderLayout()); 
       outer.add(new TestPane()); 
       frame.add(outer); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 200); 
     } 

     @Override 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      int width = getWidth() - 100; 
      int height = getHeight() - 100; 
      int x = (getWidth() - width)/2; 
      int y = (getHeight() - height)/2; 
      g2d.setColor(Color.RED); 
      g2d.drawRect(x, y, width, height); 
      g2d.dispose(); 
     } 

    } 

} 

Envisager de fournir un runnable example qui démontre votre problème

+0

Cela fonctionne mais il ne s'affiche pas dans jPanel – hello12345678

+0

@ hello12345678 Cela fonctionne très bien pour moi, montrez-moi que cela ne fonctionne pas - il est même intégré dans un autre panneau et il fonctionne toujours – MadProgrammer

+0

hey j'ai ajouté une image dans la description de la question – hello12345678