【问题标题】:swing - why is this creating two separate boxesswing - 为什么要创建两个单独的盒子
【发布时间】:2016-07-11 00:24:38
【问题描述】:

如果我的术语或措辞不正确,我现在道歉,我是新手。另外,为我试图让它工作的笨拙和聪明的方式道歉。

我正在尝试创建一个里面有一个 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 中创建它的一个实例来使用这个类(我还在这个类中创建了一个 GUIClient 的实例 GameGUI,它看起来真的很难看,但这是我能想到的唯一方法,当按钮被按下)。

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

再次,我很抱歉这太令人费解了。我希望我的解释有助于澄清问题,但如果不是,请告诉我。

非常感谢, 伊莉丝。

【问题讨论】:

  • 如果你在构造函数中创建一个本地的JFrame,为什么还要有GameGUI扩展JFrame
  • @flkes 抱歉,我不明白这是如何工作的,只是将不同的示例混合在一起。谢谢 - 我现在就解决这个问题。

标签: java swing panel jtextarea


【解决方案1】:

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

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

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

这会产生以下内容:

【讨论】:

    猜你喜欢
    • 2021-03-23
    • 2016-04-30
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    • 2011-02-10
    相关资源
    最近更新 更多