2010-06-13 60 views
1

我想更改JLabel在運行時查看的圖像,但我一直在收到NullPointerExceptions,或者當我按下應該執行操作的魔法按鈕時什麼都不會發生。 Java甚至有可能嗎?如何在運行時更改JLabel的值?

這是我的全部代碼:

import java.text.*; 
    import javax.swing.text.*; 
    import java.util.*; 
    import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; 
    import javax.swing.event.*; 

    public class Shell implements ActionListener, MenuKeyListener 
    { 
     JFrame frame; 
     JWindow window; 
     JButton PSubmit; 
     JPanel pane1, pane2; 
     JRadioButton R1, R2, R3; 
      ButtonGroup PGroup; 
     JTabbedPane layout; 

     String result; 
     String border = "Border.png"; 
     String DF = "Frame.png"; 
     String list []; 
     Driver driver; 

     public Shell() 
     { 
      driver = new Driver(); 
      list = new String [6]; 
     } 

     public void setFrame() 
     { 
      frame = new JFrame("Pokemon Program 3 by Systems Ready"); 
      frame.setSize(600, 600); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.setVisible(true); 
      frame.getContentPane().setLayout(new BorderLayout()); 
     } 
     public void frameLayout() 
     { 
      layout = new JTabbedPane(); 
      JPanel pane1 = new JPanel(); 
      JPanel pane2 = new JPanel(); 
      JLabel label = new JLabel("Please choose the restrictions:"); 
      JLabel imgLabel1 = new JLabel(new ImageIcon(border)); 
      JLabel notiLabel1 = new JLabel("The Pokemon chosen with these restrictions are:  "); 
      JLabel notiLabel2 = new JLabel("'No Restrictions': No restrictions for the kind of Pokemon chosen based on species or items."); 
      JLabel notiLabel3 = new JLabel("'Battle Revolution': All Pokemon must have unique items."); 
      JLabel notiLabel4 = new JLabel("'Battle Tower': All Pokemon must have unique items, Uber and Event Legendaries banned."); 
      JLabel label2 = new JLabel("Please choose possible Pokemon:"); 
      pane1.add(label); 
      pearlButtons(); 
      pane1.add(R1); 
      pane1.add(R2); 
      pane1.add(R3); 
      pane1.add(PSubmit); 
      pane1.add(notiLabel2); 
      pane1.add(notiLabel3); 
      pane1.add(notiLabel4); 
      pane1.add(imgLabel1); 
      pane1.add(notiLabel1); 
      JLabel pokeLabel1 = new JLabel(new ImageIcon(DF)); 
      JLabel pokeLabel2 = new JLabel(new ImageIcon(DF)); 
      JLabel pokeLabel3 = new JLabel(new ImageIcon(DF)); 
      JLabel pokeLabel4 = new JLabel(new ImageIcon(DF)); 
      JLabel pokeLabel5 = new JLabel(new ImageIcon(DF)); 
      JLabel pokeLabel6 = new JLabel(new ImageIcon(DF)); 
      pane1.add(pokeLabel1); 
      pane1.add(pokeLabel2); 
      pane1.add(pokeLabel3); 
      pane1.add(pokeLabel4); 
      pane1.add(pokeLabel5); 
      pane1.add(pokeLabel6); 
      pane2.add(label2); 
      layout.add("Pearl Version", pane1); 
      layout.add("SoulSilver Version", pane2); 
      frame.add(layout); 
     } 
     public void pearlButtons() 
     { 
      PGroup = new ButtonGroup(); 
      R1 = new JRadioButton("No Restrictions", true); 
      R1.setActionCommand("N"); 
      R1.setVisible(true); 
      R2 = new JRadioButton("Battle Revolution"); 
      R2.setActionCommand("BR"); 
      R2.setVisible(true); 
      R3 = new JRadioButton("Battle Tower"); 
      R3.setActionCommand("B"); 
      R3.setVisible(true); 
      PGroup.add(R1); 
      PGroup.add(R2); 
      PGroup.add(R3); 
      PSubmit = new JButton("Submit"); 
      PSubmit.setActionCommand("pstart"); 
      PSubmit.setVisible(true); 
      PSubmit.addActionListener(this); 
     } 
     public void pearlProcessing() 
     { 
        //The "list" array has a bunch of string names that get .png affixed to them (and I named the image files as such when I name them) 
      String file1 = list[0] + ".png"; 
      String file2 = list[1] + ".png"; 
      String file3 = list[2] + ".png"; 
      String file4 = list[3] + ".png"; 
      String file5 = list[4] + ".png"; 
      String file6 = list[5] + ".png"; 
    /*-------------------------------------------------------------------------------// 
        This is where the method's supposed to go to change the image... 
        I've tried pokeLabel = new JLabel(new ImageIcon(file1));, but that yields a NullPointerException. 
//-----------------------------------------------------------------------------------*/ 
     } 
     public static void main(String[] args) 
     { 
      Shell test = new Shell(); 
      test.setFrame(); 
      test.frameLayout(); 
      test.frame.setVisible(true); 
     } 
     public void actionPerformed(ActionEvent e) 
     { 
      if ("pstart".equals(e.getActionCommand())) 
      { 
       result = PGroup.getSelection().getActionCommand(); 
       if (result.equals("N")) 
       { 
        list = driver.Prandom(); 
        pearlProcessing(); 
       } 
       else 
        System.out.println("Not done yet! ;)"); 
      } 
     } 

     public void menuKeyPressed(MenuKeyEvent e) 
     { 
      System.out.println("pressed"); 
     } 
     public void menuKeyReleased(MenuKeyEvent e) 
     { 
      System.out.println("menuKeyReleased"); 
     } 
     public void menuKeyTyped(MenuKeyEvent e) 
     { 
      System.out.println("menuKeyTyped"); 
     } 

    } 
+1

我不認爲你表示的行可能會導致'NullPointerException'。你能發佈實際的堆棧跟蹤嗎? – 2010-06-13 01:10:07

+0

你是否通過調試器來運行它?什麼是文件名,它在您嘗試創建新的JLabel之前是否存在? – 2010-06-13 01:39:45

+0

@Micheal Mrozek:是的,你說得對。當我寫這篇文章的時候我很匆忙。的xD ;;當我將行「pane1.add(pokeLabel1);」到我在註釋行中指出的地方,我得到了一個N​​ullPointerException(而不是更合理的東西,比如說「不能在運行時間期間添加信息」)。異常引用該行和調用它所在函數的行,然後引用其餘的是包源代碼。我在評論框中提到的代碼不會因爲某種原因而導致任何事情發生,當我希望圖像改變時,這是非常令人沮喪的! – user365465 2010-06-13 02:02:09

回答

2

不知道如果是這樣的原因,我會改變

result = PGroup.getSelection().getActionCommand(); 
if (result.equals("N")) { 

ButtonModel selection = PGroup.getSelection(); 
result = (selection==null ? null : selection.getActionCommand()); 
if ("N".equals(result)) { 
// etc... 

這樣可以防範可能無效指針。

0

我強烈建議,只要你得到任何例外總是看看堆棧跟蹤,看看在哪裏發生異常的實際行。

但是如果我想推測,你可以在actionPerformed方法中調用PGroup.getSelection()。如果在單選按鈕組中沒有選中按鈕,則此方法可能會返回null。我還注意到,您沒有設置最初選定的單選按鈕(在單選按鈕上調用setSelected(true))。因此,如果所選單選按鈕是null,則調用getActionCommand將導致NullPointerException