2011-02-07 3 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

+0

필자는이 서식을 지정했습니다 (가능한 경우 코드 서식을 지정해야합니다). 대괄호가 맞는지 확실하지 않습니다. 제목의 요점이라고 생각할 때 확인하십시오. –

+0

정확한 오류 메시지를 게시하십시오 - "문제가 있습니다"가 부족합니다. –

답변

0

좋아, 여기에 대답은 확장 패널 {} 아래

가 ....

패키지 ui.panels 내 코드의 전체 목록입니다;

import interfaces.Resettable; 가져 오기 java.awt.Choice; 가져 오기 java.awt.Color; 가져 오기 java.awt.Component; 가져 오기 java.awt.GridLayout; 가져 오기 java.awt.Panel; 가져 오기 java.awt.event.ItemEvent; 가져 오기 java.awt.event.ItemListener; 가져 오기 shapes.Shape; 가져 오기 모델입니다.모델;

공용 클래스 MainPanel extends Panel 구현 가능 재설정 가능 { ActionPanel actionPanel; 컨트롤 패널 컨트롤 패널; 전용 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(); 
} 

// 숙제가 여기에서 시작됩니다. 두 가지 색상 선택 상자

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의하기위한 패널 (청취자)을 생성 // 정적 중첩 클래스 ColorPanel private String selectedColor; 개인 모양 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); 
     } 

} 

}

0

당신은 정적 내부 클래스를 만드는 : 여기

는 코드입니다. 정적 인 내부 클래스에서 외부 클래스를 참조 할 수 없습니다. 내부 클래스 만 사용해야합니다. 정적 정적 클래스 ColorPanel {선언에서 정적 필드를 제거해보십시오.

참고 : 다른 문제가있을 수 있습니다. 이는 언급 한 문제를 해결합니다.

편집 :보다 구체적으로 this.add (선택); JPanel을 확장하는 첫 번째 클래스의 인스턴스 별 메서드를 호출하려고합니다. 호출하려는 메서드는 JPanel에 있지만 내부 클래스가 정적이므로 외부 클래스 내부에 대한 참조가 없습니다.

1

정적 중첩 클래스에는 포함하는 클래스 인스턴스에 대한 암시 적 참조가 없습니다. 요구 사항이 중첩 클래스를 정적으로 유지하려는 경우 생성자의 포함 인스턴스에 대한 명시 적 참조를 제공해야합니다. 예 :

public ColorPanel(Model mdl, MainPanel main) 
0

내부 및 정적 내부 클래스에 대한 다양한 범위 지정 규칙이 있습니다. 스코프 규칙을 요약 한 reference card of this post (기사 하단)을보십시오.

0

어떤 클래스에도 add 메서드가 표시되지 않습니다. 올바른 코드 스 니펫을 게시 했습니까?

addMainPanel의 메소드 인 경우 정적 인 클래스이기 때문에 ColorPanel에 액세스 할 수 있도록 정적이어야합니다. ... 내가 그렇게 라인 (39) 중첩 정적 클래스를 확장했다 밝혀

공공 정적 클래스 ColorPanel {}

공공 정적 클래스 ColorPanel된다 :