2010-11-26 96 views
14

我正在開發的應用程序是一款遊戲。我想要做的是在JFrame中顯示JPanel,就像文本或消息窗口一樣,然後在不再使用時消失。我在Netbeans中將這些JPanel設計爲外部類,並希望能夠在actionPerformed()方法中調用它們。 JOptionPanes或其他彈出對話框不是一個選項,因爲它們將焦點從遊戲中移開。我還看到有人在類似的問題中提出了CardLayout。這不是我想要的,因爲我不只是試圖交換窗格。當程序告訴他們時,他們應該離開。如何將它綁定到JButton Action?如何顯示/隱藏JFrame中的JPanel?

+0

+1對你的問題...遊戲往往有他們自己的自定義用戶界面。那裏沒有太多的遊戲使用任何Java look'n'feel,至少看起來不太好看。你在做什麼樣的比賽?哪個平臺是你的目標? (顯然不是iPhone/iPad)* [免責聲明:我曾經在視頻遊戲行業專業工作] *。 – SyntaxT3rr0r 2010-11-26 16:03:13

+0

謝謝!這是一個非常簡單的遊戲,它是第一個使用靜態圖像作爲場景的RPG遊戲。我也希望弄清楚如何將多個圖像加載到屏幕上。也許這個問題也會回答。 – aharlow 2010-11-26 16:06:27

回答

21

您可以通過致電setVisible(false)來隱藏JPanel。例如:

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

我想這很簡單! – aharlow 2010-11-27 03:15:59

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 
} 
0

呼叫parent.remove(panel),其中parent是要在框架和panel是你要添加的面板容器。