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();
}
}
Comment allez-vous ajouter le panneau au conteneur parent? Comment est-ce que cela devient affiché sur l'écran? – MadProgrammer
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
Nous n'avons pas besoin d'une capture d'écran, nous avons besoin du code qui reproduit votre problème. –