难以填充 JComboBox

Difficulty populating a JComboBox

我几周前开始为一个个人项目编程(MKUltra 在美国政治运动中通过潜意识信息学习编码),所以不要犹豫,像野兽撕裂最温柔的年轻人一样撕裂我的愚蠢错误射击。

我正在尝试从 ArrayList 填充 JComboBox,我已经在此处解决了一系列可能的解决方案和类似问题,并得出了一个看起来应该可行的解决方案 - 从ArrayList 然后用该模型实例化 ComboBox - 但没有。这是片段:

public class Window {   

    public Window() {
        JFrame frame = new JFrame();        
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);      
        frame.getContentPane().setForeground(Color.DARK_GRAY);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ArrayList<String> yearList = new ArrayList<String>();
        yearList.add("2019");
        StackExchangeQ.setYears(yearList);
        StackExchangeQ fakePanel = new StackExchangeQ();        
        frame.add(fakePanel);       
        frame.pack();       
        frame.setVisible(true);
    }
}
public class StackExchangeQ extends JPanel {
private static ArrayList<String> yearList = new ArrayList<String>();

    public static void setYears(ArrayList<String> userYears) {     
        for(int i = 0; i < userYears.size(); i++) {
            yearList.add(userYears.get(i));         
        }
    }

    public void StackExchangeQ() {
        this.setLayout(new BorderLayout());
        JPanel menuPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));     
        this.add(menuPanel, BorderLayout.PAGE_START); 

        DefaultComboBoxModel<String> yearModel = new DefaultComboBoxModel<>();
        yearModel.addElement("All");
        for(int i = 1; i < yearList.size(); i++) {
            yearModel.addElement(yearList.get(i));
            System.out.println(yearModel.getElementAt(i));
        }
        JComboBox<String> yearSelect = new JComboBox<String>(yearModel);     
        menuPanel.add(yearSelect, BorderLayout.PAGE_START);
    }
}

基本上它应该显示从文本字段输入中获取并输入到 yearList ArrayList 中的年份列表。我检查了信息流 (?) 直至填充模型,但在我尝试填充组合框时它失败了。

***OKAY 在编辑它以创建一个最小的可重现示例时我发现了问题所在,因为一周前当我真的刚刚开始时我无法将 ArrayList 正确传递给 StackExchangeQ 对象出于某种原因,我的 hack 是在后者中将其设为静态字段,并在创建实例之前调用静态方法 setYears(),如您在上面的示例 lmao 中所见。使 ArrayList 成为一个非静态字段并正确传递它似乎使 ComboBox 以某种方式工作(但它对我来说仍然是一个黑盒子)。

问题在这里:public void StackExchangeQ()。不应该是 public StackExchangeQ() (构造函数) 以便在创建对象时启动面板?否则你将不得不:

StackExchangeQ fakePanel = new StackExchangeQ();
fakePanel.StackExchangeQ();
frame.add(fakePanel);

此外,您不需要将 yearList 声明为 static。实际上你甚至不必 yearList。只需将组合框的模型声明为字段并将字符串填充到模型中即可。

我的意思是:

public class Window {
    public static class StackExchangeQ extends JPanel {
        private DefaultComboBoxModel<String> yearModel;

        public void setYears(ArrayList<String> userYears) {
            yearModel.removeAllElements();
            userYears.forEach(yearModel::addElement);
        }

        public StackExchangeQ() {
            this.setLayout(new BorderLayout());
            JPanel menuPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
            this.add(menuPanel, BorderLayout.PAGE_START);

            yearModel = new DefaultComboBoxModel<>();
            JComboBox<String> yearSelect = new JComboBox<String>(yearModel);
            menuPanel.add(yearSelect, BorderLayout.PAGE_START);
        }
    }

    public Window() {
        JFrame frame = new JFrame();
        frame.getContentPane().setForeground(Color.DARK_GRAY);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ArrayList<String> yearList = new ArrayList<String>();
        yearList.add("All");
        yearList.add("2019");
        StackExchangeQ fakePanel = new StackExchangeQ();
        fakePanel.setYears(yearList);
        frame.add(fakePanel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new Window());
    }
}