【问题标题】:Boton61.setBackground(Color.red);; changing button background colorsBoton61.setBackground(Color.red);;更改按钮背景颜色
【发布时间】:2015-01-27 02:23:33
【问题描述】:

我遇到了以下代码的问题,我试图让按钮工作,所以当我按下它时,它会将 Button61 背景颜色从白色更改为红色,但是当我按下按钮时,它给了我以下错误:

线程“AWT-EventQueue-0”中的异常 java.lang.NullPointerException 在 proyecto.Diseños.actionPerformed(Diseños.java:367)

有人知道如何解决这个问题吗?

第 367 行是 Boton61.setBackground(Color.red);

package proyecto;


import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;



/**
 *
 * @author Cristian
 *
 * */

public class Diseños extends JComponent implements ActionListener {

   JButton Boton1, Boton2, Boton3, Boton4, Boton5, Boton6, Boton7;

   JButton Boton11,Boton21,Boton31,Boton41,Boton51,Boton61;
   JButton Boton12,Boton22,Boton32,Boton42,Boton52,Boton62;
   JButton Boton13,Boton23,Boton33,Boton43,Boton53,Boton63;
   JButton Boton14,Boton24,Boton34,Boton44,Boton54,Boton64;
   JButton Boton15,Boton25,Boton35,Boton45,Boton55,Boton65;
   JButton Boton16,Boton26,Boton36,Boton46,Boton56,Boton66;
   JButton Boton17,Boton27,Boton37,Boton47,Boton57,Boton67;




