2011-02-07 36 views
0

免责声明,这是我坚持的作业练习。请指向正确的方向。使用AWT面板的静态嵌套类下拉列表

我遇到了我的嵌套类的问题。我基本上必须创建一个嵌套的静态类,通过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 
      } 

    } 
} 
+1

“有问题”。要求描述问题是否太多? – meriton 2011-02-07 19:08:00

回答

0

好,这里是答案:原来我有这么线39条嵌套静态类...

公共静态类颜色面板{}

成为

公共静态类颜色面板延伸面板{}

下面是我的代码的整个列表....

包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; 导入模型。模型;

公共类MainPanel扩展面板实现可复位{动作面板动作面板; ControlsPanel controlsPanel; private 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(); 
} 

//作业从这里开始。 // Static Nested Class和颜色面板,其创建两种颜色选择框

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 = ""; 

//现在的如果去,如果选择的颜色等于“黑”的事情,然后设置colorVariable到color.black 面板(和听众) private String selectedColor; private Shape currentColor; 模型模型; Choice lineColor; Choice 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); 
     } 

} 

}

0

你要创建一个静态内部类。你不能从静态内部类引用外部类。你只能使用一个内部类。尝试从公共静态类ColorPanel {声明中移除静态字段。

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

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

1

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

public ColorPanel(Model mdl, MainPanel main) 
0

对于内部和内部静态内部类有各种范围规则。请看reference card of this post(文章的底部),其中总结了范围规则。

0

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

如果add是类MainPanel中的方法,它必须是静态的,因为它可以被ColorPanel访问,因为这也是一个静态类。