JInternalFrame

Estou desenvolvendo um aplicativo e ele tem q abrir varias janelas internas, com isso estou usando o JInternalFrame, mas está acontecendo dele não aparecer nada do JInternalFrame… eu uso ele pra abrir uma figura, mas ele aparece so uma parte da figua e tem como eu ajustar o tamanho e se clicar no canto superior direto da figura ele fecha-a… o JInternalFrame está lá., mas não aparece as suas bordas e botoes… so a figura que está no seu Pane;

cara, coloca o codigo aqui, vc falando assim, ao meu ver fica dificil, mas coloca o codigo aqui que fica facil, bele?

ate mais

[code]class novajanela
extends JInternalFrame {
String caminho;
novajanela(String titulo, String caminho, Container c){
super(titulo,true,true,true,true); this.caminho = caminho;
Container pane = getContentPane();
pane.setLayout(new FlowLayout());
setSize(140,280);
pack();
show();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
c.add(this);
}

public void paint(Graphics g){ 
	Image img = Toolkit.getDefaultToolkit().getImage(caminho);	
g.drawImage(img,10,10,this);
}

}

/////////////
//Sendo que esta classe é chamada por outra classe a qual é a janela principal…

void abrirarquivo(){ // Método da classe principal
ExampleFileFilter filtro = new ExampleFileFilter();
filtro.addExtension(“GIF”)
filtro.addExtension(“JPG”);
filtro.setDescription(“Arquivos GIF e JPG”);
dlg.setFileFilter(filtro); // dlg é o JFileChooser
int abre = dlg.showOpenDialog(this);
if(abre == dlg.APPROVE_OPTION){
String arquivo = dlg.getSelectedFile().getName();
String caminho = dlg.getSelectedFile().toString();
novajanela nj = new novajanela(arquivo, caminho, paneprinc); // onde paneprinc é o JDesktopPane da janela principal
[/code]

Ola,

Bom, vc esta fazendo errado, vc tem que sobreescrever o método paint em outro lugar, não no JInternalFrame. Por isso é q vc só ve a imagem.

Use essa classe para ver a imagem.

import javax.swing.JPanel;
import javax.swing.ImageIcon;

import java.awt.Image;
import java.awt.Graphics;

/**
 * This class shows an image that automatically resizes.
 * @version $Revision: 1.2 $
 * @author  Last modified by $Author: MLopes $
 */
public class ImagePanel extends JPanel
{
	/** the image to resize */
    Image centerImage;
    /** the initial width of the image */
    int initialWidth;
    /** the initial height of the image */
    int initialHeight;

    /** creates a new ImagePanel */
    public ImagePanel()
    {
    	super();
    }

	/**
	 * creates a new ImagePanel
	 * @param centerImage the center image to show
	 */
    public ImagePanel(ImageIcon centerImage)
    {
    	setImage(centerImage);
    }

	/**
	 * paints this component
	 * @param g the Graphics to paint the image
	 */
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        int widthFactor = this.getWidth() / initialWidth;
        int higthFactor = this.getHeight() / initialHeight;

        //calculate factor

        int factor = Math.min(widthFactor, higthFactor);

        if (factor < 1) {
        	factor = 1;
        }

        int newWidth = initialWidth * factor;
        int newHight = initialHeight * factor;

        // center image

		int imageX = (this.getWidth() - newWidth) / 2;
		int imageY = (this.getHeight() - newHight) / 2;

        g.drawImage(centerImage, imageX, imageY, newWidth, newHight, this);
    }

	/**
	 * returns the center image
	 * @return the center image
	 */
    public Image getImage()
    {
    	return centerImage;
    }

	/**
	 * sets the center image
	 * @param newImage the center image
	 */
    public void setImage(ImageIcon newImage)
    {
        this.centerImage = newImage.getImage();
        initialWidth = centerImage.getWidth(null);
        initialHeight = centerImage.getHeight(null);
    	this.repaint();
    }

Outra coisa, vc deve fazer qualquer operação swing antes de chamar o setVisible(), ou seja, mude o código para:

class novajanela
   extends JInternalFrame {
   String caminho;
   novajanela(String titulo, String caminho, Container c){
      super(titulo,true,true,true,true);             
      this.caminho = caminho;
      Container pane = getContentPane();
      pane.setLayout(new BorderLayout());
      pane.add(new ImagePanel(Toolkit.getDefaultToolkit().getImage(caminho));
      setSize(300,280);
      pack();
      setDefaultCloseOperation(DISPOSE_ON_CLOSE);
      c.add(this);
      setVisible(true);
   } 

Deve funcionar… :slight_smile:

[]'s

Note que o fato do seu aplicativo ter várias janelas não quer dizer que ele necessariamente tenha que abrir JInternalFrames. Mas esse é outro assunto.

O que aconteceu com o resto do seu componente? Ele existe, mas não está sendo desenhado, vc vc sobrescreveu o método paint() dele.

Vc pode ler como o Swing desenha seus componentes aqui.

mas a ídéia básica é chamar, na ordem:
[b]- paintComponent()

  • paintBorder()
  • paintChildren()
    [/b]

Se vc sobrescrever só PaintComponent, em tese não tem problema. Mas o mais comum é o pessoal usar apenas JPanels para fazer componentes com desenhos estranhos.

[]s