Java 难以填充JComboBox

Java 难以填充JComboBox,java,swing,jcombobox,Java,Swing,Jcombobox,几周前,我开始为一个个人项目编程(MKUltra学习在美国政治竞选中通过潜意识信息编码),所以不要犹豫,像野兽一样撕碎我愚蠢的错误,撕碎最温柔的年轻镜头 我试图从ArrayList填充一个JComboBox,我已经在这里讨论了一系列可能的解决方案和类似的问题,并得出了一个似乎应该有效的解决方案-从ArrayList填充一个模型,然后用该模型实例化ComboBox-但没有。以下是片段: public class Window { public Window() {

几周前,我开始为一个个人项目编程(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);
    }
}
公共类窗口{
公共窗口(){
JFrame=新JFrame();
setExtendedState(JFrame.MAXIMIZED_二者);
frame.getContentPane().setForeground(颜色为深灰色);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ArrayList yearList=新建ArrayList();
年鉴。添加(“2019”);
StackExchangeQ.设定年份(年表);
StackExchangeQ fakePanel=新的StackExchangeQ();
框架。添加(假面板);
frame.pack();
frame.setVisible(true);
}
}
公共类StackExchangeQ扩展了JPanel{
私有静态ArrayList yearList=新ArrayList();
公共静态void setYears(ArrayList userYears){
对于(int i=0;i
基本上,它应该显示从textfield输入中提取并输入到yearList ArrayList中的年份列表。我已经检查了信息流(?)直到填充模型,在我尝试填充组合框时失败


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

问题在于:
public void StackExchangeQ()
。不应该是
公共StackExchangeQ()
(构造函数),以便在创建对象时启动面板?否则,您必须:

StackExchangeQ fakePanel = new StackExchangeQ();
fakePanel.StackExchangeQ();
frame.add(fakePanel);
此外,您不需要将
年表
声明为
静态
。实际上,你甚至不必有
年鉴
。只需将组合框的模型声明为一个字段,并将字符串直接填充到模型

我的意思是:

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());
    }
}
公共类窗口{
公共静态类StackExchangeQ扩展了JPanel{
私有DefaultComboxModelYearModel;
公共无效设置年数(ArrayList用户年数){
yearModel.removeAllElements();
userYears.forEach(yearModel::addElement);
}
公共StackExchangeQ(){
此.setLayout(新的BorderLayout());
JPanel menuPanel=newjpanel(newflowlayout(FlowLayout.LEFT));
添加(menuPanel,BorderLayout.PAGE_START);
yearModel=新的DefaultComboxModel();
JComboBox yearSelect=新的JComboBox(yearModel);
menuPanel.add(yearSelect,BorderLayout.PAGE_START);
}
}
公共窗口(){
JFrame=新JFrame();
frame.getContentPane().setForeground(颜色为深灰色);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ArrayList yearList=新建ArrayList();
年鉴。添加(“全部”);
年鉴。添加(“2019”);
StackExchangeQ fakePanel=新的StackExchangeQ();
伪造专家组。设定年份(年表);
框架。添加(假面板);
frame.pack();
frame.setVisible(true);
}
公共静态void main(字符串[]args){
调用器(()->new Window());
}
}

问题在这里:
public void StackExchangeQ()
。不应该是
公共StackExchangeQ()
(构造函数),以便在创建对象时启动面板?否则,您必须:

StackExchangeQ fakePanel = new StackExchangeQ();
fakePanel.StackExchangeQ();
frame.add(fakePanel);
此外,您不需要将
年表
声明为
静态
。实际上,你甚至不必有
年鉴
。只需将组合框的模型声明为一个字段,并将字符串直接填充到模型

我的意思是:

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());
    }
}
公共类窗口{
公共静态类StackExchangeQ扩展了JPanel{
私有DefaultComboxModelYearModel;
公共无效设置年数(ArrayList用户年数){
yearModel.removeAllElements();
userYears.forEach(yearModel::addElement);
}
公共StackExchangeQ(){
此.setLayout(新的BorderLayout());
JPanel menuPanel=newjpanel(newflowlayout(FlowLayout.LEFT));
添加(menuPanel,BorderLayout.PAGE_START);
yearModel=新的DefaultComboxModel();
JComboBox yearSelect=新的JComboBox(yearModel);
menuPanel.add(yearSelect,BorderLayout.PAGE_START);
}
}
公共窗口(){
JFrame框架