2013-10-24 38 views
0

我想知道如何在選擇JList中的項目時更新JLabel。這將用於遊戲,它是遊戲的庫存面板。我希望它顯示已選擇的特定項目的itemName。裏面的玩家階級這裏的變量聲明:如何根據在JList中選擇的內容來更新JLabel

static Item equippedSword = Item.stickSword, equippedArmor = Item.clothesArmor, equippedShield = Item.noShield; 
static String equippedSwordDisplay = equippedSword.itemName, equippedArmorDisplay = equippedArmor.itemName, equippedShieldDisplay = equippedShield.itemName; 

這裏是主GUI文件(庫存JList的項目是對底部和選擇偵聽器朝向頂部):

@SuppressWarnings("serial") 
public class GuiMain extends JFrame 
{ 
//universal buttons 
private JButton storeButton, backButton = new JButton("Back"); 
//stat and room tracking 
private JLabel pstats, roomInfo; 
//fillers 
private JPanel filler1,filler2,filler3,filler4,filler5,filler6,filler7,filler8,filler9,filler10,filler11; 
//control items 
private JButton upControl, downControl, leftControl, rightControl, moveButton; 
//action listener button panels 
private JPanel controlPanel, mainPanel = new JPanel(), backPanel; 
//gameinfo items 
private JPanel gameInfoPanel; 
private JButton instructionsButton, historyButton, gameInfoButton; 
String gameHistory, gameInstructions; 
//inventory items 
private JButton inventoryButton; 
@SuppressWarnings("rawtypes") 
public JList swordList, armorList, shieldList, itemList; 
private JPanel inventoryPanel; 
private JLabel swordListLbl, armorListLbl, shieldListLbl, itemListLbl; 

Player p = new Player(); 

//main file 
public GuiMain(){ 
    super("Dungeon Crawler v 0.0.1 Created By: Jordan Savage"); 
    mainPanel.setLayout(new GridLayout(4,3)); 
    p.setStartingInventory(); 
    inventoryScheme(); 
    //mainbuttons 
    inventoryButton = new JButton("Inventory"); 
    inventoryButton.setToolTipText("Gives you access to all of your items and lets you manage them"); 

    storeButton = new JButton("Store"); 
    storeButton.setToolTipText("The marketplace where you can buy and sell items such as swords and armor"); 

    moveButton = new JButton("Move"); 
    moveButton.setToolTipText("Choose where you want to move next"); 

    gameInfoButton = new JButton("Game Instructions and History"); 
    gameInfoButton.setToolTipText("All the info for the game including instructions, version info, and contact details"); 

    //these go inside game info 
    historyButton = new JButton("Game History"); 
    instructionsButton = new JButton("Game Instructions"); 

    //stat tracking label 
    pstats = new JLabel("Character Stats: " + Player.gold + " Gold, " + Player.health + " Health, and Level is " + Player.lvl); 
    roomInfo = new JLabel("Character Postion: (" + Player.x + "," + Player.y + ") In room: " + Room.start.name); 

    //fillers for grid layouts 
    filler1 = new JPanel();filler2 = new JPanel();filler3 = new JPanel();filler4 = new JPanel();filler5 = new JPanel();filler6 = new JPanel();filler7 = new JPanel();filler7 = new JPanel();filler8 = new JPanel();filler9 = new JPanel();filler10 = new JPanel();filler11 = new JPanel(); 

    //action listeners 
    //how you move, will be disabled when move is forbidden 
    moveButton.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      controlScheme(); 
      backToMain(); 
      getContentPane().removeAll(); 
      getContentPane().add(controlPanel, BorderLayout.CENTER); 
      getContentPane().add(backPanel, BorderLayout.SOUTH); 
      getContentPane().doLayout(); 
      getContentPane().revalidate(); 
      update(getGraphics()); 
     } 
    }); 

    //information about the game 
    gameInfoButton.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      gameInfoScheme(); 
      backToMain(); 
      getContentPane().removeAll(); 
      getContentPane().add(gameInfoPanel, BorderLayout.CENTER); 
      getContentPane().add(backPanel, BorderLayout.SOUTH); 
      getContentPane().doLayout(); 
      getContentPane().revalidate(); 
      update(getGraphics()); 
     } 
    }); 

    inventoryButton.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      inventoryScheme(); 
      backToMain(); 
      getContentPane().removeAll(); 
      getContentPane().add(inventoryPanel, BorderLayout.CENTER); 
      getContentPane().add(backPanel, BorderLayout.SOUTH); 
      getContentPane().doLayout(); 
      getContentPane().revalidate(); 
      update(getGraphics()); 
     } 
    }); 

    //brings you back to the main 
    backButton.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      getContentPane().removeAll(); 
      getContentPane().add(mainPanel); 
      getContentPane().doLayout(); 
      update(getGraphics()); 
     } 
    }); 

    //both inside gameInfo 
    //tells history of game 
    historyButton.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      gameInfoScheme(); 
      backToMain(); 
      JOptionPane.showMessageDialog(null, String.format(gameHistory, e.getActionCommand())); 
      getContentPane().removeAll(); 
      getContentPane().add(gameInfoPanel, BorderLayout.CENTER); 
      getContentPane().add(backPanel, BorderLayout.SOUTH); 
      getContentPane().doLayout(); 
      getContentPane().revalidate(); 
      update(getGraphics()); 
     } 
    }); 
    //gives instructions of how to play 
    instructionsButton.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      gameInfoScheme(); 
      backToMain(); 
      JOptionPane.showMessageDialog(null, String.format(gameInstructions, e.getActionCommand())); 
      getContentPane().removeAll(); 
      getContentPane().add(gameInfoPanel, BorderLayout.CENTER); 
      getContentPane().add(backPanel, BorderLayout.SOUTH); 
      getContentPane().doLayout(); 
      getContentPane().revalidate(); 
      update(getGraphics()); 
     } 
    }); 
    //sword inventory selection 
    swordList.addListSelectionListener(
     new ListSelectionListener(){ 
      public void valueChanged(ListSelectionEvent event){ 
       inventoryScheme(); 
       backToMain(); 
       Player.equippedSword = (Item) swordList.getSelectedValue(); 
       swordListLbl.setText("Equipped Sword: " + Player.equippedSwordDisplay); 
       swordListLbl.repaint(); 
      } 
     } 
    ); 
    //armor inventory selection 
    armorList.addListSelectionListener(
     new ListSelectionListener(){ 
      public void valueChanged(ListSelectionEvent event){ 
       inventoryScheme(); 
       backToMain(); 
       Player.equippedArmor = (Item) armorList.getSelectedValue(); 
       armorListLbl.setText("Equipped Armor: " + Player.equippedArmorDisplay); 
       armorListLbl.repaint(); 
      } 
     } 
    ); 
    //shield inventory selection 
    shieldList.addListSelectionListener(
     new ListSelectionListener(){ 
      public void valueChanged(ListSelectionEvent event){ 
       inventoryScheme(); 
       backToMain(); 
       Player.equippedShield = (Item) shieldList.getSelectedValue(); 
       shieldListLbl.setText("Equipped Shield: " + Player.equippedShieldDisplay); 
       shieldListLbl.repaint(); 
      } 
     } 
    ); 

    mainPanel.add(inventoryButton); 
    mainPanel.add(filler1); 
    mainPanel.add(storeButton); 
    mainPanel.add(filler2); 
    mainPanel.add(pstats); 
    mainPanel.add(filler3); 
    mainPanel.add(filler4); 
    mainPanel.add(roomInfo); 
    mainPanel.add(filler5); 
    mainPanel.add(moveButton); 
    mainPanel.add(filler6); 
    mainPanel.add(gameInfoButton); 
    add(mainPanel); 
} 

