gpt4 book ai didi

java - 使用 AWT 面板的静态嵌套类下拉列表

转载 作者:行者123 更新时间:2023-12-01 15:56:38 25 4
gpt4 key购买 nike

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

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

}
}

最佳答案

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

public ColorPanel(Model mdl, MainPanel main)

关于java - 使用 AWT 面板的静态嵌套类下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4925286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com