-2

私はJavaが初めてで、基本的なチェスゲームに取り組んでいます。現在、プレーヤー名画面に取り組んでおり、jPanelを非表示にする方法がわかりません。シンボルが見つからないというエラーが表示されます。コードは次のとおりです。

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(boolean) は静的コンテキストから参照できません

4

1 に答える 1

2

ChessUI.setVisibile(false)Classnotへの参照Objectです。

Class、のインスタンスを記述しますObject

で(非静的)メソッドを呼び出すには、そのインスタンス(つまり)Classを参照する必要があります。ClassObject

あなたの場合、単に呼び出すだけsetVisible(false)でうまくいくはずです。

これはおそらく紛らわしいように聞こえますが、このように考えてください。2つのインスタンスがある場合はどうなりますChessUIか?それらをどのように区別しますか?

于 2012-10-08T00:30:24.563 に答える