使用工厂返回多个JButton数组......?

时间:2013-12-21 03:29:19

标签: java arrays return jbutton

我是新的java学生,我需要一些代码帮助。

我正在编写一个包含JButton数组的方法,我正在使用工厂方法来制作它们。现在的问题是,我不知道如何返回按钮(它们有不同的颜色,所以我不得不将它们分开)。总共我有4个阵列,我需要以某种方式将它们全部归还......

这是我的代码

private JButton[] createButtons() {
    JButton [] numberButton      =   null ;
    JButton [] operatorButtons   =   null ;
    JButton [] controlButtons    =   null ;
    JButton [] equalsButton      =   null ;

    numberButton                 =   new JButton[10] ;
    operatorButtons              =   new JButton[6]  ;
    controlButtons               =   new JButton[2]  ;
    equalsButton                 =   new JButton[1]  ;

    int index                    =   0 ;

    String   [] calcButtons   =   {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-", "*", "/", "Sqrt", "%",
                                     "C", "BS", "="} ;

    for (index = 0; index < 10; index++)
    numberButton[index]   =   ButtonFactory.createButton(calcButtons[index], Color.LIGHT_GRAY) ; 
    for (index = 10; index < 17; index++)
    operatorButtons[6]    =   ButtonFactory.createButton(calcButtons[index], Color.CYAN) ; 
    for (index = 17; index < 20; index++)
    controlButtons[2]     =   ButtonFactory.createButton(calcButtons[index], Color.MAGENTA) ;
    for (index = 19; index == 20; index++) 
    equalsButton[1]       =   ButtonFactory.createButton(calcButtons[index], Color.GREEN) ; 

    return (JButton[]) new Object[]{numberButton, numberButton, operatorButtons, controlButtons, equalsButton} ;


}

我在这里读到的地方,我可以创建一个对象,就像我在底部做的那样,它可以工作,但它不是......

另外,我可能需要一些帮助,将actionListener和mouseListener添加到每个数组。

谢谢!

使用一个阵列编辑整个新方法

private JButton[] createButtons() {
    JButton [] calcButtons        =   null ;
    calcButtons                  =   new JButton[BUTTON_COLUMNS * BUTTON_ROWS] ;
    int index                    =   0 ;

    String   [] numButtons       =   {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."} ;
    String   [] operButtons      =   {"+", ".", "-", "*", "/", "Sqrt", "%"} ;
    String   [] contButtons      =   {"C", "BS"} ;
    String   [] equalButton      =   {"="} ;


    for (int i = 0; i < numButtons.length ; i++) {
    calcButtons[i].add(ButtonFactory.createButton(numButtons[index], Color.LIGHT_GRAY)) ;
    index++ ; 
    calcButtons[i].addMouseListener(this)  ;
    calcButtons[i].addActionListener(this) ; }

    for (int i = 0; i < operButtons.length ; i++) {
    calcButtons[i].add(ButtonFactory.createButton(operButtons[index], Color.CYAN)) ;
    index++ ; 
    calcButtons[i].addMouseListener(this)  ;
    calcButtons[i].addActionListener(this) ; }

    for (int i = 0; i < contButtons.length; i++) {
    calcButtons[i].add(ButtonFactory.createButton(contButtons[index], Color.MAGENTA)) ; 
    index++ ; 
    calcButtons[i].addMouseListener(this)  ;
    calcButtons[i].addActionListener(this) ; }

    for(int i = 0; i < equalButton.length; i++){
    calcButtons[i].add(ButtonFactory.createButton(equalButton[index], Color.GREEN)) ; 
    index++ ; 
    calcButtons[i].addMouseListener(this)  ;
    calcButtons[i].addActionListener(this) ;}


    return calcButtons ;

}

但我的按钮仍然是空的,我无法弄清楚什么是错的。我假设它与calcButtons数组有关。

基本上

我希望能够做到这一点

好的,基本上我希望能够做到这一点

calcButtons[0].add(ButtonFactory.createButton("0", Color.LIGHT_GRAY)) ;
        calcButtons[1].add(ButtonFactory.createButton("1", Color.LIGHT_GRAY)) ;
        calcButtons[2].add(ButtonFactory.createButton("2", Color.LIGHT_GRAY)) ;
        calcButtons[3].add(ButtonFactory.createButton("3", Color.LIGHT_GRAY)) ;
        calcButtons[4].add(ButtonFactory.createButton("3", Color.LIGHT_GRAY)) ; 

有数组(包括操作和东西)......

修改

你的意思是这样吗?

for (int i = numButtons.length; i < (numButtons.length + operButtons.length) ; i++) {
    calcButtons[i].add(ButtonFactory.createButton(operButtons[index++], Color.CYAN)) ;
    calcButtons[i].addMouseListener(this)  ;
    calcButtons[i].addActionListener(this) ; }

因为在我将for循环改为this之后,我仍然得到null。我甚至算上了他们和一切。是否与索引++有关?

2 个答案:

答案 0 :(得分:1)

  • 你在你的for循环中对你的数组索引进行硬编码,这意味着代码将会失败,因为你将重复分配一个JButton到相同的数组项,留下另一个数组项null。您必须在JButton和String calcButtons数组中使用变量索引。
  • 考虑使用以i开头的for循环和一个名为index的单独索引,并在每个for循环的每次迭代中递增索引。
  • 然后使用i作为按键数组索引和String数组索引的索引。
  • 不要使用魔法数字,因为他们最终会咬你的屁股。使用数组的长度来确定for循环何时完成。
  • 您最终返回的数组也是正确的想法,但它不应该是Object[],而是JButton[][]

如,

int index = 0;
for (int i = 0; i < numberButton.length; i++) {
  // fill numberButton[i] using calcButtons[index]
  index++;
}
for (int i = 0; i < operatorButtons.length; i++) {
  // fill in operatorButtons[i], use calcButtons[index]
  index++;
}

/// ... etc
return new JButton[][]{numberButton, ..... etc...};

__ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _

修改
关于您的最新代码,让我们在精神上浏览您的代码的一部分......

  for (int i = 0; i < numButtons.length; i++) {

     // Line (A)
     calcButtons[i].add(ButtonFactory.createButton(numButtons[index], Color.LIGHT_GRAY));
     index++;
     calcButtons[i].addMouseListener(this);
     calcButtons[i].addActionListener(this);
  }

  for (int i = 0; i < operButtons.length; i++) {

     // Line (B)
     calcButtons[i].add(ButtonFactory.createButton(operButtons[index], Color.CYAN));
     index++;
     calcButtons[i].addMouseListener(this);
     calcButtons[i].addActionListener(this);
  }

那么在注释(A)下的行会发生什么?:你创建新的JButtons并将它们分配给数组位置0到numberButton.length的数组,基本上填充了calcButtons数组的开头部分。好的,到目前为止一切顺利。

但是在注释(B)和所有类似的行下会发生什么?:你创建新的JButton并将它们分配给数组位置0到operatorButtons.length的数组。

但这样可以吗?不,这不是因为您在阵列的开头重新分配了所有JButton。这是你想要做的吗?不,因为它将所有数组项留在数组null的末尾和中间。您希望前进到 之前分配的阵列JButton项目。有没有解决的办法?是的,只需使用您正在使用的变量。

我会告诉你如何做到这一点 - 我知道你可以。

答案 1 :(得分:1)

如果按钮组很重要,我会考虑返回二维数组,例如......

private JButton[][] createButtons() {

简单return使用类似......

的内容
return new JButton[][]{numberButton, operatorButtons, controlButtons, equalsButton};