Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法在多次单击时更改按钮的颜色_Java_Eclipse_Swing - Fatal编程技术网

Java 无法在多次单击时更改按钮的颜色

Java 无法在多次单击时更改按钮的颜色,java,eclipse,swing,Java,Eclipse,Swing,我计划建造一个机器人地板。我的要求是,在运行时,只需设置障碍,即更改按钮的颜色,即可设置地板。到目前为止,只需按下按钮即可更改按钮的颜色,但如果我再次按下该特定按钮,我想将其更改回以前的颜色。我无法在按偶数次点击后将按钮的颜色更改回以前的颜色。如果点击次数为偶数,则按钮不应着色,而应更改其奇数颜色。下面是我的代码 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.*;

我计划建造一个机器人地板。我的要求是,在运行时,只需设置障碍,即更改按钮的颜色,即可设置地板。到目前为止,只需按下按钮即可更改按钮的颜色,但如果我再次按下该特定按钮,我想将其更改回以前的颜色。我无法在按偶数次点击后将按钮的颜色更改回以前的颜色。如果点击次数为偶数,则按钮不应着色,而应更改其奇数颜色。下面是我的代码

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.JFrame;

class butMaddFrame extends JFrame implements ActionListener
{
int x=20;
int y=20;
JButton[][] buttons = new JButton[x][y];
    JPanel mPanel = new JPanel();
    JPanel bPanel = new JPanel();
    JPanel cPanel = new JPanel();
    JTextArea scoreKeeper = new JTextArea();
    Container c = getContentPane();
    int[][] intArray = new int[x][y];

    public butMaddFrame()
    {
        butGen();
        score2();
        //cPanel.add(scoreKeeper);
        bPanel.setLayout(new GridLayout(x,y));
        mPanel.setLayout(new BorderLayout());
        mPanel.add(bPanel, BorderLayout.CENTER);
      //  mPanel.add(cPanel, BorderLayout.LINE_END);
        c.add(mPanel);   
        setTitle("ButtonMaddness");
        setSize(500,400);
        setLocation(200,200);
            setVisible(true);
    }

    private void butGen()
    {
        for(int i=0;i<x;i++)
            for(int j=0;j<y;j++)
            {
                buttons[i][j] = new JButton(String.valueOf(i)+"x"+String.valueOf(j));
               buttons[i][j].setActionCommand("button" +i +"_" +j);
                buttons[i][j].addActionListener(this);
                buttons[i][j].setSize(100, 100);
                bPanel.add(buttons[i][j]);
            }
    }

    private void score()
    {

    }

    private void score2()
    {
        for(int i=0;i<x;i++)
            for(int j=0;j<y;j++)
               // buttons[i][j].setText(String.valueOf(intArray[i][j]));
            buttons[i][j].setText("");
        }

    public void actionPerformed(ActionEvent e)
    {           
        if(e.getActionCommand().contains("button"))
        {
            String str = e.getActionCommand().replaceAll("button", "");
            System.out.println(str);
            String[] v = str.split("_");
            int i = Integer.parseInt(v[0]);
            int j = Integer.parseInt(v[1]);            
            intArray[i][j]++;
                buttons[i][j].setBackground(Color.BLUE);
        //buttons[i][j].setBackground(null);
        System.out.println(e.getActionCommand() +"  " +i +"  " +j);
        // System.out.println();
        score2();
    }
  }
}

要将按钮颜色设置为其默认颜色(如果已单击两次):

 int check [][]= new int [100][100];

public void actionPerformed(ActionEvent e)
{           
    if(e.getActionCommand().contains("button"))
    {
        String str = e.getActionCommand().replaceAll("button", "");
        System.out.println(str);
        String[] v = str.split("_");
        int i = Integer.parseInt(v[0]);
        int j = Integer.parseInt(v[1]);            
        intArray[i][j]++;
        if(check[i][j]!=1){
            buttons[i][j].setBackground(Color.BLUE);
            check[i][j]=1;
        }
        else{
            buttons[i][j].setBackground(null);
            check[i][j]=0;
        }
    //buttons[i][j].setBackground(null);
    System.out.println(e.getActionCommand() +"  " +i +"  " +j);
    // System.out.println();
    score2();
}

}

检查当前颜色并设置另一种颜色?具体问题是什么?不仅仅是“我无法改变”,它看起来更像是“我甚至没有试图找到改变的方法”…这不是Android代码。这是针对Java Swing应用程序的。为什么要使用Android标签?如果你按两次相同的按钮,上面的代码会将按钮颜色更改为默认颜色。
 int check [][]= new int [100][100];

public void actionPerformed(ActionEvent e)
{           
    if(e.getActionCommand().contains("button"))
    {
        String str = e.getActionCommand().replaceAll("button", "");
        System.out.println(str);
        String[] v = str.split("_");
        int i = Integer.parseInt(v[0]);
        int j = Integer.parseInt(v[1]);            
        intArray[i][j]++;
        if(check[i][j]!=1){
            buttons[i][j].setBackground(Color.BLUE);
            check[i][j]=1;
        }
        else{
            buttons[i][j].setBackground(null);
            check[i][j]=0;
        }
    //buttons[i][j].setBackground(null);
    System.out.println(e.getActionCommand() +"  " +i +"  " +j);
    // System.out.println();
    score2();
}