2014-12-25 19 views
-3

我試圖做一個程序,將框架中的不同顏色更改爲使用彈出菜單選擇的顏色。我在ActionListener中發生錯誤,沒有它,我無法爲每個彩色按鈕執行操作。Java ActionListener錯誤,按鈕使用彈出窗口顏色更改程序

Exception in thread "main" java.lang.NullPointerException 
    at Graphic.<init>(Graphic.java:43) 
    at Graphic.main(Graphic.java:273) 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
public class Graphic extends JFrame implements ActionListener 
{ 
    JFrame frame = new JFrame("Graphic"); 
    private JButton [] l; 
     private JPopupMenu menu; 
     private Toolkit toolkit; 
public void actionPerformed(ActionEvent actionEvent) { 
    System.out.println("Selected: " + actionEvent.getActionCommand()); 
    } 
    public Graphic() 
    { 
     setLayout(new GridLayout(3,3)); 
       JButton l[]=new JButton[9]; 
       for (int i=0;i<9;i++) 
       { 
     l[i] = new JButton(); 
     add(l[i]); 
       if(i==0) 
     l[i].setBackground(Color.GREEN); 
       else if(i==1) 
     l[i].setBackground(Color.BLUE); 
       else if(i==2) 
     l[i].setBackground(Color.RED); 
       else if(i==3) 
     l[i].setBackground(Color.WHITE); 
       else if(i==4) 
     l[i].setBackground(Color.PINK); 
       else if(i==5) 
     l[i].setBackground(Color.GRAY); 
       else if(i==6) 
     l[i].setBackground(Color.CYAN); 
       else if(i==7) 
     l[i].setBackground(Color.MAGENTA); 
       else if(i==8) 
     l[i].setBackground(Color.BLACK); 
       for(JButton j:l) 
       { 
        j.addActionListener(this); 
       } 
       } 

     //Create the popup menu. 
     //private class ColorListener implements ActionListener{ 
     final JPopupMenu popup = new JPopupMenu(); 

     popup.add(new JMenuItem(new AbstractAction("Green") { 

      public void actionPerformed(ActionEvent e) { 
       if(e.getSource() == l[0]) 
        l[0].setBackground(Color.GREEN); 
       else if(e.getSource() == l[1]) 
        l[1].setBackground(Color.GREEN); 
       else if(e.getSource() == l[2]) 
        l[2].setBackground(Color.GREEN); 
       else if(e.getSource() == l[3]) 
        l[3].setBackground(Color.GREEN); 
       else if(e.getSource() == l[4]) 
        l[4].setBackground(Color.GREEN); 
       else if(e.getSource() == l[5]) 
        l[5].setBackground(Color.GREEN); 
       else if(e.getSource() == l[6]) 
        l[6].setBackground(Color.GREEN); 
       else if(e.getSource() == l[7]) 
        l[7].setBackground(Color.GREEN); 
       else if(e.getSource() == l[8]) 
        l[8].setBackground(Color.GREEN); 

     } 

     })); 

     popup.add(new JMenuItem(new AbstractAction("Blue") { 
      public void actionPerformed(ActionEvent e) { 
       if(e.getSource() == l[0]) 
        l[0].setBackground(Color.BLUE); 
       else if(e.getSource() == l[1]) 
        l[1].setBackground(Color.BLUE); 
       else if(e.getSource() == l[2]) 
        l[2].setBackground(Color.BLUE); 
       else if(e.getSource() == l[3]) 
        l[3].setBackground(Color.BLUE); 
       else if(e.getSource() == l[4]) 
        l[4].setBackground(Color.BLUE); 
       else if(e.getSource() == l[5]) 
        l[5].setBackground(Color.BLUE); 
       else if(e.getSource() == l[6]) 
        l[6].setBackground(Color.BLUE); 
       else if(e.getSource() == l[7]) 
        l[7].setBackground(Color.BLUE); 
       else if(e.getSource() == l[8]) 
        l[8].setBackground(Color.BLUE); 

      } 

     })); 
     popup.add(new JMenuItem(new AbstractAction("Red") { 
      public void actionPerformed(ActionEvent e) { 
       if(e.getSource() == l[0]) 
        l[0].setBackground(Color.RED); 
       else if(e.getSource() == l[1]) 
        l[1].setBackground(Color.RED); 
       else if(e.getSource() == l[2]) 
        l[2].setBackground(Color.RED); 
       else if(e.getSource() == l[3]) 
        l[3].setBackground(Color.RED); 
       else if(e.getSource() == l[4]) 
        l[4].setBackground(Color.RED); 
       else if(e.getSource() == l[5]) 
        l[5].setBackground(Color.RED); 
       else if(e.getSource() == l[6]) 
        l[6].setBackground(Color.RED); 
       else if(e.getSource() == l[7]) 
        l[7].setBackground(Color.RED); 
       else if(e.getSource() == l[8]) 
        l[8].setBackground(Color.RED); 

      } 
     })); 
     popup.add(new JMenuItem(new AbstractAction("Pink") { 
      public void actionPerformed(ActionEvent e) { 
       if(e.getSource() == l[0]) 
        l[0].setBackground(Color.PINK); 
       else if(e.getSource() == l[1]) 
        l[1].setBackground(Color.PINK); 
       else if(e.getSource() == l[2]) 
        l[2].setBackground(Color.PINK); 
       else if(e.getSource() == l[3]) 
        l[3].setBackground(Color.PINK); 
       else if(e.getSource() == l[4]) 
        l[4].setBackground(Color.PINK); 
       else if(e.getSource() == l[5]) 
        l[5].setBackground(Color.PINK); 
       else if(e.getSource() == l[6]) 
        l[6].setBackground(Color.PINK); 
       else if(e.getSource() == l[7]) 
        l[7].setBackground(Color.PINK); 
       else if(e.getSource() == l[8]) 
        l[8].setBackground(Color.PINK); 

      } 
     })); 
     popup.add(new JMenuItem(new AbstractAction("Gray") { 
      public void actionPerformed(ActionEvent e) { 
       if(e.getSource() == l[0]) 
        l[0].setBackground(Color.GRAY); 
       else if(e.getSource() == l[1]) 
        l[1].setBackground(Color.GRAY); 
       else if(e.getSource() == l[2]) 
        l[2].setBackground(Color.GRAY); 
       else if(e.getSource() == l[3]) 
        l[3].setBackground(Color.GRAY); 
       else if(e.getSource() == l[4]) 
        l[4].setBackground(Color.GRAY); 
       else if(e.getSource() == l[5]) 
        l[5].setBackground(Color.GRAY); 
       else if(e.getSource() == l[6]) 
        l[6].setBackground(Color.GRAY); 
       else if(e.getSource() == l[7]) 
        l[7].setBackground(Color.GRAY); 
       else if(e.getSource() == l[8]) 
        l[8].setBackground(Color.GRAY); 

      } 
     })); 
     popup.add(new JMenuItem(new AbstractAction("Yellow") { 
      public void actionPerformed(ActionEvent e) { 
       if(e.getSource() == l[0]) 
        l[0].setBackground(Color.YELLOW); 
       else if(e.getSource() == l[1]) 
        l[1].setBackground(Color.YELLOW); 
       else if(e.getSource() == l[2]) 
        l[2].setBackground(Color.YELLOW); 
       else if(e.getSource() == l[3]) 
        l[3].setBackground(Color.YELLOW); 
       else if(e.getSource() == l[4]) 
        l[4].setBackground(Color.YELLOW); 
       else if(e.getSource() == l[5]) 
        l[5].setBackground(Color.YELLOW); 
       else if(e.getSource() == l[6]) 
        l[6].setBackground(Color.YELLOW); 
       else if(e.getSource() == l[7]) 
        l[7].setBackground(Color.YELLOW); 
       else if(e.getSource() == l[8]) 
        l[8].setBackground(Color.YELLOW); 

      } 
     })); 
     popup.add(new JMenuItem(new AbstractAction("Cyan") { 
      public void actionPerformed(ActionEvent e) { 
       if(e.getSource() == l[0]) 
        l[0].setBackground(Color.CYAN); 
       else if(e.getSource() == l[1]) 
        l[1].setBackground(Color.CYAN); 
       else if(e.getSource() == l[2]) 
        l[2].setBackground(Color.CYAN); 
       else if(e.getSource() == l[3]) 
        l[3].setBackground(Color.CYAN); 
       else if(e.getSource() == l[4]) 
        l[4].setBackground(Color.CYAN); 
       else if(e.getSource() == l[5]) 
        l[5].setBackground(Color.CYAN); 
       else if(e.getSource() == l[6]) 
        l[6].setBackground(Color.CYAN); 
       else if(e.getSource() == l[7]) 
        l[7].setBackground(Color.CYAN); 
       else if(e.getSource() == l[8]) 
        l[8].setBackground(Color.CYAN); 

      } 
     })); 
     popup.add(new JMenuItem(new AbstractAction("Magenta") { 
      public void actionPerformed(ActionEvent e) { 
       if(e.getSource() == l[0]) 
        l[0].setBackground(Color.MAGENTA); 
       else if(e.getSource() == l[1]) 
        l[1].setBackground(Color.MAGENTA); 
       else if(e.getSource() == l[2]) 
        l[2].setBackground(Color.MAGENTA); 
       else if(e.getSource() == l[3]) 
        l[3].setBackground(Color.MAGENTA); 
       else if(e.getSource() == l[4]) 
        l[4].setBackground(Color.MAGENTA); 
       else if(e.getSource() == l[5]) 
        l[5].setBackground(Color.MAGENTA); 
       else if(e.getSource() == l[6]) 
        l[6].setBackground(Color.MAGENTA); 
       else if(e.getSource() == l[7]) 
        l[7].setBackground(Color.MAGENTA); 
       else if(e.getSource() == l[8]) 
        l[8].setBackground(Color.MAGENTA); 

      } 
     })); 
     popup.add(new JMenuItem(new AbstractAction("Black") { 
      public void actionPerformed(ActionEvent e) { 
       if(e.getSource() == l[0]) 
        l[0].setBackground(Color.BLACK); 
       else if(e.getSource() == l[1]) 
        l[1].setBackground(Color.BLACK); 
       else if(e.getSource() == l[2]) 
        l[2].setBackground(Color.BLACK); 
       else if(e.getSource() == l[3]) 
        l[3].setBackground(Color.BLACK); 
       else if(e.getSource() == l[4]) 
        l[4].setBackground(Color.BLACK); 
       else if(e.getSource() == l[5]) 
        l[5].setBackground(Color.BLACK); 
       else if(e.getSource() == l[6]) 
        l[6].setBackground(Color.BLACK); 
       else if(e.getSource() == l[7]) 
        l[7].setBackground(Color.BLACK); 
       else if(e.getSource() == l[8]) 
        l[8].setBackground(Color.BLACK); 

      } 
     })); 
     for(int j=0;j<9;j++)     
     l[j].addMouseListener(new MouseAdapter() { 
      public void mousePressed(MouseEvent e) { 
       popup.show(e.getComponent(), e.getX(), e.getY()); 
      } 
     }); 

     } 