//back button for all slides 
public void backToMain(){ 
    backPanel = new JPanel(); 
    backPanel.setLayout(new FlowLayout()); 
    backPanel.add(backButton); 
} 

//control scheme for moving 
public void controlScheme(){ 
    upControl = new JButton("Up"); 
    downControl = new JButton("Down"); 
    leftControl = new JButton("Left"); 
    rightControl = new JButton("Right"); 

    controlPanel = new JPanel(); 
    controlPanel.setLayout(new GridLayout(3,3)); 

    controlPanel.add(filler7); 
    controlPanel.add(upControl); 
    controlPanel.add(filler8); 
    controlPanel.add(leftControl); 
    controlPanel.add(filler9); 
    controlPanel.add(rightControl); 
    controlPanel.add(filler10); 
    controlPanel.add(downControl); 
    controlPanel.add(filler11); 
} 

//gameInfo scheme for settings and game info 
public void gameInfoScheme(){ 
    gameHistory = "This game was created by Jordan Savage.He started the project in July of 2013.Any questions about how to play or coding?Email me at: [email protected]"; 
    gameInstructions = "In this game your player is a knight who awakes in a strange cell. You don't know where you are and you dont know what you are doing here.\n" 
     + "Your player moves around in a 2D world fighting monsters and leveling up. When you fight a monster you enter into a turn based battle sequence.\n" 
     + "during the turns you give damage, recieve damage, and are able to use items such as health potions. There are three ways to get items and gold.\n" 
     + "The first way is to kill a monster and gain the item or gold. The second way is to find it in a treasure room which are sctattered about.\n" 
     + "The third way is to use the shop. At the shop you can buy or sell items. All of your items are stored in the inventory tab"; 
    gameInfoPanel = new JPanel(); 
    gameInfoPanel.setLayout(new GridLayout(2,1)); 
    gameInfoPanel.add(historyButton); 
    gameInfoPanel.add(instructionsButton); 
} 

