【问题标题】:Why can't I hide my JFrame in my java class [closed]为什么我不能在我的 java 类中隐藏我的 JFrame [关闭]
【发布时间】:2012-10-08 00:20:45
【问题描述】:

我是 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)

【问题讨论】:

  • 实际的错误信息是什么?什么符号?什么线?为什么要让我们猜测?
  • setVisibile mewthod 在哪里?
  • 这是一个错字,应该是可见的

标签: java swing netbeans ide jframe


【解决方案1】:

ChessUI.setVisibile(false) 引用 Class 而不是 Object

Class 描述了Object 的一个实例。

为了在Class 上调用(非静态)方法,您必须引用该Class 的实例(即Object)。

在您的情况下,只需调用 setVisible(false) 即可。

虽然这听起来可能令人困惑,但请这样想。如果你有两个ChessUI 实例会发生什么?你会如何区分它们?

【讨论】:

  • 谢谢你我所做的就是把 ChessUI(类名) 改成这个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-21
  • 1970-01-01
  • 1970-01-01
  • 2021-09-21
  • 1970-01-01
  • 2020-04-28
  • 1970-01-01
相关资源
最近更新 更多