我正在做一个彩色铅笔项目,使用100个JButton作为像10x10矩阵定位的像素。我还有10个其他jButton代表颜色,另外2个j代表工具“铅笔”和“桶”。
我目前仅使用铅笔jButton,因此您可以通过单击铅笔JButton然后选择一种颜色的JButton来绘制100个JButton中的任何一个。
该算法运行良好,问题是我需要对所有100个JButton应用相同的着色方法(colorButton),因此我想创建一个数组来存储所有JButton,然后为每个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代码使用反射使用ColorButtons类中定义的现有JButtons填充ArrayList 。
我仍然不确定为什么需要在与另一个List的循环中分配Array,但这是在这里。
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();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句