ActionListener不能从公共类引用数组?

时间:2011-11-23 10:40:06

标签: java swing actionlistener jradiobutton

为什么public void actionPerformed (ActionEvent event), colorButton[]突出显示无法找到符号或变量?

我该如何调试?我正在尝试在colorButton[]

中定义public void actionPerformed (ActionEvent event)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ColorOptionsPanel extends JPanel {
private final int WIDTH = 350, HEIGHT = 100, FONT_SIZE = 20;
private final int NUM_COLORS = 5;
private Color [] color = new Color[NUM_COLORS];
private JLabel heading;
private JRadioButton [] colorButton= new JRadioButton[color.length];

// ------------------------------------------------------------------
// Sets up a panel with a label at the top and a set of radio buttons
// that control the background color of the panel.
// ------------------------------------------------------------------
public ColorOptionsPanel ()
{

// Set up heading and colors
heading = new JLabel ("Choose the background color!");
heading.setFont (new Font ("Helvetica", Font.BOLD, FONT_SIZE));
color[0] = Color.yellow;
color[1] = Color.cyan;
color[2] = Color.red;
color[3] = Color.green;
color[4] = Color.magenta;

colorButton[0]=new JRadioButton("Yellow",true);
colorButton[1]=new JRadioButton("Cyan");
colorButton[2]=new JRadioButton("Red");
colorButton[3]=new JRadioButton("Green");
colorButton[4]=new JRadioButton("Magenta");

// Instantiate a ButtonGroup object and a ColorListener object

ButtonGroup group=new ButtonGroup();
ColorListener listener = new ColorListener();
 for(int i = 0; i <colorButton.length; i++)
 {group.add(colorButton[i]);
 colorButton[i].addActionListener(listener);
 colorButton[i].setBackground(Color.white);
 colorButton[i].addActionListener(listener);
   add(colorButton[i]);
 }

add(heading);
setBackground (Color.yellow);
setPreferredSize (new Dimension (WIDTH, HEIGHT));
 }






// Set up the panel

// Group the radio buttons, add a ColorListener to each,
 // set the background color of each and add each to the panel.
}
// **************************************************************
// Represents the listener for the radio buttons.
// **************************************************************
private class ColorListener implements ActionListener
{
// --------------------------------------------------------
// Updates the background color of the panel based on
// which radio button is selected.
// --------------------------------------------------------

public void actionPerformed (ActionEvent event)
{

Object source = event.getSource();


if (source==colorButton[i])
{setBackground(colorButton[i]);
}   



}



}
}

}

4 个答案:

答案 0 :(得分:1)

您可以将数组colorButton的每个元素作为参数传递给ColorListener类的构造函数:

private class ColorListener implements ActionListener
{
    private JRadioButton rdoButton;
    public ColorListener(JRadioButton rdoButton)
    {
        this.rdoButton = rdoButton;
    }

    @Override
    public void actionPerformed(ActionEvent event)
    {
         Object source = event.getSource();
         if(source == rdoButton) //...
    }
}

然后您就可以使用它:

ColorListener listener = new ColorListener(colorButton[i]);
colorButton[i].addActionListener(listener);

或者,您可以使用ActionCommand(请参阅this example)。

答案 1 :(得分:1)

由于ColorListener是私有的,因此无法从其他任何地方看到它,所以只需将其更改为ColorOptionsPanel类的嵌套类。这样,ColorOptionsPanel的私有字段将显示在ColorListener

public class ColorOptionsPanel extends JPanel {
    private JRadioButton [] colorButton= new JRadioButton[color.length];
    //....
    private class ColorListener implements ActionListener{
    // --------------------------------------------------------
    // Updates the background color of the panel based on
    // which radio button is selected.
    // --------------------------------------------------------
    public void actionPerformed (ActionEvent event){
        Object source = event.getSource();
        if (source==colorButton[i]){setBackground(colorButton[i]);}   
    }
}

答案 2 :(得分:0)

您未在i方法中初始化actionPerformed()。留意。

答案 3 :(得分:0)

  1. colorButton仅限于ColorOptionsPanel
  2. ColorListener位于ColorOptionsPanel的外部,因此只能访问ColorOptionsPanel的公共成员