开发者

Static nested class drop down list using AWT Panel

开发者 https://www.devze.com 2023-02-08 21:20 出处:网络
Disclaimer, this is a homework exercise I\'m stuck on. Please point me in the right direction. I\'m having trouble with my nested class. I basically have to create a nested static class that generate

Disclaimer, this is a homework exercise I'm stuck on. Please point me in the right direction.

I'm having trouble with my nested class. I basically have to create a nested static class that generates a drop down list via java.AWT Panel.

Here is the code: (slight update to my code...still confused tho')

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
          }

     }
}


A static nested class does not have an implicit reference to the containing class instance. If the requirement is to have the nested class static, you'll need to provide an explicit reference to the containing instance in the constructor. I.e:

public ColorPanel(Model mdl, MainPanel main)


You're creating a static inner class. You can't reference the outside class from a static inner class. You'll have to use just a inner class. Try removing the static field from the public static class ColorPanel { declaration.

NOTE: There might be other problems, this just addresses the problem you mentioned.

EDIT: More specifically, when you call this.add(selection); You are trying to call the instance-specific method of your first class that extends JPanel. The method you are attempting to call is in JPanel but since your inner class is static, it has no reference to your outer classes innards.


There are various scoping rules for inner and static inner classes. Look at the reference card of this post (at the bottom of the article) that summarizes the scoping rules.


I don't see an add method in any of the classes. Have you posted the correct code snippet?

If add is a method in class MainPanel it has to be static for it to be accessible by ColorPanel, since that's also a static class.


Ok, here is the answer: Turns out I had to extend the nested static class so line 39...

Public static class ColorPanel {}

becomes

Public static class ColorPanel extends Panel {}

Below is the entire listing of my code....

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;

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();
 }

// Homework starts here. // static nested class ColorPanel which creates a panel (and listeners) for two Color choice boxes

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

// now an if thing that goes if selected color equals "black" then set colorVariable to color.black private String selectedColor; private Shape currentColor; Model model; 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

精彩评论

暂无评论...
验证码 换一张
取 消