2012-10-08 53 views
-2

我是java新手,我正在開發基本的國際象棋遊戲,目前我在玩家名稱屏幕上工作,我無法弄清楚如何隱藏我的jPanel。我得到一個錯誤說,它不能在這裏找到一個符號的代碼:爲什麼我不能隱藏我的Java類中的JFrame

package chess; 

import java.awt.Color; 


public class ChessUI extends javax.swing.JFrame { 

    public String pOneName; 
    public String pTwoName; 

    public ChessUI() { 
     initComponents(); 
     getContentPane().setBackground(Color.white); 
    } 

    /** 
    * 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() { 

     jLabel2 = new javax.swing.JLabel(); 
     jLabel3 = new javax.swing.JLabel(); 
     playerOneNameText = new javax.swing.JTextField(); 
     playerTwoNameText = new javax.swing.JTextField(); 
     playButton = new javax.swing.JButton(); 
     jLabel1 = new javax.swing.JLabel(); 
     jLabel4 = new javax.swing.JLabel(); 
     errorText = new javax.swing.JLabel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jLabel2.setText("Player One Name:"); 

     jLabel3.setText("Player Two Name:"); 

     playerOneNameText.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       playerOneNameTextActionPerformed(evt); 
      } 
     }); 

     playerTwoNameText.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       playerTwoNameTextActionPerformed(evt); 
      } 
     }); 

     playButton.setText("Play"); 
     playButton.addMouseListener(new java.awt.event.MouseAdapter() { 
      public void mouseClicked(java.awt.event.MouseEvent evt) { 
       playButtonMouseClicked(evt); 
      } 
     }); 

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

    private void playerOneNameTextActionPerformed(java.awt.event.ActionEvent evt) { 
     // TODO add your handling code here: 
    } 

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

    } 

    private void playButtonMouseClicked(java.awt.event.MouseEvent evt) { 
     if (playerOneNameText.getText().equals("")) 
     { 
      errorText.setText("One or More Player Names Missing !"); 
     } 

     if (playerTwoNameText.getText().equals("")) 
     { 
      errorText.setText("One or More Player Names Missing !"); 
     } 

     pOneName = playerOneNameText.getText(); 
     pTwoName = playerTwoNameText.getText(); 

     ChessUI.setVisibile(false); //Error Here 


    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     /* Set the Nimbus look and feel */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(ChessUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(ChessUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(ChessUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(ChessUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new ChessUI().setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration - do not modify 
    private javax.swing.JLabel errorText; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JLabel jLabel3; 
    private javax.swing.JLabel jLabel4; 
    private javax.swing.JButton playButton; 
    private javax.swing.JTextField playerOneNameText; 
    private javax.swing.JTextField playerTwoNameText; 
    // End of variables declaration 
} 

,我得到的是錯誤:非靜態方法調用setVisible(布爾)不能從靜態上下文中引用

+4

什麼是實際的錯誤信息?什麼符號?什麼線?爲什麼你讓我們猜測? –

+0

對不起第二個 – CodeLover

+0

會在一會兒加上 – CodeLover

回答

2

ChessUI.setVisibile(false)是參考Class而不是Object

Class描述了Object的一個實例。

爲了在Class上調用(非靜態)方法,必須引用該實例Class(即Object)。

在你的情況,只需撥打setVisible(false)應該工作。

雖然這可能聽起來很混亂,但可以這樣想。如果你有兩個ChessUI的實例會發生什麼?你會如何區分它們?

+0

謝謝你我所做的是改變ChessUI(類名)這個 – CodeLover

相關問題