swing - 为什么要创建两个单独的框

swing - why is this creating two separate boxes

如果我的术语或措辞不当,我深表歉意,我是 swing 新手。另外,为我试图使它工作的笨拙而聪明的方式道歉。

我正在尝试创建一个内部有 JTextArea 的单个框,接下来这个区域(仍然在同一个框中)有一些按钮。但是,我所要做的就是制作一个带有按钮的框,以及一个带有文本区域的单独框。

我看过许多人们尝试做类似事情的例子,我认为我设法理解并实现了它,但我还没有成功。如果有人能指出我误解的代码部分,我将不胜感激。

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);                                                                                                                          
      }                                                                                                                                                    

  }                                                                                                                                                        

} 

我通过在 GUIClient 中创建它的一个实例来使用它 class(我还在这个 class,GameGUI 中创建了一个 GUIClient 的实例,它看起来真的很难看,但它是这是我能想到的在按下按钮时注意到的唯一方法。

所以,如果我想 运行 游戏,我会设置服务器,然后 运行 GUIClient,它会创建两个框。然后 GUIGame 注意到按钮何时被按下,并将此信息发送到客户端,客户端将其发送到服务器。

再次抱歉,这太复杂了。我希望我的解释能帮助澄清问题,但如果不能让我知道。

非常感谢, 爱丽丝.

我添加了一个 'main()' 方法(并注释掉了客户端部分)所以我可以 运行 你的程序。我还将框架的大小设置为 800x500,以便它可见。这是我看到的:

文本区域不可见的原因是您没有将它添加到框架中。您需要将包含包含文本区域的滚动窗格的面板添加到框架。您还需要对其进行定位和调整大小。例如:

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

这会产生以下结果: