swing-使用gridlayout和jlist

xe55xuns  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(264)

抱歉我的代码太乱了。我已经添加、修改和注解了很多代码,还没有来得及清理任何代码。
我的主要问题是对如何获得一组5x5的图像感到困惑。我需要能够定期更新它,因为它是一个Map。这些更新将在一个独立于main的方法中进行,并且很难知道在一个特定的正方形中有什么图像-因此我做了一个 ArrayList 称为上一张Map。
这样我就可以删除上一个Map的元素,然后添加新的元素。这是真的很难看,虽然,所以我试图改变的方式,我这样做,用一个 JList . 我不完全理解,但我看到的很多例子中都有。
我认为现在我可以添加所有的25张图片(如图所示) JLabel 组件),然后将此列表添加到我希望它显示的面板中。
我也在尝试使用 GridLayout 这在很多例子中也有体现。我认为网格布局所发生的是,我设置了一个5x5的网格布局,然后使用 setLayout(<GridLayout name>) 我希望网格在面板上。然后我把我想在Map上的图像添加到这个网格布局中。
我的理解正确吗?我看过很多例子和解释,但我还是不确定。我也在努力理解如何将这两个想法结合起来——我必须这样做吗?如果是这样的话,我做的对吗?如果不是,我该怎么做?
到目前为止,我已经做到了:

但是,由于某些原因,图像显示为一列,而不是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);                                                                                                                                  
  }

非常感谢-如果有什么不清楚,请让我知道,我会尝试重新措辞我的问题或解释我的代码更好。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题