java - 为什么我尝试使用 getActionCommand 时出现空指针异常

标签 java swing events colors jbutton

您好,我已经设法让我的按钮着色。现在我想编写一段代码,只允许在任何行或列中使用每种颜色的一个按钮。我试过这个但不能让我的私有(private) boolean 值工作。下面是我现在只想检查行的代码

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
public class Grid5 extends JFrame implements ActionListener
{
        private ColourChooser paintBox = null;
        public static final int ROW = 6;
        public static final int COLUMN = 6;
        private static Grid5 grid;
        public static final String defaultName = "Black";
        public JButton[][] buttons; //makes an array called buttons
        public static void main(String[] args)// sets up a 6x6 grid
        {
            int rows = 6;
            int cols = 6;
            int size = 600;
            Grid5 grid = new Grid5(rows, cols);
            grid.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            grid.setPreferredSize(new Dimension(size, size));
            grid.pack();
            grid.setLocationRelativeTo(null);
            grid.setVisible(true);
        }
        // main
        public Grid5(int rows, int cols) // makes the 6x6 main grid a grid of JButtons
        {
            int rowSize = 6;
            int colSize = 6;
            int gridSize = 600;
            buttons = new JButton[rowSize][colSize];
            Container pane = getContentPane();
            pane.setLayout(new GridLayout(rows, cols));
            for(int j =0; j < rows; j++){
                for (int i = 0; i < cols; i++) {
                    buttons[j][i] = new JButton("");
                    buttons[j][i].setOpaque(true);
                    buttons[j][i].setBackground(Color.BLACK);
                    buttons[j][i].setActionCommand(j + " " + i);
                    buttons[j][i].setName("Black");
                    buttons[j][i].addActionListener(this);
                    pane.add(buttons[j][i]);
                }
            }
        }               //end of grid constructor

        public void actionPerformed(ActionEvent e) 
        {
            JButton button = (JButton) e.getSource();
            if ( paintBox != null && paintBox.isShowing())//stops more than one paintBox from opening
                paintBox.dispose();
            if( e.getSource() instanceof JButton){
                ((JButton)e.getSource()).setBackground(Color.BLACK);
            } 

            int rows = 6;
            int cols = 1;
            int size = 300;
            paintBox = new ColourChooser(grid, false, button);
            paintBox.setPreferredSize(new Dimension(size/3, size));
            paintBox.pack();
            paintBox.setVisible(true);
        }

        public class ColourChooser extends JDialog
        { 
            private JButton fillRed = new JButton("Red");
            private JButton fillYellow = new JButton("Yellow");
            private JButton fillBlue = new JButton("Blue");
            private JButton fillGreen = new JButton("Green");
            private JButton fillPurple = new JButton("Purple");
            private JButton fillBrown = new JButton("Brown");
            private JButton[] paintButton = {fillRed,fillYellow,fillBlue,fillGreen,fillPurple,fillBrown};
            private Color[] colours = {Color.RED, Color.YELLOW, Color.BLUE, Color.GREEN, new Color(102, 0, 102), new Color(102, 51, 0)};
            private JButton buttonPress;

            private int buttonsLeftRow ;
        private int buttonsLeftColumn ;
        private int row,column;

            public ColourChooser(final Grid5 frame, boolean isModal, JButton button)
            {

                buttonPress = button;
                JPanel panel = new JPanel();
                panel.setLayout(new GridLayout(6, 1));
                for (int i = 0; i < paintButton.length; i++) {
                    paintButton[i].setOpaque(true);
                    paintButton[i].addActionListener(buttonAction);
                    paintButton[i].setForeground(new Color(100,100,100));
                    paintButton[i].setBackground(colours[i]);
                    panel.add(paintButton[i]);
                }
                add(panel);
                pack();
            }
            private ActionListener buttonAction = new ActionListener()
        {
            public void actionPerformed(ActionEvent a)
            {
                JButton fill = (JButton) a.getSource(); 
                String colour = "Red";
                if(fill == fillRed){

                    if(checkColoursRow(colour,row)){
                        buttonPress.setBackground(Color.RED);
                        buttonPress.setName("Red");
                        dispose();
                    }
                }
                colour = "Yellow";
                if(fill == fillYellow){

                    if(checkColoursRow(colour,row)){
                    buttonPress.setBackground(Color.YELLOW);
                    buttonPress.setName("Yellow");
                    dispose();
                    }
                }
                colour = "Blue";
                if(fill == fillBlue){

                    if(checkColoursRow(colour,row)){
                    buttonPress.setBackground(Color.BLUE);
                    buttonPress.setName("Blue");
                    dispose();
                    }
                }
                colour = "Green";
                if(fill == fillGreen){

                    if(checkColoursRow(colour,row)){
                    buttonPress.setBackground(Color.GREEN);
                    buttonPress.setName("Green");
                    dispose();
                    }
                }
                colour = "Purple";
                if(fill == fillPurple){

                    if(checkColoursRow(colour,row)){
                    buttonPress.setBackground(new Color(102, 0, 102));
                    buttonPress.setName("Purple");
                    dispose();
                    }
                }
                colour = "Brown";
                if(fill == fillBrown){

                    if(checkColoursRow(colour,row)){
                    buttonPress.setBackground(new Color(102, 51, 0));
                    buttonPress.setName("Brown");
                    dispose();
                    }
                } 
            }
        };

        private boolean checkColoursRow(String colour, int row){
            buttonsLeftRow = 0;
            buttonsLeftColumn = 0;
            String command = buttonPress.getActionCommand();
            String[] arrayInfo = command.split(" ");
            row = Integer.parseInt(arrayInfo[0]);
            column = Integer.parseInt(arrayInfo[1]);
            for(String c : grid.getRow(row)){
                if(c.equals(null)){
                continue;
                if(buttonPress.getName().equals(c)){
                    return true;
                }
                }
                return false;

            }
            }
        }
}

谢谢

最佳答案

您在构造函数的一个设置行中将 ji 倒过来,因此它指的是尚未创建的按钮实例:

buttons[i][j].setActionCommand(j + " " + i);
        ^  ^

当我调换它们并测试它时,程序运行得很好。

关于java - 为什么我尝试使用 getActionCommand 时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9930141/

相关文章:

c# - 如何在 C# 中取消 CellEnter/CellLeave 事件?

java - 如何从包含任何字符的文本文件中查找最长的单词?

java - 如何使用 Dagger 2 注入(inject) pojo 依赖项?

java - android:通过json解析

Java Swing——将 JPanel 的按键输入添加到 JOptionpane

events - ZK如何处理多个事件?

java - Java 中的 Nats 请求/回复

java - 在 maven 和/或 eclipse 中强制执行 java 6 源兼容性

java - 如何在java swing中的每个页面中打印带有页脚的整个JPanel

java - 如何向 JFrame 添加 key 监听器?