有人可以告诉我为什么我的程序没有运行吗?

问题描述

 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 
 public class RecallStudy extends JFrame
        implements KeyListener,ActionListener
 {
    
    //CREATION OF ELEMENTS
     JLabel recallLabel;
     JTextField enterText;
     String enterTex;
     JTextField recieveText;
     JPanel displayInfo;
     GridBagConstraints constraints = new GridBagConstraints();
     
     
     public static void main (String [] args) {
         //MAKING THE WINDOW
         JFrame frame = new JFrame("RecallStudy");
            frame.setDefaultCloSEOperation( JFrame.EXIT_ON_CLOSE );
            frame.setSize(500,500);
            frame.setLocation(200,200);
            frame.setContentPane(new RecallStudy());
            frame.pack();
            frame.setVisible(true);
    
}
     //GRIDLAYOUT CONSTRUCTION
        public RecallStudy() {
            setLayout(new GridBagLayout());
            constraints.weightx = 3.0;
            constraints.weighty = 3.0;
            constraints.fill = GridBagConstraints.BOTH;
            int x,y; // for clarity
            constraints.gridheight = 2; // span two rows
            addGB(recieveText,x =2,y = 1);
            constraints.gridheight = 2; // set it back
            addGB(enterText,x = 0,y = 1);
            constraints.gridwidth = 1; // span two columns
            addGB(new JLabel("Recall"),x = 1,y = 0);
            constraints.gridwidth = 2; // set it back
            addGB(new JLabel(""),y = 0);
            constraints.gridwidth = 1;
            addGB(new JLabel(""),x = 2,y =0);
            constraints.gridwidth = 1;
            addGB(new JLabel(""),y = 2);
            constraints.gridwidth = 1;
            addGB (new JLabel(""),y =2);
            constraints.gridwidth = 1;
            
       //Set the proper restrictions on the listeners
            enterText.addKeyListener(this);
            recieveText.setEditable(false);
        }

          void addGB(Component component,int x,int y) {
            constraints.gridx = x;
            constraints.gridy = y;
            add(component,constraints);
          }
          public RecallStudy(String name) {
              super (name);
          }
      
          public void keyTyped(KeyEvent e) {
              displayInfo(e,"KEY TYPED: ");
          }
          
          /** Handle the key pressed event from the text field. */
          public void keypressed(KeyEvent e) {
              displayInfo(e,"KEY pressed: ");
          }
          
          /** Handle the key released event from the text field. */
          public void keyreleased(KeyEvent e) {
              displayInfo(e,"KEY RELEASED: ");
          }
          
          /** Handle the button click. */
          public void actionPerformed(ActionEvent e) {
              //Clear the text components.
              enterText.setText("");
              recieveText.setText(enterTex);
              
              //Return the focus to the typing area.
              enterText.requestFocusInWindow();
          }
         private void displayInfo (KeyEvent e,String keyStatus) {
             
         }

}

由于某种原因,此代码未运行...我不明白为什么...有人可以解释一下吗?当我关闭它时,我的代码没有任何错误,但是第二次尝试运行它时,它不起作用。我对缺少使它运行的组件感到困惑。我知道我还没有完成displayInfo部分,但这似乎并不是它无法运行的原因。我真的很困惑,感觉就像我以其他方式启动这段代码一样,但是这段代码不会运行。

解决方法

对于初学者:

JTextField recieveText; // declares the field,but does not create it!

所以应该是:

JTextField recieveText = new JTextField("No NullPointerException!");

然后:

JFrame frame = new JFrame("RecallStudy");
// ..
frame.setContentPane(new RecallStudy());

应该是这样的:

JFrame frame = new RecallStudy();
// ..
frame.setTitle("RecallStudy");

但是,老实说,该代码充满了“错误”。最好将它扔掉,并在完成本教程的Swing尝试后重新开始。

但这似乎不是它无法运行的原因

始终复制/粘贴错误和异常输出!