2010-11-26 9 views
14

L'application que je développe est un jeu. Ce que je veux faire, c'est avoir des JPanels qui apparaissent dans le JFrame, comme une fenêtre de texte ou de message, puis disparaissent quand ils ne sont plus utilisés. J'ai conçu ces JPanels dans Netbeans comme des classes externes et je veux pouvoir les appeler dans une méthode actionPerformed(). JOptionPanes ou d'autres boîtes de dialogue contextuelles ne sont pas une option, car ils détournent l'attention du jeu. J'ai aussi vu quelqu'un suggérer un CardLayout dans une question similaire. Ce n'est pas ce que je veux parce que je n'essaie pas seulement d'échanger les vitres. Ils devraient partir quand le programme leur dit de le faire. Comment ferais-je cela, disons en le liant à une action JButton?Comment afficher/masquer JPanels dans un JFrame?

+0

+1 à votre question ... Les jeux ont souvent leur propre interface utilisateur personnalisée. Il n'y a pas beaucoup de jeux qui utilisent l'un des Java look'n'feel, du moins pas ceux qui sont beaux. Quel genre de jeu faites-vous? Quelle plate-forme (s) ciblez-vous? (évidemment pas l'iPhone/iPad) * [Disclaimer: Je travaillais professionnellement dans l'industrie du jeu vidéo] *. – SyntaxT3rr0r

+0

Merci! C'est un jeu très simple, c'est un RPG à la première personne qui utilise des images statiques comme des scènes. J'espère également comprendre comment charger plusieurs images sur l'écran. Peut-être que cette question répondra à cela aussi. – aharlow

Répondre

21

Vous pouvez masquer un JPanel en appelant le setVisible(false). Par exemple:

public static void main(String args[]){ 
    JFrame f = new JFrame(); 
    f.setLayout(new BorderLayout()); 
    final JPanel p = new JPanel(); 
    p.add(new JLabel("A Panel")); 
    f.add(p, BorderLayout.CENTER); 

    //create a button which will hide the panel when clicked. 
    JButton b = new JButton("HIDE"); 
    b.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
       p.setVisible(false); 
     } 
    }); 

    f.add(b,BorderLayout.SOUTH); 
    f.pack(); 
    f.setVisible(true); 
} 
+0

Je suppose que c'était aussi simple que ça! – aharlow

+0

Il n'y a qu'un seul problème, changez p.setVisible (false); par f.setVisible (faux); masquer le panneau. – bestyasser

1
/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/* 
* Style1.java 
* 
* Created on May 5, 2011, 6:31:16 AM 
*/ 
package Test; 

import javax.swing.JButton; 
import javax.swing.JFileChooser; 
import javax.swing.JOptionPane; 

/** 
* 
* @author Sameera 
*/ 
public class Style2 extends javax.swing.JFrame { 

    /** Creates new form Style1 */ 
    public Style2() { 
     initComponents(); 
    } 

    /** This method is called from within the constructor to 
    * initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is 
    * always regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

     jPanel1 = new javax.swing.JPanel(); 
     cmd_SH = new javax.swing.JButton(); 
     pnl_2 = new javax.swing.JPanel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); 

     cmd_SH.setText("Hide"); 
     cmd_SH.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       cmd_SHActionPerformed(evt); 
      } 
     }); 

     javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
     jPanel1.setLayout(jPanel1Layout); 
     jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() 
       .addContainerGap(558, Short.MAX_VALUE) 
       .addComponent(cmd_SH) 
       .addContainerGap()) 
     ); 
     jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() 
       .addContainerGap(236, Short.MAX_VALUE) 
       .addComponent(cmd_SH) 
       .addContainerGap()) 
     ); 

     pnl_2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); 

     javax.swing.GroupLayout pnl_2Layout = new javax.swing.GroupLayout(pnl_2); 
     pnl_2.setLayout(pnl_2Layout); 
     pnl_2Layout.setHorizontalGroup(
      pnl_2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 621, Short.MAX_VALUE) 
     ); 
     pnl_2Layout.setVerticalGroup(
      pnl_2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 270, Short.MAX_VALUE) 
     ); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
        .addComponent(pnl_2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
       .addContainerGap()) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
       .addComponent(pnl_2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(17, Short.MAX_VALUE)) 
     ); 

     pack(); 
    }// </editor-fold> 

    private void cmd_SHActionPerformed(java.awt.event.ActionEvent evt) {          


     System.out.println(evt.getActionCommand()); 
     if (evt.getActionCommand().equals("Hide")) { 
      pnl_2.setVisible(false); 
      cmd_SH.setText("Show"); 
      this.setSize(643, 294); 
      this.pack(); 


     } 
     if (evt.getActionCommand().equals("Show")) { 
      pnl_2.setVisible(true); 
      cmd_SH.setText("Hide"); 
      this.setSize(643, 583); 
      this.pack();  
     } 
    }          

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 

      public void run() { 
       new Style1().setVisible(true); 
      } 
     }); 
    } 

    // Variables declaration - do not modify 
    private javax.swing.JButton cmd_SH; 
    private javax.swing.JPanel jPanel1; 
    private javax.swing.JPanel pnl_2; 
    // End of variables declaration 
} 
+4

Veuillez ne pas fournir de réponses uniquement au code. –

0

Appel parent.remove(panel), où parent est le conteneur que vous voulez que le cadre et panel est le panneau que vous souhaitez ajouter.

Questions connexes