//inventory scheme for player 
@SuppressWarnings({ "unchecked", "rawtypes" }) 
public void inventoryScheme(){ 
    swordList = new JList(Player.swordinventorydisplay.toArray()); 
    swordList.setVisibleRowCount(4); 
    swordList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
    swordList.setSelectedValue(Player.equippedSwordDisplay, true); 

    armorList = new JList(Player.armorinventorydisplay.toArray()); 
    armorList.setVisibleRowCount(4); 
    armorList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
    armorList.setSelectedValue(Player.equippedArmorDisplay, true); 

    shieldList = new JList(Player.shieldinventorydisplay.toArray()); 
    shieldList.setVisibleRowCount(4); 
    shieldList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
    shieldList.setSelectedValue(Player.equippedShieldDisplay, true); 

    itemList = new JList(Player.iteminventorydisplay.toArray()); 
    itemList.setVisibleRowCount(4); 
    itemList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 

    swordListLbl = new JLabel("Equipped Sword: " + Player.equippedSword.itemName); 
    armorListLbl = new JLabel("Equipped Armor Kit: " + Player.equippedArmor.itemName); 
    shieldListLbl = new JLabel("Equipped Shield: " + Player.equippedShield.itemName); 
    itemListLbl = new JLabel("Item Inventory"); 

    inventoryPanel = new JPanel(); 
    inventoryPanel.setLayout(new GridLayout(8,1)); 

    inventoryPanel.add(swordListLbl); 
    inventoryPanel.add(swordList); 
    inventoryPanel.add(armorListLbl); 
    inventoryPanel.add(armorList); 
    inventoryPanel.add(shieldListLbl); 
    inventoryPanel.add(shieldList); 
    inventoryPanel.add(itemListLbl); 
    inventoryPanel.add(itemList); 
} 

任何幫助非常感謝,因爲我已經搜索了數十個試圖找到答案的論壇。如果您有任何問題,請詢問,如果您需要其他代碼部分,請詢問。謝謝:)

回答

1

我想知道如何在JList中的項目被選中時更新JLabel。

您將要使用List Selecton Listener。 Swing教程中關於如何編寫列表選擇監聽器的部分有一些示例代碼。

還有其他問題,您的代碼:

getContentPane().removeAll(); 
getContentPane().add(controlPanel, BorderLayout.CENTER); 
getContentPane().add(backPanel, BorderLayout.SOUTH); 
getContentPane().doLayout(); 
getContentPane().revalidate(); 
update(getGraphics()); 

這不是換面板的方式。使用Card Layout要容易得多。 Swing教程有一個關於How to Use a Card Layout的部分,其中包含一個示例。

與上面的代碼的具體問題是:

  1. 不要調用的doLayout()。該方法由其他Swing方法調用,您不需要手動調用它。

  2. 請勿調用update()並且不要使用getGraphics()。 Swing足夠聰明,可以在需要時重新繪製自己。但是,有時可以使用repaint()強制組件重新繪製自己。

+0

我看着列表選擇偵聽器,但它並沒有回答我的問題將JLabel沒有更新由於某種原因,我不知道爲什麼 – user2280906

+0

'我看着列表選擇偵聽器,但它沒」回答我的問題 - 你運行了演示代碼嗎? '因爲某種原因,jlabel沒有更新 - 你的代碼是錯誤的。發佈你的'SSCCE'來展示你的嘗試。 – camickr

+0

對不起,沒有回覆,但過去幾天我一直很忙,但是當我有空閒時間的時候,我一定會看看那些消息來源,看看他們是否有幫助 – user2280906

相關問題