Java Swing-使用GridLayout和JList

Java Swing-使用GridLayout和JList,java,image,swing,jlist,grid-layout,Java,Image,Swing,Jlist,Grid Layout,抱歉我的代码太乱了。我已经添加、更改和注释了很多代码,还没有抽出时间来清理任何代码 我的主要问题是对如何获得5x5组图像感到困惑。我需要能够定期更新它,因为它是一个地图。这些更新将以独立于main的方法进行,并且很难知道特定正方形中的图像是什么-因此,我制作了一个名为previousmap的ArrayList 这样我就可以删除上一个贴图的元素,然后添加新的元素。这真的很难看,所以我试图通过使用JList来改变我这样做的方式。我不完全理解它,但在我看到的很多例子中都出现了 我认为现在发生的是,我可

抱歉我的代码太乱了。我已经添加、更改和注释了很多代码,还没有抽出时间来清理任何代码

我的主要问题是对如何获得5x5组图像感到困惑。我需要能够定期更新它,因为它是一个地图。这些更新将以独立于main的方法进行,并且很难知道特定正方形中的图像是什么-因此,我制作了一个名为previousmap的
ArrayList

这样我就可以删除上一个贴图的元素,然后添加新的元素。这真的很难看,所以我试图通过使用
JList
来改变我这样做的方式。我不完全理解它,但在我看到的很多例子中都出现了

我认为现在发生的是,我可以将我的所有25个图像(如
JLabel
组件)添加到此列表中,然后将此列表添加到我希望其显示的面板中

我还尝试使用
GridLayout
,这也在很多示例中显示出来。我认为网格布局所发生的是,我设置了一个5x5网格布局,然后使用
setLayout()
到我希望网格所在的面板。然后,我将要在地图中显示的图像添加到此网格布局中

我的理解正确吗?我看过很多例子和解释,但我仍然不确定。我也在努力理解如何将这两个想法结合起来——我必须这样做吗?如果是这样的话,我的做法是否正确?如果不是,我怎么做

到目前为止,我成功地做到了:

但是,由于某些原因,图像显示为一列,而不是5x5网格。 以下是我一直在研究的相关代码:

