java - ActionListener 无法从公共(public)类引用数组?

标签 java swing actionlistener jradiobutton

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

如何调试它?我试图在 public void actionPerformed (ActionEvent event)

中定义 colorButton[]
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]);
}   



}



}
}

}

最佳答案

由于 ColorListener 是私有(private)的,因此从外部任何其他地方都看不到它,因此只需将其更改为 ColorOptionsPanel 类的嵌套类即可。这样,ColorOptionsPanel 的私有(private)字段将从 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]);}   
    }
}

关于java - ActionListener 无法从公共(public)类引用数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8240756/

相关文章:

java - 在类数组中使用 'contains' 方法

Java ImageIcons 和 Action 监听器

java - 在 Java 中从 MySQL 获取不同的行

java - 如何从数组列表获取和设置价格?

java - JFrame 和矩形

java - 尝试运行一个简单的 Java ActionListener 示例并收到错误?

java - 如何识别在当前选择的单选按钮之前选择了哪个单选按钮?

java - 将字节数组写入oracle中的原始列

java - Spring - 以编程方式生成一组 bean

java - 从 php 向 java 发送消息