    public static void main (String args[]) 
    { 
     Graphic g = new Graphic(); 
     g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     g.setSize(300, 300); 
     g.setVisible(true); 
    } 

} 
+0

不只是轉儲代碼,並期望有人修復它 – redFIVE

+0

我不是要求一個修復,我只是想知道該怎麼做才能修復它:) –

+0

內部循環,你添加ActionListener看起來令人擔憂,你不能'不要嘗試添加ActionListeners到尚未初始化的按鈕 – MadProgrammer

回答

2

看看本節......

  for (int i=0;i<9;i++) 
      { 
       l[i] = new JButton(); 
       //... 
       for(JButton j:l) 
       { 
        j.addActionListener(this); 
       } 
      } 

基本上,你內環試圖訪問還沒有被初始化,但是你的數組中的元素...

雖然你有機會,將一個斷點放入你的代碼,並通過它,這會給你一些重要的經驗

+0

1+,我幾乎有我的回答,當我被打電話時被輸入:)真正的問題是爲什麼他甚至有一個內部循環。創建按鈕時,只需將ActionListener添加到按鈕即可。發佈代碼的另一個問題是按鈕數組的陰影。 – camickr

+0

我會看看該部分該怎麼辦,謝謝你們 但是彈出菜單的ActionListener呢?有關它的任何想法? –

+0

@PrinceQuestiorinos您的彈出式菜單項ActionListener不會按照您認爲的方式工作,因爲事件的來源實際上是菜單項本身。看看setComponentPopupMenu – MadProgrammer