2013-03-10 4 views
0

J'ai un cadre appelé "Menu_Project". J'ai un autre cadre appelé "Main_Menu" où il a un JButton et en cliquant sur le bouton, je veux appeler le cadre "Menu_Project" à l'intérieur du JDesktopPane. J'ai essayé de suivre un tutoriel sur YouTube à ce lien: https://www.youtube.com/watch?v=TUL3qEoZkR4Ajouter un cadre externe dans un JDesktopPane Java

Quand je cours, je reçois:

" Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: adding a window to a container" 

Mes codes:

import java.awt.BorderLayout; 


public class Menu extends JFrame { 

private JPanel contentPane; 
private JDesktopPane desktopPane; 
private JButton btnNewButton; 
private Menu_Modifications_Single m1; //The Frame to be added 
/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       Menu frame = new Menu(); 
       frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the frame. 
*/ 
public Menu() { 
    initComponents(); 
    createEvents(); 
} 
private void initComponents(){ 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 1706, 995); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(contentPane); 

    btnNewButton = new JButton("Analyze Single"); 


    desktopPane = new JDesktopPane(); 
    desktopPane.setBackground(SystemColor.window); 
    GroupLayout gl_contentPane = new GroupLayout(contentPane); 
    gl_contentPane.setHorizontalGroup(
     gl_contentPane.createParallelGroup(Alignment.LEADING) 
      .addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup() 
       .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addComponent(desktopPane, GroupLayout.PREFERRED_SIZE, 1597, GroupLayout.PREFERRED_SIZE)) 
      .addGroup(gl_contentPane.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(btnNewButton) 
       .addContainerGap(1496, Short.MAX_VALUE)) 
    ); 
    gl_contentPane.setVerticalGroup(
     gl_contentPane.createParallelGroup(Alignment.LEADING) 
      .addGroup(gl_contentPane.createSequentialGroup() 
       .addComponent(btnNewButton) 
       .addGap(18) 
       .addComponent(desktopPane, GroupLayout.DEFAULT_SIZE, 823, Short.MAX_VALUE)) 
    ); 
    GroupLayout gl_desktopPane = new GroupLayout(desktopPane); 
    gl_desktopPane.setHorizontalGroup(
     gl_desktopPane.createParallelGroup(Alignment.LEADING) 
      .addGap(0, 1597, Short.MAX_VALUE) 
    ); 
    gl_desktopPane.setVerticalGroup(
     gl_desktopPane.createParallelGroup(Alignment.LEADING) 
      .addGap(0, 823, Short.MAX_VALUE) 
    ); 
    desktopPane.setLayout(gl_desktopPane); 
    contentPane.setLayout(gl_contentPane); 
} 
private void createEvents(){ 
    btnNewButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      if(m1==null){ 
      m1 = new Menu_Modifications_Single(); 
      desktopPane.add(m1); 

      } 
     } 
    }); 

} 
} 

Quelqu'un peut-il indiquer où me trompe .Après le didacticiel vidéo.

Répondre

2

Vous devez utiliser le JInternalFrame léger, au lieu de JFrame, dans un JDesktopPane.

Vous pouvez facilement modifier votre code:

if(m1==null){ 
    m1 = new Menu_Modifications_Single(); 
    desktopPane.add(m1); 
} 

Pour:

if(m1==null){ 
    m1 = new Menu_Modifications_Single(); 
    JInternalFrame iFrame = 
      new JInternalFrame("Internal Frame", true, true, true, true);  
      /* give the same contents of m1 */ 
    iFrame.setContentPane(m1.getContentPane());  
      /* additional Integer argument, required by JDesktopPane 
       (although not strictly required) */ 
    desktopPane.add(iFrame, new Integer(0));  
} 
+0

Am Regarder cet exemple: http://www.java2s.com/Tutorial/Java/0240__Swing/ AddingInternalFramestoaJDesktopPane.htm Mais Comment ajouter une instance de l'autre image. Je suis coincé –

+0

Je reçois une erreur sur cette ligne seulement: iFrame.setRootPane est surligné en rouge. –

+0

Que dit-elle? (J'espère que vous comprenez, c'est de mon cerveau ... Pas de test.) – Mordechai

Questions connexes