Maldito JDesktopPane e JInternalFrame

7 respostas
S
Galera, eu soh kero fazer um menu q tenha algo do tipo Adicionar, e toda a vez q eu aperto, apareca uma JInternalFrame. Pq meu algortimo nao funfa?

import javax.swing.<em>;

import java.awt.event.</em>;

import java.awt.*;
public class DesktopTest extends JFrame {	

public DesktopTest() {

super( Usando o JDesktop Pane);
JMenuBar bar = new JMenuBar();
	JMenu addMenu = new JMenu("Adicionar");
	JMenuItem newFrame = new JMenuItem("Frames Internos");
	addMenu.add(newFrame);
	setJMenuBar( bar);
	JPanel a = new JPanel();
	a.setLayout(new FlowLayout());
	getContentPane().add(a);
	a.add(bar);
	
			
	final JDesktopPane desktop = new JDesktopPane();
	getContentPane().add(desktop);		
	desktop.add(addMenu);	
	
	newFrame.addActionListener (
		new ActionListener() {
			public void actionPerformed (ActionEvent e) {
				JInternalFrame frame = new JInternalFrame(
					"Frame Interno", true, true, true, true);
				Container c =  frame.getContentPane();					
				MyJPanel panel = new MyJPanel();
				
				c.add(panel, BorderLayout.CENTER);
				frame.setSize(200,250);
				desktop.add(frame);
			}
		}
	);
	setSize(500,400);
	show();
}	
	public static void main(String args[]) {
		DesktopTest a = new DesktopTest();
		a.addWindowListener(
			new WindowAdapter() {
				public void windowClosing(WindowEvent e)  {
					System.exit(0);
				}
			}
		);
	}
}


class MyJPanel extends JPanel {
	private ImageIcon imgIcon;
	public MyJPanel() {
		imgIcon = new ImageIcon("sd.gif");
	}
	
	public void paintComponent(Graphics g) {
		imgIcon.paintIcon(this,g,0,0);
	}
}

[1] Qual a diferenca dessa linha para esta: Container c = getContentPane();
c.add(desktop);

Elas se comportam diferente no programa.

Kem puder ajudar, fico grato.

PS - Meu primeiro topico :wink:

7 Respostas

luiz_ross

Bem vindo ao GUJ!!!

Cara  uma olhada nesse tópico aqui que ele tem o vc precisa.
    Qualquer dúvida posta ai!!!

http://www.guj.com.br/forum/viewtopic.php?t=1671&highlight=jinternalframe

S

“luiz_ross”:
Bem vindo ao GUJ!!!

Cara  uma olhada nesse tópico aqui que ele tem o vc precisa.
    Qualquer dúvida posta ai!!!

http://www.guj.com.br/forum/viewtopic.php?t=1671&highlight=jinternalframe


vlw mininu :lol:

S

nossa cara, vc nao gosta de da import nao? hehhehehe
to estudando aki teu codigo, tentando entender.
qualquer coisa, volto aki

S

Consegui! Mas to tendo uns problemas aki
1 - O JInternalFrame vem maximizado e nao aceita nada mais do q isso… eu coloquei nos atributos da JInternalFrame tudo true
2 - Por que a criacao do seu menu funciona e a minha nao? hehehe
Olha o meu MENU :

JMenuBar bar = new JMenuBar();

JMenu addMenu = new JMenu(Adicionar);

JMenuItem newFrame = new JMenuItem(Frames Internos);

addMenu.add(newFrame);

setJMenuBar(bar);

Olha o seu MENU:

jMenuBar1 = new javax.swing.JMenuBar();

jMenu1 = new javax.swing.JMenu();

jMenuItem1 = new javax.swing.JMenuItem();

jMenu1.setText(“Menu”);

jMenu1.add(jMenuItem1);

jMenuBar1.add(jMenu1);

setJMenuBar(jMenuBar1);

private javax.swing.JInternalFrame jInternalFrame1;

private javax.swing.JDesktopPane jDesktopPane1;

private javax.swing.JMenuItem jMenuItem1;

private javax.swing.JMenuBar jMenuBar1;

private javax.swing.JMenu jMenu1;

Pra mim eh a mesma coisa, mas o seu menu aparece e o meu nao :frowning:

3 - Qual a diferenca de: getContentPane().add(objeto);

Para essa: Container c = getContentPane();

c.add(objeto);

Essas linhas funcionam diferentem.

VLW GENTE!!!

own_prison

Felipe... vc naum sabe o que é identação naum???

dá uma olhada no seu codigo como ficou....
import javax.swing.*; 
import java.awt.event.*; 
import java.awt.*; 

