Search code examples
javaswingpaneljtextarea

swing - why is this creating two separate boxes


I apologise now if my terminology or turns of phrase aren't right, I'm new to swing. Also, apologise for the clunky and intelligent way I've tried to get this to work.

I am trying to create a single box that has a JTextArea inside it, and next this this area (still in the same box), has some buttons. However, all I've managed to do is make a box with the buttons in, and a separate box with a text area.

I have looked at many examples where people are trying to do similar things and I thought I managed to understand and implement it, but I haven't yet succeeded. If someone could point me at the part(s) of the code I'm misunderstanding I would be very grateful.

import java.awt.*;                                                                                                                                           
import java.awt.event.*;                                                                                                                                     
import java.awt.Color;                                                                                                                                       
import java.awt.event.ActionEvent;                                                                                                                           
import java.awt.event.ActionListener;                                                                                                                        

import javax.swing.JFrame;                                                                                                                                   
import javax.swing.*;                                                                                                                                        

public class GameGUI extends JFrame implements ActionListener                                                                                                
{                                                                                                                                                            
  private static final long serialVersionUID = 1L;                                                                                                         
  JPanel panel = new JPanel();                                                                                                                             
  JTextArea textArea = new JTextArea(50, 50);                                                                                                              
  JScrollPane scrollPane = new JScrollPane(textArea);                                                                                                      
  String action;                                                                                                                                           
  private static GUIClient client = new GUIClient();                                                                                                       

  JButton n = new JButton("N");                                                                                                                            
  JButton e = new JButton("E");                                                                                                                            
  JButton s = new JButton("S");                                                                                                                            
  JButton w = new JButton("W");                                                                                                                            
  JButton look = new JButton("LOOK");                                                                                                                      
  JButton pickup = new JButton("PICKUP");                                                                                                                  
  JButton hello = new JButton("HELLO");                                                                                                                    
  JButton quit = new JButton("QUIT");                                                                                                                      



  public GameGUI()                                                                                                                                         
  {                                                                                                                                                        
      textArea.setEditable(false);                                                                                                                                                                                                                                      
      panel.add(new JScrollPane(textArea));                                                                                                                
      this.setBounds(300, 300, 750, 500);                                         
      this.setVisible(true);                                                                                                                               
      this.setResizable(false);                                                                                                                            
      this.setLayout(null);                                                                                                                                

      n.setBounds(225, 25, 50, 50);                                                                                                                        
      e.setBounds(300, 100, 50, 50);                                                                                                                       
      s.setBounds(225, 175, 50, 50);                                                                                                                       
      w.setBounds(150, 100, 50, 50);                                                                                                                       
      look.setBounds(50, 362, 100, 50);                                                                                                                    
      pickup.setBounds(200, 362, 100, 50);
      hello.setBounds(350, 362, 100, 50);                                                                                                                  
      quit.setBounds(500, 362, 100, 50);                                                                                                                   

      this.add(n);                                                                                                                                         
      this.add(e);                                                                                                                                         
      this.add(s);                                                                                                                                         
      this.add(w);                                                                                                                                         
      this.add(look);                                                                                                                                      
      this.add(pickup);                                                                                                                                    
      this.add(hello);                                                                                                                                     
      this.add(quit);                                                                                                                                      

      n.addActionListener(this);                                                                                                                           
      e.addActionListener(this);                                                                                                                           
      s.addActionListener(this);                                                                                                                           
      w.addActionListener(this);                                                                                                                           
      look.addActionListener(this);                                                                                                                        
      pickup.addActionListener(this);                                                                                                                      
      hello.addActionListener(this);                                                                                                                       
      quit.addActionListener(this);                                                                                                                        

      this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);                                                                                              
      this.pack();                                                                                                                                            
      this.setVisible(true);                                                                                                                                  
  }                                                                                                                                                        

  public void displayInGUI(String fromServer)                                                                                                              
  {                                                                                                                                                        
      textArea.append(fromServer);                                                                                                                         
      textArea.setCaretPosition(textArea.getDocument().getLength());                                                                                       
  }                                                                                                                                                        

  public void actionPerformed(ActionEvent f)                                                                                                               
  {                                                                                                                                                        
      if(f.getSource()==n)                                                                                                                                 
      {                                                                                                                                                    
          action = "MOVE N";                                                                                                                               
          client.display(action);                                                                                                                          
      }                                                                                                                                                    
      else if(f.getSource()==e)                                                                                                                            
      {                                                                                                                                                    
          action = "MOVE E";                                                                                                                               
          client.display(action);                                                                                                                          
      } 
      else if(f.getSource()==s)                                                                                                                            
      {                                                                                                                                                    
          action = "MOVE S";                                                                                                                               
          client.display(action);                                                                                                                          
      }                                                                                                                                                    
      else if(f.getSource()==w)                                                                                                                            
      {                                                                                                                                                    
          action = "MOVE W";                                                                                                                               
          client.display(action);                                                                                                                          
      }                                                                                                                                                    
      else if(f.getSource()==look)                                                                                                                         
      {                                                                                                                                                    
          action = "LOOK";                                                                                                                                 
          client.display(action);                                                                                                                          
      }                                                                                                                                                    
      else if(f.getSource()==pickup)                                                                                                                       
      {                                                                                                                                                    
          action = "PICKUP";                                                                                                                               
          client.display(action);                                                                                                                          
      }                                                                                                                                                    
      else if(f.getSource()==hello)                                                                                                                        
      {                                                                                                                                                    
          action = "HELLO";                                                                                                                                
          client.display(action);                                                                                                                          
      }                                                                                                                                                    
      else if(f.getSource()==quit)                                                                                                                         
      {                                                                                                                                                    
          action = "QUIT";                                                                                                                                 
          client.display(action);                                                                                                                          
      }                                                                                                                                                    

  }                                                                                                                                                        

} 

I've used this class by creating an instance of it in GUIClient (I've also created an instance of GUIClient in this class, GameGUI, which seems really ugly but it was the only way I could think to notice when the buttons were pressed).

So, if I want to run the game, I set up the server, then run GUIClient, which creates the two boxes. GUIGame then notices when the buttons are pressed and sends this info to the client which sends it to the server.

Again, I'm sorry this is so convoluted. I hope my explanation helped clear things up but if not just let me know.

Many thanks, Elise.


Solution

  • I added a 'main()' method (and commented out the client parts) so I could run your program. I also set the Frame's size to 800x500 so it is visible. This is what I see:

    Screenshot: original

    The reason the text area is not visible is you did not add it to the frame. You will need to add the panel that contains the scrollpane that contains the textarea to the frame. You will also need to position and size it. For example:

    panel.setBounds(400, 25, 200, 200);
    this.add(panel);
    

    This produces the following:

    Screenshot: update