2011-11-23 39 views
1

爲什麼public void actionPerformed (ActionEvent event), colorButton[]突出顯示無法找到符號或變量?ActionListener無法從公共類中引用數組?

如何調試?我試圖做出定義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]); 
} 



} 



} 
} 

} 

回答

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]);} 
    } 
} 
+0

THX我解決了這個問題。 =) –

+0

更改爲嵌套的一切工作就好了 –

+0

偉大的工作。祝好運與其他:) – GETah

0

你沒有在actionPerformed()方法來初始化i。保持注意。

0
  1. colorButton是私有的ColorOptionsPanel
  2. ColorListener是外部ColorOptionsPanel所以只能訪問ColorOptionsPanel
  3. 的公共成員
+0

我試圖改變我的實例化colorButton和顏色陣列公衆,但它仍然指出「無法找到符號」 varaible colorButton –

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)。