我試圖做一個程序,將框架中的不同顏色更改爲使用彈出菜單選擇的顏色。我在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);
}
}
不只是轉儲代碼,並期望有人修復它 – redFIVE
我不是要求一個修復,我只是想知道該怎麼做才能修復它:) –
內部循環,你添加ActionListener看起來令人擔憂,你不能'不要嘗試添加ActionListeners到尚未初始化的按鈕 – MadProgrammer