public class DesktopTest extends JFrame &#123; 
	public DesktopTest&#40;&#41; &#123; 
		super&#40;&quot;Usando o JDesktop Pane&quot;&#41;; 
		JMenuBar bar = new JMenuBar&#40;&#41;; 
		JMenu addMenu = new JMenu&#40;&quot;Adicionar&quot;&#41;; 
		JMenuItem newFrame = new JMenuItem&#40;&quot;Frames Internos&quot;&#41;; 
		JPanel a = new JPanel&#40;&#41;;
		addMenu.add&#40;newFrame&#41;; 
		bar.add&#40;addMenu&#41;;
		a.setLayout&#40;new FlowLayout&#40;&#41;&#41;; 
		getContentPane&#40;&#41;.add&#40;a&#41;; 
		addMenu.add&#40;newFrame&#41;;
		setJMenuBar&#40;bar&#41;;
		
		
		final JDesktopPane desktop = new JDesktopPane&#40;&#41;; 
		this.setContentPane&#40;desktop&#41;; 
		 
		
		newFrame.addActionListener &#40;new ActionListener&#40;&#41; &#123; 
			public void actionPerformed &#40;ActionEvent e&#41; &#123; 
				JInternalFrame frame = new JInternalFrame&#40;&quot;Frame Interno&quot;, true, true, true, true&#41;; 
				Container c = frame.getContentPane&#40;&#41;; 
				MyJPanel panel = new MyJPanel&#40;&#41;; 
				c.add&#40;panel, BorderLayout.CENTER&#41;; 
				frame.setSize&#40;200,250&#41;; 
				frame.setOpaque&#40;true&#41;;
				frame.show&#40;&#41;;
				desktop.add&#40;frame&#41;; 
				
			&#125; 
			&#125; 
			&#41;; 
		setSize&#40;500,400&#41;; 
		show&#40;&#41;; 
&#125; 

public static void main&#40;String args&#91;&#93;&#41; &#123; 
	DesktopTest a = new DesktopTest&#40;&#41;; 
	a.addWindowListener&#40;new WindowAdapter&#40;&#41; &#123; 
		public void windowClosing&#40;WindowEvent e&#41; &#123; 
			System.exit&#40;0&#41;; 
		&#125; 
		&#125; 
		&#41;;
&#125; 
&#125;

CAJÚ

richardpeder

acho que o own_prison passou a ideia cara…mas vou reforçar, e caso ele nao tenha passado, vou passar…
Para que vc possa criar JInternalFrame’s dinamicamente vc deve colocar um ouvinte no seu JMenuItem, e, a cada vez que o usuario clicar, instancia-se um novo JInternalFrame…
Algo assim…

jmenuitem.addActionListener&#40;new ActionListener&#40;&#41;
&#123;
    public void actionPerformed&#40;ActionEvent e&#41;
    &#123;
        JInternalFrame internal = new JInternalFrame&#40;"Internal Adicionado",true,true,true,true&#41;;
    //seta o internalframe no desktop
    .......
   &#125;
&#125;&#41;;

Bom, na verdade, apenas esboçei minha ideia acima…vc pode partir dela para que consiga resolver o problema…ou ate mesmo fazer o que os amigos propuseram anteriormente. :wink:

ate mais…

S
"own_prison":
Felipe... vc naum sabe o que é identação naum??? dá uma olhada no seu codigo como ficou....
import javax.swing.*; 
import java.awt.event.*; 
import java.awt.*; 

public class DesktopTest extends JFrame &#123; 
	public DesktopTest&#40;&#41; &#123; 
		super&#40;&quot;Usando o JDesktop Pane&quot;&#41;; 
		JMenuBar bar = new JMenuBar&#40;&#41;; 
		JMenu addMenu = new JMenu&#40;&quot;Adicionar&quot;&#41;; 
		JMenuItem newFrame = new JMenuItem&#40;&quot;Frames Internos&quot;&#41;; 
		JPanel a = new JPanel&#40;&#41;;
		addMenu.add&#40;newFrame&#41;; 
		bar.add&#40;addMenu&#41;;
		a.setLayout&#40;new FlowLayout&#40;&#41;&#41;; 
		getContentPane&#40;&#41;.add&#40;a&#41;; 
		addMenu.add&#40;newFrame&#41;;
		setJMenuBar&#40;bar&#41;;
		
		
		final JDesktopPane desktop = new JDesktopPane&#40;&#41;; 
		this.setContentPane&#40;desktop&#41;; 
		 
		
		newFrame.addActionListener &#40;new ActionListener&#40;&#41; &#123; 
			public void actionPerformed &#40;ActionEvent e&#41; &#123; 
				JInternalFrame frame = new JInternalFrame&#40;&quot;Frame Interno&quot;, true, true, true, true&#41;; 
				Container c = frame.getContentPane&#40;&#41;; 
				MyJPanel panel = new MyJPanel&#40;&#41;; 
				c.add&#40;panel, BorderLayout.CENTER&#41;; 
				frame.setSize&#40;200,250&#41;; 
				frame.setOpaque&#40;true&#41;;
				frame.show&#40;&#41;;
				desktop.add&#40;frame&#41;; 
				
			&#125; 
			&#125; 
			&#41;; 
		setSize&#40;500,400&#41;; 
		show&#40;&#41;; 
&#125; 

public static void main&#40;String args&#91;&#93;&#41; &#123; 
	DesktopTest a = new DesktopTest&#40;&#41;; 
	a.addWindowListener&#40;new WindowAdapter&#40;&#41; &#123; 
		public void windowClosing&#40;WindowEvent e&#41; &#123; 
			System.exit&#40;0&#41;; 
		&#125; 
		&#125; 
		&#41;;
&#125; 
&#125;

CAJÚ

Cajú amore mio. HAUHUEHUHE
Vlw pelas dicas. Obrigado a todos! :P

Criado 9 de abril de 2003
Ultima resposta 10 de abr. de 2003
Respostas 7
Participantes 4