public class GameGUI3 extends JFrame implements ActionListener                                                                                               
{                                                                                                                                                            
  private static final long serialVersionUID = 1L;                                                                                                         
  JFrame f = new JFrame("Dungeons of Doom");                                                                                                               
  JPanel textPanel = new JPanel();                                                                                                                         
  JPanel leftPanel = new JPanel(new GridBagLayout());                                                                                                      
  JPanel moveButtonPanel = new JPanel(new GridBagLayout());                                                                                                
  JPanel extraButtonPanel = new JPanel(new GridBagLayout());                                                                                               
  JPanel mapPanel = new JPanel(new GridBagLayout());                                                                                                       
  GridLayout gl = new GridLayout(5, 5);                                                                                                                    
  JTextArea textArea = new JTextArea(20, 50);                                                                                                              
  JScrollPane scrollPane = new JScrollPane(textArea);                                                                                                      
  DefaultListModel listModel = new DefaultListModel();                                                                                                     
  JList list = new JList(listModel);                                                                                                                       
  String action;                                                                                                                                           
  ArrayList<String> previousMap = new ArrayList<String>();                                                                                                 
  GridBagConstraints mapC = new GridBagConstraints();                                                                                                      
  private static GUIClient3 client = new GUIClient3();                                                                                                     

  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 GameGUI3()                                                                                                                                        
  {                                                                                                                                                        
      GridBagConstraints northC = new GridBagConstraints();                                                                                                
      GridBagConstraints eastC = new GridBagConstraints();                                                                                                 
      GridBagConstraints southC = new GridBagConstraints();                                                                                                
      GridBagConstraints westC = new GridBagConstraints();                                                                                                 
      GridBagConstraints leftTopC = new GridBagConstraints();                                                                                              
      GridBagConstraints leftBottomC = new GridBagConstraints(GridBagConstraints extraC = new GridBagConstraints();                                                                                                

      northC.insets = new Insets(10, 65, 10, 10);                                                                                                          
      eastC.insets = new Insets(10, 65, 10, 10);                                                                                                           
      southC.insets = new Insets(10, 65, 10, 10);                                                                                                          
      westC.insets = new Insets(10, 0, 10, 10);                                                                                                            
      leftTopC.insets = new Insets(10, 10, 30, 10);                                                                                                        
      mapC.insets = new Insets(30, 30, 30, 30);                                                                                                            
      extraC.insets = new Insets(10, 10, 10, 10);                                                                                                          

      f.setBounds(100, 100, 1000, 600); // (start on x-axis, start on y, width, height)                                                                    

      textPanel.setBounds(1000, 250, 500, 200);                                                                                                            
      textPanel.add(new JScrollPane(textArea));                                                                                                            

      f.getContentPane().add(leftPanel, BorderLayout.WEST);                                                                                                

      leftTopC.gridx = 0;                                                                                                                                  
      leftTopC.gridy = 0;                                                                                                                                  
      leftTopC.anchor = GridBagConstraints.FIRST_LINE_START;                                                                                               
      leftPanel.add(moveButtonPanel, leftTopC);                                                                                                            

      //mapC.gridx = 0;                                                                                                                                    
      //mapC.gridy = 1;                                                                                                                                    
      //mapC.anchor = GridBagConstraints.LINE_START;                                                                                                       
      //leftPanel.add(mapPanel, mapC);                                                                                                                     

      //mapPanel.setLayout(gl);                                                                                                                            
      leftPanel.add(mapPanel);                                                                                                                             

      leftBottomC.gridx = 0;                                                                                                                               
      leftBottomC.gridy = 2;                                                                                                                               
      leftBottomC.anchor = GridBagConstraints.LAST_LINE_START;                                                                                             
      leftPanel.add(extraButtonPanel, leftBottomC);                                                                                                        

      f.getContentPane().add(textPanel, BorderLayout.EAST);                                                                                                

      textArea.setEditable(false);                                                                                                                         

      this.setVisible(false);                                                                                                                              

      northC.gridx = 0;                                                                                                                                    
      northC.gridy = 0;                                                                                                                                    
      northC.gridwidth = 3;                                                                                                                                
      northC.anchor = GridBagConstraints.NORTH;
      moveButtonPanel.add(n, northC);                                                                                                                      

      westC.gridx = 0;                                                                                                                                     
      westC.gridy = 1;                                                                                                                                     
      westC.gridwidth = 1;                                                                                                                                 
      westC.anchor = GridBagConstraints.WEST;                                                                                                              
      moveButtonPanel.add(w, westC);                                                                                                                       

      eastC.gridx = 2;                                                                                                                                     
      eastC.gridy = 1;                                                                                                                                     
      eastC.gridwidth = 3;                                                                                                                                 
      eastC.anchor = GridBagConstraints.EAST;                                                                                                              
      moveButtonPanel.add(e, eastC);                                                                                                                       

      southC.gridx = 0;                                                                                                                                    
      southC.gridy = 2;                                                                                                                                    
      southC.gridwidth = 3;                                                                                                                                
      southC.anchor = GridBagConstraints.SOUTH;                                                                                                            
      moveButtonPanel.add(s, southC);                                                                                                                      

      mapC.gridx = 0;                                                                                                                                      
      mapC.gridy = 2;                                                                                                                                      
      //mapC.gridwidth = 10;                                                                                                                               
      //mapC.anchor = GridBagConstraints.LINE_START;                                                                                                       
      //mapPanel.setLayout(gl);                                                                                                                            
      leftPanel.add(mapPanel, mapC);                                                                                                                       

      ImageIcon HereBeDragonsIcon = new ImageIcon("HereBeDragons.jpg", "Image");                                                                           
      JLabel HereBeDragonsLabel = new JLabel(HereBeDragonsIcon);                                                                                           

      //mapPanel.add(HereBeDragonsLabel);                                                                                                                  

      int count=0;                                                                                                                                         

      for(int i=0; i<4; i++)                                                                                                                               
      {                                                                                                                                                    
          listModel.add(count++, HereBeDragonsIcon);                                                                                                       
          //listModel.addElement(HereBeDragonsIcon);                                                                                                       

          previousMap.add("HereBeDragonsLabel");                                                                                                           
          //mapPanel.add(HereBeDragonsLabel);                                                                                                              
      }                                                                                                                                                    
      //JList list = new JList(listModel);                                                                                                                 
      //mapPanel.add(list);                                                                                                                                
      mapPanel.add(list);                                                                                                                                  

      extraButtonPanel.add(look, extraC);                                                                                                                  
      extraButtonPanel.add(pickup, extraC);                                                                                                                
      extraButtonPanel.add(hello, extraC);                                                                                                                 
      extraButtonPanel.add(quit, extraC);                                                                                                                  

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

      f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);                                                                                                 
      f.pack();                                                                                                                                            
      f.setVisible(true);                                                                                                                                  
  }                                                                                                                                                        
公共类GameGUI3扩展JFrame实现ActionListener
{                                                                                                                                                            
私有静态最终长serialVersionUID=1L;
JFrame f=新JFrame(“末日之地”);
JPanel textPanel=新的JPanel();
JPanel leftPanel=newjpanel(newgridbaglayout());
JPanel moveButtonPanel=newjpanel(newgridbaglayout());
JPanel extraButtonPanel=newjpanel(newgridbaglayout());
JPanel mapPanel=newJPanel(newGridBagLayout());
GridLayout gl=新的GridLayout(5,5);
JTextArea textArea=新的JTextArea(20,50);
JScrollPane scrollPane=新的JScrollPane(textArea);
DefaultListModel listModel=新的DefaultListModel();
JList list=新的JList(listModel);
弦作用;
ArrayList previousMap=新ArrayList();
GridBagConstraints mapC=新的GridBagConstraints();
private static GUIClient3 client=new GUIClient3();
JButton n=新JButton(“n”);
JButton e=新JButton(“e”);
JButton s=新JButton(“s”);
JButton w=新JButton(“w”);
JButton look=新JButton(“look”);
JButton拾取=新JButton(“拾取”);
JButton hello=新JButton(“hello”);
JButton quit=新JButton(“quit”);
公共游戏3()
{