   @Override
  public void paintComponent(Graphics g) {    

    Boton1=new JButton("lol"); 
    Boton1.setBounds(90, 410, 30, 30); 
    Boton1.setBackground(Color.black);
    Component agregar1 = add(Boton1);
    Boton1.addActionListener(new Diseños());

    Boton2=new JButton("lol"); 
    Boton2.setBounds(150, 410, 30, 30); 
    Boton2.setBackground(Color.black);
    Component agregar2 = add(Boton2);
    Boton2.addActionListener(new Diseños());

    Boton3=new JButton("lol"); 
    Boton3.setBounds(210, 410, 30, 30); 
    Boton3.setBackground(Color.black);
    Component agregar3 = add(Boton3);
    Boton3.addActionListener(new Diseños());

    Boton4=new JButton("lol"); 
    Boton4.setBounds(270, 410, 30, 30); 
    Boton4.setBackground(Color.black);
    Component agregar4 = add(Boton4);
    Boton4.addActionListener(new Diseños());

    Boton5=new JButton("lol"); 
    Boton5.setBounds(330, 410, 30, 30); 
    Boton5.setBackground(Color.black);
    Component agregar5 = add(Boton5);
    Boton5.addActionListener(new Diseños());

    Boton6=new JButton("lol"); 
    Boton6.setBounds(390, 410, 30, 30); 
    Boton6.setBackground(Color.black);
    Component agregar6 = add(Boton6);
    Boton6.addActionListener(new Diseños());

    Boton7=new JButton("lol"); 
    Boton7.setBounds(450, 410, 30, 30); 
    Boton7.setBackground(Color.black);
    Component agregar7 = add(Boton7);
    Boton7.addActionListener(new Diseños());


    Boton11=new JButton(""); 
    Boton11.setBounds(85, 10, 40, 60); 
    Boton11.setBackground(Color.white);
    Component agregar11 = add(Boton11);
    Boton11.addActionListener(new Diseños());

    Boton21=new JButton(""); 
    Boton21.setBounds(85, 70, 40, 60); 
    Boton21.setBackground(Color.white);
    Component agregar21 = add(Boton21);
    Boton21.addActionListener(new Diseños());

    Boton31=new JButton(""); 
    Boton31.setBounds(85, 130, 40, 60); 
    Boton31.setBackground(Color.white);
    Component agregar31 = add(Boton31);
    Boton31.addActionListener(new Diseños());

    Boton41=new JButton(""); 
    Boton41.setBounds(85, 190, 40, 60); 
    Boton41.setBackground(Color.white);
    Component agregar41 = add(Boton41);
    Boton41.addActionListener(new Diseños());

    Boton51=new JButton(""); 
    Boton51.setBounds(85, 250, 40, 60); 
    Boton51.setBackground(Color.white);
    Component agregar51 = add(Boton51);
    Boton51.addActionListener(new Diseños());

    Boton61=new JButton(""); 
    Boton61.setBounds(85, 310, 40, 60); 
    Boton61.setBackground(Color.white);
    Component agregar61 = add(Boton61);
    Boton61.addActionListener(new Diseños());


    Boton12=new JButton(""); 
    Boton12.setBounds(145, 10, 40, 60); 
    Boton12.setBackground(Color.white);
    Component agregar12 = add(Boton12);
    Boton12.addActionListener(new Diseños());

    Boton22=new JButton(""); 
    Boton22.setBounds(145, 70, 40, 60); 
    Boton22.setBackground(Color.white);
    Component agregar22 = add(Boton22);
    Boton22.addActionListener(new Diseños());

    Boton32=new JButton(""); 
    Boton32.setBounds(145, 130, 40, 60); 
    Boton32.setBackground(Color.white);
    Component agregar32 = add(Boton32);
    Boton32.addActionListener(new Diseños());

    Boton42=new JButton(""); 
    Boton42.setBounds(145, 190, 40, 60); 
    Boton42.setBackground(Color.white);
    Component agregar42 = add(Boton42);
    Boton42.addActionListener(new Diseños());

    Boton52=new JButton(""); 
    Boton52.setBounds(145, 250, 40, 60); 
    Boton52.setBackground(Color.white);
    Component agregar52 = add(Boton52);
    Boton52.addActionListener(new Diseños());

    Boton62=new JButton(""); 
    Boton62.setBounds(145, 310, 40, 60); 
    Boton62.setBackground(Color.white);
    Component agregar62 = add(Boton62);
    Boton62.addActionListener(new Diseños());

    Boton13=new JButton(""); 
    Boton13.setBounds(205, 10, 40, 60); 
    Boton13.setBackground(Color.white);
    Component agregar13 = add(Boton13);
    Boton13.addActionListener(new Diseños());

    Boton23=new JButton(""); 
    Boton23.setBounds(205, 70, 40, 60); 
    Boton23.setBackground(Color.white);
    Component agregar23 = add(Boton23);
    Boton23.addActionListener(new Diseños());

    Boton33=new JButton(""); 
    Boton33.setBounds(205, 130, 40, 60); 
    Boton33.setBackground(Color.white);
    Component agregar33 = add(Boton33);
    Boton33.addActionListener(new Diseños());

    Boton43=new JButton(""); 
    Boton43.setBounds(205, 190, 40, 60); 
    Boton43.setBackground(Color.white);
    Component agregar43 = add(Boton43);
    Boton43.addActionListener(new Diseños());

    Boton53=new JButton(""); 
    Boton53.setBounds(205, 250, 40, 60); 
    Boton53.setBackground(Color.white);
    Component agregar53 = add(Boton53);
    Boton53.addActionListener(new Diseños());

    Boton63=new JButton(""); 
    Boton63.setBounds(205, 310, 40, 60); 
    Boton63.setBackground(Color.white);
    Component agregar63 = add(Boton63);
    Boton63.addActionListener(new Diseños());

    Boton14=new JButton(""); 
    Boton14.setBounds(265, 10, 40, 60); 
    Boton14.setBackground(Color.white);
    Component agregar14 = add(Boton14);
    Boton14.addActionListener(new Diseños());

    Boton24=new JButton(""); 
    Boton24.setBounds(265, 70, 40, 60); 
    Boton24.setBackground(Color.white);
    Component agregar24 = add(Boton24);
    Boton24.addActionListener(new Diseños());

    Boton34=new JButton(""); 
    Boton34.setBounds(265, 130, 40, 60); 
    Boton34.setBackground(Color.white);
    Component agregar34 = add(Boton34);
    Boton34.addActionListener(new Diseños());

    Boton44=new JButton(""); 
    Boton44.setBounds(265, 190, 40, 60); 
    Boton44.setBackground(Color.white);
    Component agregar44 = add(Boton44);
    Boton44.addActionListener(new Diseños());

    Boton54=new JButton(""); 
    Boton54.setBounds(265, 250, 40, 60); 
    Boton54.setBackground(Color.white);
    Component agregar54 = add(Boton54);
    Boton54.addActionListener(new Diseños());

    Boton64=new JButton(""); 
    Boton64.setBounds(265, 310, 40, 60); 
    Boton64.setBackground(Color.white);
    Component agregar64 = add(Boton64);
    Boton64.addActionListener(new Diseños());

    Boton15=new JButton(""); 
    Boton15.setBounds(325, 10, 40, 60); 
    Boton15.setBackground(Color.white);
    Component agregar15 = add(Boton15);
    Boton15.addActionListener(new Diseños());

    Boton25=new JButton(""); 
    Boton25.setBounds(325, 70, 40, 60); 
    Boton25.setBackground(Color.white);
    Component agregar25 = add(Boton25);
    Boton25.addActionListener(new Diseños());

    Boton35=new JButton(""); 
    Boton35.setBounds(325, 130, 40, 60); 
    Boton35.setBackground(Color.white);
    Component agregar35 = add(Boton35);
    Boton35.addActionListener(new Diseños());

    Boton45=new JButton(""); 
    Boton45.setBounds(325, 190, 40, 60); 
    Boton45.setBackground(Color.white);
    Component agregar45 = add(Boton45);
    Boton45.addActionListener(new Diseños());

    Boton55=new JButton(""); 
    Boton55.setBounds(325, 250, 40, 60); 
    Boton55.setBackground(Color.white);
    Component agregar55 = add(Boton55);
    Boton55.addActionListener(new Diseños());

    Boton65=new JButton(""); 
    Boton65.setBounds(325, 310, 40, 60); 
    Boton65.setBackground(Color.white);
    Component agregar65 = add(Boton65);
    Boton65.addActionListener(new Diseños());

    Boton16=new JButton(""); 
    Boton16.setBounds(385, 10, 40, 60); 
    Boton16.setBackground(Color.white);
    Component agregar16 = add(Boton16);
    Boton16.addActionListener(new Diseños());

    Boton26=new JButton(""); 
    Boton26.setBounds(385, 70, 40, 60); 
    Boton26.setBackground(Color.white);
    Component agregar26 = add(Boton26);
    Boton26.addActionListener(new Diseños());

    Boton36=new JButton(""); 
    Boton36.setBounds(385, 130, 40, 60); 
    Boton36.setBackground(Color.white);
    Component agregar36 = add(Boton36);
    Boton36.addActionListener(new Diseños());

    Boton46=new JButton(""); 
    Boton46.setBounds(385, 190, 40, 60); 
    Boton46.setBackground(Color.white);
    Component agregar46 = add(Boton46);
    Boton46.addActionListener(new Diseños());

    Boton56=new JButton(""); 
    Boton56.setBounds(385, 250, 40, 60); 
    Boton56.setBackground(Color.white);
    Component agregar56 = add(Boton56);
    Boton56.addActionListener(new Diseños());

    Boton66=new JButton(""); 
    Boton66.setBounds(385, 310, 40, 60); 
    Boton66.setBackground(Color.white);
    Component agregar66 = add(Boton66);
    Boton66.addActionListener(new Diseños());

    Boton17=new JButton(""); 
    Boton17.setBounds(445, 10, 40, 60); 
    Boton17.setBackground(Color.white);
    Component agregar17 = add(Boton17);
    Boton17.addActionListener(new Diseños());

    Boton27=new JButton(""); 
    Boton27.setBounds(445, 70, 40, 60); 
    Boton27.setBackground(Color.white);
    Component agregar27 = add(Boton27);
    Boton27.addActionListener(new Diseños());

    Boton37=new JButton(""); 
    Boton37.setBounds(445, 130, 40, 60); 
    Boton37.setBackground(Color.white);
    Component agregar37 = add(Boton37);
    Boton37.addActionListener(new Diseños());

    Boton47=new JButton(""); 
    Boton47.setBounds(445, 190, 40, 60); 
    Boton47.setBackground(Color.white);
    Component agregar47 = add(Boton47);
    Boton47.addActionListener(new Diseños());

    Boton57=new JButton(""); 
    Boton57.setBounds(445, 250, 40, 60); 
    Boton57.setBackground(Color.white);
    Component agregar57 = add(Boton57);
    Boton57.addActionListener(new Diseños());

    Boton67=new JButton(""); 
    Boton67.setBounds(445, 310, 40, 60); 
    Boton67.setBackground(Color.white);
    Component agregar67 = add(Boton67);
    Boton67.addActionListener(new Diseños());

    super.paintComponent(g);
    g.drawRect (570, 10, 300, 200);
    g.setColor(Color.BLACK);
    g.fillRect(570, 10, 300, 200);
    g.drawRect (570, 250, 300, 200);
    g.setColor(Color.BLACK);
    g.fillRect(570, 250, 300, 200);
    g.setColor(Color.WHITE);
    Font oldFont=getFont();
    Font fuente=new Font("Primer Jugador", Font.BOLD, 14);
    g.setFont(fuente);
    g.drawString("Primer Jugador", 580, 30);
    Font titulo=new Font("Segundo Jugador", Font.BOLD, 14);
    g.setFont(titulo);
    g.drawString("Segundo Jugador", 580, 270);
    g.drawString("[ " + Proyecto.primernombre + " ]", 580, 50);
    g.drawString("[ " + Proyecto.segundonombre + " ]", 580, 290);
    g.setColor(Color.CYAN);
    g.fillRoundRect(10, 5, 550, 450, 50, 50);



  }

