使用AWT Panel的静态嵌套类下拉列表

时间:2011-02-07 19:00:45

标签: java

免责声明,这是我坚持的家庭作业。请指出我正确的方向。

我的嵌套类遇到问题。我基本上必须创建一个嵌套的静态类,通过java.AWT Panel生成一个下拉列表。

这是代码:(稍微更新我的代码......仍然困惑于')

package ui.panels;

import interfaces.Resettable;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import shapes.Shape;
import model.Model;

public class MainPanel extends Panel implements Resettable{
    ActionPanel actionPanel;
    ControlsPanel controlsPanel;
    private ColorPanel colorPanel;

    private void init() {
        colorPanel = new ColorPanel();
    }

    public MainPanel(Model model) {
        actionPanel = new ActionPanel(model);
        controlsPanel = new ControlsPanel(model);

        setLayout(new GridLayout(2,1));
        add(controlsPanel);
        add(actionPanel);

    }

     public void resetComponents() {
         controlsPanel.resetComponents();
         actionPanel.resetComponents();
     }

     public static class ColorPanel {

       public final static String BLACK = "Black";
       public final static String BLUE = "Blue";
       public final static String GREEN = "Green";
       public final static String RED = "Red";
       public final static String YELLOW = "Yellow";
       public final static String Magenta = "Magenta";

       private static String[] color_selections = {"Black","Blue","Green","Red","Yellow","Magenta"};
       String msg = "";

          // now create list panel
          public ColorPanel(){
            Choice myChoice = new Choice();

            for (String msg : color_selections) {
              myChoice.add(msg);
            }
            myChoice.addItemListener(new ItemListener() {
              public void itemStateChanged(ItemEvent e) {

                //do something here when item is selected

              }
          });
            this.add(myChoice); //here is my problem. I don't know what this should say
          }

     }
}

5 个答案:

答案 0 :(得分:1)

静态嵌套类没有对包含类实例的隐式引用。如果要求是将嵌套类设置为static,则需要在构造函数中提供对包含实例的显式引用。即:

public ColorPanel(Model mdl, MainPanel main)

答案 1 :(得分:0)

您正在创建一个静态内部类。您不能从静态内部类引用外部类。你必须只使用一个内部类。尝试从公共静态类ColorPanel {declaration。

中删除静态字段

注意:可能还有其他问题,这只是解决了您提到的问题。

编辑:更具体地说,当你调用this.add(选择)时;您正在尝试调用扩展JPanel的第一个类的特定于实例的方法。您尝试调用的方法是在JPanel中,但由于您的内部类是静态的,因此它没有引用您的外部类内部。

答案 2 :(得分:0)

内部和静态内部类有各种范围规则。查看总结范围规则的reference card of this post(在文章的底部)。

答案 3 :(得分:0)

我没有在任何类中看到add方法。您是否发布了正确的代码段?

如果add是类MainPanel中的方法,则必须是静态的,ColorPanel才能访问它,因为它也是静态类。

答案 4 :(得分:0)

好的,这是答案:结果我必须扩展嵌套的静态类,所以第39行......

公共静态类ColorPanel {}

变为

公共静态类ColorPanel扩展了Panel {}

以下是我的代码的完整列表....

包ui.panels;

import interfaces.Resettable; import java.awt.Choice; import java.awt.Color; import java.awt.Component; import java.awt.GridLayout; import java.awt.Panel; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; 进口形状。形状; import model.Model;

公共类MainPanel扩展Panel实现Resettable {     ActionPanel actionPanel;     ControlsPanel controlsPanel;     私人ColorPanel colorPanel;

public MainPanel(Model model) {
    actionPanel = new ActionPanel(model);
    controlsPanel = new ControlsPanel(model);
    colorPanel = new ColorPanel();

    setLayout(new GridLayout(2,1));
    add(controlsPanel);
    add(actionPanel);
    add(colorPanel);

}

 public void resetComponents() {
     controlsPanel.resetComponents();
     actionPanel.resetComponents();
 }

//家庭作业从这里开始。 //静态嵌套类ColorPanel,它为两个Color选择框

创建一个面板(和侦听器)
 public static class ColorPanel extends Panel{

   public final static String BLACK = "Black";
   public final static String BLUE = "Blue";
   public final static String GREEN = "Green";
   public final static String RED = "Red";
   public final static String YELLOW = "Yellow";
   public final static String Magenta = "Magenta";

   private static String[] color_selections = {"Black","Blue","Green","Red","Yellow","Magenta"};
   String msg = "";

//如果选择的颜色等于“黑色”,那么现在是if,然后将colorVariable设置为color.black           private String selectedColor;           private Shape currentColor;           模型模型;           选择lineColor;           选择fillColor;

      public Shape createShapecolor() {
        if(selectedColor == BLUE){
          Color currentColor = Color.blue;
        }
        return currentColor;
      }

      // now create list panel
      public ColorPanel(){
        lineColor = new Choice();
        fillColor = new Choice();

        for (String msg : color_selections) {
          lineColor.add(msg);
          fillColor.add(msg);
        }
        lineColor.addItemListener(new ItemListener() {
          public void itemStateChanged(ItemEvent e) { 
            //reset line color here when item is selected
              createShapecolor();
            //how to take returned currentColor value and assign it to objects line color?  

              repaint();
          }
        });

        fillColor.addItemListener(new ItemListener() {
          public void itemStateChanged(ItemEvent e) { 
            //overload fill color here when item is selected
              repaint();
          }
        });

        this.add(lineColor);//this line is driving me nuts.
        //this.add(fillColor);
      }

 }

}