0
 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(""), x = 0, y = 0);
            constraints.gridwidth = 1;
            addGB(new JLabel(""), x = 2, y =0);
            constraints.gridwidth = 1;
            addGB(new JLabel(""), x = 2, y = 2);
            constraints.gridwidth = 1;
            addGB (new JLabel(""), x = 0, 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) {
             
         }

}

For some reason, this code is not running...I cannot understand why...can someone please explain? I do not get any errors on my code when I just have it down, but the second I try to run it it does not work. I am confused as to what components I am missing to make it run. I know that I have not finished the displayInfo section, but that does not seem to be the reason why it won't run. I am just really confused, I feel like I started this code the same way I do every other but this one just won't run.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • This appears to me to be a continuation of your [previous question](https://stackoverflow.com/questions/64433071/sending-a-text-from-jtextfield-to-another-jtextfield-using-the-enter-button#comment113934347_64433071). It looks like you have made some improvements on the code in that question and rather than [edit] that question, you decided to post another question. Your code gives me the impression that you need to thoroughly learn _Swing_ and that is not something you can do via questions on SO. Note that the value for `weightx` must be between `0.0` and `1.0` and not `3.0`. – Abra Oct 20 '20 at 09:31
  • On at least two of your questions related to Java & Swing, neither tag was added. To get the best 'coverage' to the people who might be able to help, don't forget to add these tags! (E.G. I tune in only to the [tag:swing] tag these days). The only reason I saw this is that occasionally I pick out a tag (like [tag:jframe]) and look for posts that lack the Swing tag. – Andrew Thompson Oct 21 '20 at 15:37
  • See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Oct 21 '20 at 15:50

1 Answers1

1

For starters:

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

So it should be:

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

Then:

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

Should be something like:

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

But honestly, that code is riddled with 'errors'. It'd be better to toss it out and start again after going through the Swing trail of the tutorial.

but that does not seem to be the reason why it won't run

Always copy/paste error and exception output!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433