   @Override
    public void actionPerformed(ActionEvent ae) {
        System.out.println("Ayy lmao");
        Boton61.setBackground(Color.red);;


    }

}

【问题讨论】:

  • 按钮组件的创建代码都不应该在paintComponent中。这在你的上一个问题中讨论过。将您的组件创建代码放在类的构造函数中。 paintComponent 方法应该只用于绘画和绘画。

标签: java


【解决方案1】:
  1. 任何按钮组件创建代码都不应在paintComponent 中。这在我对your last question 的回答中得到了很好的讨论(我认为)。将您的组件创建代码放在类的构造函数中。 paintComponent 方法应该只用于绘画和绘画。
  2. 您将该类用作 ActionListener,这对于玩具程序来说是可以的,但如果您这样做,请不要创建新的 Disenos 对象,而应使用 this。这就是 NullPointerException 的原因,因为新创建的 Disenos ActionListeners 中的 JButtons 从未创建过,因为该类从未“绘制”过,也从未显示过。需要明确的是,您正在创建近 100 个 Disenos 对象,但仅显示其中一个。
  3. 我自己,我更愿意避免让我的视图类实现我的侦听器接口,因为它给视图类带来了太多的责任。
  4. 此外,如果您使用数组和 for 循环,您的代码可能会大大缩短

