English 中文(简体)
行动生计者能够从公共部门中排出参考阵列?
原标题:ActionListener can t reference array from public class?

为什么<条码> 公开无效行动 演练(活动),光彩/密码,强调无法找到象征或变数?

我是如何去掉的? 页: 1 表演(活动)

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是私人的,因此不能从任何其他地方看到,只是将其改为<编码>ColorOptions。 小组。 这样,私人领域<代码> 小组将从<编码>中查找。

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]);}   
    }
}
问题回答

您可将每一阵列要素作为理由通过<代码>colorButton至的构造者。 班级:

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) //...
    }
}

and then you can use it like:

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

或者,你可以使用<条码>行动通知/代码>(见 rel=“nofollow”>,这一实例

您在<代码>i上做了初步编辑(<>>>)。 关注。

  1. colorButton is private to ColorOptionsPanel
  2. ColorListener is external to ColorOptionsPanel so can only access public members of ColorOptionsPanel




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签