Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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 访问JButton[]_Java_Arrays_Swing_Jbutton - Fatal编程技术网

Java 访问JButton[]

Java 访问JButton[],java,arrays,swing,jbutton,Java,Arrays,Swing,Jbutton,我有三排三个按钮:绿色、黄色和红色。它们都在自己的数组中。 当我单击绿色按钮时,同一行中的其他两个按钮应被禁用。但我不知道如何使用数组处理它 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Test extends JFrame implements ActionListener { View view = new View(); JButton bGreen[] = new JBu

我有三排三个按钮:绿色、黄色和红色。它们都在自己的数组中。
当我单击绿色按钮时,同一行中的其他两个按钮应被禁用。但我不知道如何使用数组处理它

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

public class Test extends JFrame implements ActionListener {

View view = new View();
JButton bGreen[] = new JButton[3];
JButton bYellow[] = new JButton[3];
JButton bRed[] = new JButton[3];

public Test() {
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setBounds(300, 100, 500, 400);
    this.setVisible(true);
    this.setLayout(new GridLayout(3, 3));

    makeButtons();
}

public void makeButtons() {
    for (int i = 0; i < 3; i++) {
        bGreen[i] = new JButton("Green");
        bYellow[i] = new JButton("Yellow");
        bRed[i] = new JButton("Red");

        bGreen[i].setBackground(Color.green);
        bYellow[i].setBackground(Color.yellow);
        bRed[i].setBackground(Color.red);

        bGreen[i].addActionListener(this);
        bYellow[i].addActionListener(this);
        bRed[i].addActionListener(this);

        this.add(bGreen[i]);
        this.add(bYellow[i]);
        this.add(bRed[i]);
    }
}

@Override
public void actionPerformed(ActionEvent ae) {
    Object source = ae.getSource();
    if (source == bGreen) // e.g. bGreen[1]
    {
        // bYellow[1].setEnabled(false);
        // bRed[1].setEnabled(false);
    }
    if (source == bYellow) // e.g. bYellow[1]
    {
        // bGreen[1].setEnabled(false);
        // bRed[1].setEnabled(false);
    }
    // the same with bRed
}

public static void main(String[] args) {
    Test test = new Test();
}
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类测试扩展JFrame实现ActionListener{
视图=新视图();
JButton bGreen[]=新JButton[3];
JButton bYellow[]=新JButton[3];
JButton繁殖[]=新JButton[3];
公开考试(){
此.setDefaultCloseOperation(关闭时退出);
这个.挫折(300100500400);
此.setVisible(true);
这个.setLayout(新的GridLayout(3,3));
makeButtons();
}
公共按钮(){
对于(int i=0;i<3;i++){
bGreen[i]=新的JButton(“绿色”);
bYellow[i]=新的按钮(“黄色”);
繁殖的[i]=新的纽扣(“红色”);
绿色[i].挫折背景(颜色.绿色);
bYellow[i].挫折背景(颜色.黄色);
繁殖[i].挫折地(颜色.红色);
bGreen[i].addActionListener(this);
bYellow[i].addActionListener(此);
繁殖[i].addActionListener(此);
添加(bGreen[i]);
本条加上(bYellow[i]);
本.添加(繁殖[i]);
}
}
@凌驾
已执行的公共无效行动(行动事件ae){
Object source=ae.getSource();
if(source==bGreen)//例如bGreen[1]
{
//bYellow[1]。设置已启用(false);
//繁殖[1]。设置已启用(false);
}
if(source==bYellow)//例如bYellow[1]
{
//bGreen[1]。设置已启用(false);
//繁殖[1]。设置已启用(false);
}
//同样的道理
}
公共静态void main(字符串[]args){
测试=新测试();
}
}

不要这样做。你将重新发明轮子。使用
JToggleButton
s并按行将它们全部分组到相同的
ButtonGroup


@Hovercraft Full Of Eels提出的建议是正确的(应该是一个答案)。

使用JToggleButtons,将一行中的每个按钮添加到该行的ButtonGroup对象中怎么样?@HovercraftFullOfEels你一针见血+1:)@HovercraftFullOfEels,一旦你给出答案,我将删除我的占位符答案。@mre:谢谢,但不要删除你的答案。这是一个很好的答案,如果我没有我的评论,你可能会用它来回答(我猜你甚至在你的答案发布后才看到我的评论)。1+