例如:

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;

public class Disenos2 extends JPanel {
   public static final int ROWS = 6;
   public static final int COLUMNS = 7;

   private JButton[][] botones = new JButton[ROWS][COLUMNS];
   private boolean redTurn = true;

   public Disenos2() {
      ButtonAction buttonAction = new ButtonAction();
      setLayout(new GridLayout(ROWS, COLUMNS, 5, 5));
      for (int row = 0; row < botones.length; row++) {
         for (int col = 0; col < botones[row].length; col++) {
            JButton boton = new JButton("      ");
            boton.addActionListener(buttonAction);
            add(boton);
            botones[row][col] = boton;
         }
      }
   }

   private class ButtonAction implements ActionListener {
      @Override
      public void actionPerformed(ActionEvent e) {
         JButton boton = (JButton) e.getSource();
         int column = -1;
         for (int row = 0; row < botones.length; row++) {
            for (int col = 0; col < botones[row].length; col++) {
               if (boton == botones[row][col]) {
                  column = col;
               }
            }
         }

         for (int row = ROWS - 1; row >= 0; row--) {
            Color bkgd = botones[row][column].getBackground();
            if (!isColorSelected(botones[row][column])) {
               Color c = redTurn ? Color.red : Color.black;
               botones[row][column].setBackground(c);
               redTurn = !redTurn;
               break;
            }
         }
      }

      private boolean isColorSelected(JButton jButton) {
         Color c = jButton.getBackground();
         if (Color.red.equals(c)) {
            return true;
         }
         if (Color.black.equals(c)) {
            return true;
         }
         return false;
      }
   }

   private static void createAndShowGui() {
      Disenos2 mainPanel = new Disenos2();

      JFrame frame = new JFrame("Disenos2");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }

}

【讨论】:

  • 我知道如何使用数组和for循环,感谢您的帮助,您的建议也帮助我解决了颜色变化,很抱歉给您带来不便,但我是新手,您能给我一个将按钮定位到类的构造函数是什么意思的小例子?
  • @CristianGonzalez:例如
  • 非常感谢,您清除了我所有的疑虑,现在我的程序可以正常运行了。
猜你喜欢
  • 2015-06-04
  • 1970-01-01
  • 2021-06-22
  • 2015-04-03
  • 1970-01-01
  • 2011-06-26
相关资源
最近更新 更多