Pregunta

Simplemente implementé una clase que hereda Jpanel como a continuación

public class Orpanel extends JPanel {

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

Clase Orpanel está cargando imagen y ajustando su propio tamaño.

Aquí hay pregunta.

Llamar a SetContentPane de JFframe (la instancia de Orpanel) hace que funcione bien, pero cuando adjunto Orpanel al método JFrame llamado add () en lugar del setContentPane (sé que SetContentPane no es decir, de todos modos), no funciona.

Finalmente, descubrí cuando utilicé el método Add (), el componente que se agregó al método JFRame no llamado pintComponent (). Aunque llamo al método Repaint () manualmente, no se llama al método PintarComponent ().

¿Me he perdido algo? ¡Cualquier ayuda será apreciada!

gracias por adelantado. Jaeyong Shin.


Agregué código adicional.

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);
        }
    });

Todavía no funciona si el método setContentPane () reemplazado a getContentPane (). add (). No te confundas. Owpanel y Orpanel son los mismos :)

No hay solución correcta

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top