Java 向JPanel添加元素,但它们不是';t显示

Java 向JPanel添加元素,但它们不是';t显示,java,swing,Java,Swing,我一直在尝试在JPanel中添加一组复杂的元素,根据返回的列数进行布局,并在一个新面板中显示,该面板通过单击按钮生成 我做错了什么,但不知道是什么。 替换header面板中的其他文本元素效果绝对不错,但是看不出为什么我生成的代码没有添加到面板中(或者更可能的是,面板没有以可访问的方式显示) 供参考-获取高清格式组合框 private JPanel getHDCombo() { JComboBox combo = new JComboBox(); combo.setFont(new

我一直在尝试在JPanel中添加一组复杂的元素,根据返回的列数进行布局,并在一个新面板中显示,该面板通过单击按钮生成

我做错了什么,但不知道是什么。 替换header面板中的其他文本元素效果绝对不错,但是看不出为什么我生成的代码没有添加到面板中(或者更可能的是,面板没有以可访问的方式显示)

供参考-获取高清格式组合框

private JPanel getHDCombo() {
    JComboBox combo = new JComboBox();
    combo.setFont(new Font("Tahoma", Font.PLAIN, 10));
    combo.setMaximumRowCount(10);
    final Map<String, Integer> map = man.HDVidFormats();
    Collection<String> keys = map.keySet();
    Iterator<String> it = keys.iterator();
    while (it.hasNext()) {
        String key = it.next();
        combo.addItem(key);
    }
    combo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JComboBox combo = (JComboBox) e.getSource();
            String key = (String) combo.getSelectedItem();
            int format = map.get(key);
            System.out.println(format);
            HDformat = (short) format;
        }
    });
    JPanel panel = new JPanel();
    panel.setBackground(new Color(0, 0, 0, 0));
    panel.add(combo);
    return panel;
}
private JPanel getHDCombo(){
JComboBox组合=新的JComboBox();
setFont(新字体(“Tahoma”,Font.PLAIN,10));
combo.setMaximumRowCount(10);
最终地图Map=man.HDVidFormats();
集合键=map.keySet();
Iterator it=keys.Iterator();
while(it.hasNext()){
String key=it.next();
组合添加项(键);
}
combo.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
JComboBox组合=(JComboBox)e.getSource();
字符串键=(字符串)组合。getSelectedItem();
int format=map.get(键);
System.out.println(格式);
HDformat=(短)格式;
}
});
JPanel面板=新的JPanel();
面板.立根(新颜色(0,0,0,0));
面板。添加(组合);
返回面板;
}
我相信这很简单,但我真的不明白为什么没有添加面板
为代码道歉-我已经好几年没有用Java从头开始编写任何GUI代码了

使用
frame.setLayout(null)将布局设置为null

照我说的做

尝试添加frame.getContentPane().invalidate();,frame.getContentPane().validate();和frame.getContentPane().repaint()在frame.getContentPane()之后添加(content_面板,BorderLayout.CENTER)–8月29日0:42

关于你的第二个问题,加上这个

frame.add(*jpanel here*, BorderLayout.CENTER);
*jpanel*.setBounds(width,height,x,y); //check up on this one. i think the param might be backwards

设置的边界应将您的jpanel移动到该位置,并根据高度和宽度设置其边界。

您是否为框架使用了
null
布局,我的意思是
frame.setLayout(null)
?您是否已将面板添加到构造函数中的框架考虑到要获得更好的帮助,代码太多请尝试添加
frame.getContentPane().invalidate()
frame.getContentPane().validate()
frame.getContentPane().repaint()
frame.getContentPane().add之后添加(content\u panel,BorderLayout.CENTER)
此外,您不需要
frame.getContentPane()
frame.add
将委托给
getContentPane()
确定-我已将我的内容添加到面板(即标题)虽然不可原谅的凌乱,但内容就在那里——因此它看起来像是一个边界问题,或者是将内容面板直接添加到框架中的问题。我可能会在框架中添加一个新面板,并将面板嵌入其中——但希望不大——框架上的重新绘制似乎会将内容面板的大小有效地设置为“0x0”
private JPanel getHDCombo() {
    JComboBox combo = new JComboBox();
    combo.setFont(new Font("Tahoma", Font.PLAIN, 10));
    combo.setMaximumRowCount(10);
    final Map<String, Integer> map = man.HDVidFormats();
    Collection<String> keys = map.keySet();
    Iterator<String> it = keys.iterator();
    while (it.hasNext()) {
        String key = it.next();
        combo.addItem(key);
    }
    combo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JComboBox combo = (JComboBox) e.getSource();
            String key = (String) combo.getSelectedItem();
            int format = map.get(key);
            System.out.println(format);
            HDformat = (short) format;
        }
    });
    JPanel panel = new JPanel();
    panel.setBackground(new Color(0, 0, 0, 0));
    panel.add(combo);
    return panel;
}
frame.add(*jpanel here*, BorderLayout.CENTER);
*jpanel*.setBounds(width,height,x,y); //check up on this one. i think the param might be backwards