如何使用 Java 中的现有 JButton 创建 JButton 数组?
How to create a JButton Array with existing JButtons in Java?
我正在做一个彩色铅笔项目,使用 100 个 JButton 作为像 10x10 矩阵一样定位的像素。我还有其他 10 个代表颜色的 jButton 和另外 2 个代表工具 "pencil" 和 "bucket"。
我现在只使用铅笔 jButton,因此您可以通过单击铅笔 JButton 然后选择一种颜色的 JButton 来绘制 100 个 JButton 中的任何一个。
算法工作正常,问题是我需要对所有 100 个 JButton 应用相同的着色方法 (colorButton),所以我想创建一个数组来存储所有 JButton,然后调用我的 colorButton 方法他们每个人。
我不知道如何将所有 100 个 JButton 存储到 JButton 数组中,因为它们已经创建并命名。
这就是我正在尝试的:
public void colorButton(JButton button){
if (type == "pencil"){
if(color.equals("gray")){
button.setBackground( new Color(101,101,101));
}else if(color.equals("white")){
button.setBackground( new Color(255,255,255));
}else if(color.equals("black")){
button.setBackground( new Color(0,0,0));
}else if(color.equals("blue")){
button.setBackground( new Color(0,0,255));
}else if(color.equals("red")){
button.setBackground( new Color(255,0,0));
}
public void buttonArray(){
JButton[] button = new JButton[100];
for (int i = 0; i < 100; i++){
button[i] = jButton1; //I need to get each of the 100 buttons here
colorButton(button[i]);
}
}
因此,除了 JButton1,我还需要一种方法来存储所有 100 个。
有什么想法吗?
谢谢
*编辑以澄清问题和情况
不知道其用途的上下文并假设 colorButton() 方法是模拟方法,因为它缺少几个大括号。
以下 Java 代码使用反射来填充 ArrayList,使用 ColorButtons class.
中定义的现有 JButton
我仍然不确定为什么你需要在循环中将数组分配给另一个列表,但就是这样。
public class ColorButtons {
// JButton sample.
private JButton button1 = new JButton("1");
private JButton button2 = new JButton("2");
private JButton button3 = new JButton("3");
// This is used to store the buttons.
ArrayList<JButton> jbuttons = new ArrayList<JButton>();
// Boilerplate, as I have no idea what this does.
private String type = "pencil";
private String color = "white";
/**
* Populate the JButton List on instantiation.
*
* @see ColorButtons#populateJButtonList()
*/
public ColorButtons() {
// Populate "jbuttons" ArrayList with JButtons.
this.populateJButtonList();
}
public void colorButton(JButton button) {
if (type == "pencil") {
if (color == "gray") {
button.setBackground(new Color(101, 101, 101));
} else if (color == "white") {
button.setBackground(new Color(255, 255, 255));
} else if (color == "black") {
button.setBackground(new Color(0, 0, 0));
} else if (color == "blue") {
button.setBackground(new Color(0, 0, 255));
} else if (color == "red") {
button.setBackground(new Color(255, 0, 0));
}
}
}
public void buttonArray() {
JButton[] button = new JButton[100];
for (int i = 0; i < 100; i++) {
// Assign each JButton in the list to array element.
for (JButton jbutton : jbuttons) {
button[i] = jbutton; // I need to get each of the 100 buttons
// here
System.out.println("Button" + button[i].getText());
colorButton(button[i]);
}
}
}
/**
* This is used to add the JButtons to a list using reflection. Used in the
* constructor.
*
* @see ColorButtons#ColorButtons()
*/
public void populateJButtonList() {
// Gets the class attributes, e.g. JButton, String, Integer types, everything.
// In this case it is this class, but can define any other class in your project.
Field[] fields = ColorButtons.class.getDeclaredFields();
// Loop over each field to determine if it is a JButton.
for (Field field : fields) {
// If it is a JButton then add it to the list.
if (field.getType().equals(JButton.class)) {
try {
// De-reference the field from the object (ColorButtons) and cast it to a JButton and add it to the list.
jbuttons.add((JButton) field.get(this));
} catch (IllegalArgumentException | IllegalAccessException
| SecurityException e) {
e.printStackTrace();
}
}
}
}
public static void main(String... args) {
ColorButtons color = new ColorButtons();
color.buttonArray();
}
}
我正在做一个彩色铅笔项目,使用 100 个 JButton 作为像 10x10 矩阵一样定位的像素。我还有其他 10 个代表颜色的 jButton 和另外 2 个代表工具 "pencil" 和 "bucket"。
我现在只使用铅笔 jButton,因此您可以通过单击铅笔 JButton 然后选择一种颜色的 JButton 来绘制 100 个 JButton 中的任何一个。
算法工作正常,问题是我需要对所有 100 个 JButton 应用相同的着色方法 (colorButton),所以我想创建一个数组来存储所有 JButton,然后调用我的 colorButton 方法他们每个人。
我不知道如何将所有 100 个 JButton 存储到 JButton 数组中,因为它们已经创建并命名。
这就是我正在尝试的:
public void colorButton(JButton button){
if (type == "pencil"){
if(color.equals("gray")){
button.setBackground( new Color(101,101,101));
}else if(color.equals("white")){
button.setBackground( new Color(255,255,255));
}else if(color.equals("black")){
button.setBackground( new Color(0,0,0));
}else if(color.equals("blue")){
button.setBackground( new Color(0,0,255));
}else if(color.equals("red")){
button.setBackground( new Color(255,0,0));
}
public void buttonArray(){
JButton[] button = new JButton[100];
for (int i = 0; i < 100; i++){
button[i] = jButton1; //I need to get each of the 100 buttons here
colorButton(button[i]);
}
}
因此,除了 JButton1,我还需要一种方法来存储所有 100 个。
有什么想法吗?
谢谢
*编辑以澄清问题和情况
不知道其用途的上下文并假设 colorButton() 方法是模拟方法,因为它缺少几个大括号。
以下 Java 代码使用反射来填充 ArrayList,使用 ColorButtons class.
中定义的现有 JButton我仍然不确定为什么你需要在循环中将数组分配给另一个列表,但就是这样。
public class ColorButtons {
// JButton sample.
private JButton button1 = new JButton("1");
private JButton button2 = new JButton("2");
private JButton button3 = new JButton("3");
// This is used to store the buttons.
ArrayList<JButton> jbuttons = new ArrayList<JButton>();
// Boilerplate, as I have no idea what this does.
private String type = "pencil";
private String color = "white";
/**
* Populate the JButton List on instantiation.
*
* @see ColorButtons#populateJButtonList()
*/
public ColorButtons() {
// Populate "jbuttons" ArrayList with JButtons.
this.populateJButtonList();
}
public void colorButton(JButton button) {
if (type == "pencil") {
if (color == "gray") {
button.setBackground(new Color(101, 101, 101));
} else if (color == "white") {
button.setBackground(new Color(255, 255, 255));
} else if (color == "black") {
button.setBackground(new Color(0, 0, 0));
} else if (color == "blue") {
button.setBackground(new Color(0, 0, 255));
} else if (color == "red") {
button.setBackground(new Color(255, 0, 0));
}
}
}
public void buttonArray() {
JButton[] button = new JButton[100];
for (int i = 0; i < 100; i++) {
// Assign each JButton in the list to array element.
for (JButton jbutton : jbuttons) {
button[i] = jbutton; // I need to get each of the 100 buttons
// here
System.out.println("Button" + button[i].getText());
colorButton(button[i]);
}
}
}
/**
* This is used to add the JButtons to a list using reflection. Used in the
* constructor.
*
* @see ColorButtons#ColorButtons()
*/
public void populateJButtonList() {
// Gets the class attributes, e.g. JButton, String, Integer types, everything.
// In this case it is this class, but can define any other class in your project.
Field[] fields = ColorButtons.class.getDeclaredFields();
// Loop over each field to determine if it is a JButton.
for (Field field : fields) {
// If it is a JButton then add it to the list.
if (field.getType().equals(JButton.class)) {
try {
// De-reference the field from the object (ColorButtons) and cast it to a JButton and add it to the list.
jbuttons.add((JButton) field.get(this));
} catch (IllegalArgumentException | IllegalAccessException
| SecurityException e) {
e.printStackTrace();
}
}
}
}
public static void main(String... args) {
ColorButtons color = new ColorButtons();
color.buttonArray();
}
}