向JPanel添加元素但它们没有显示

时间:2013-08-29 00:35:53

标签: java swing

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

我做错了什么,但无法弄清楚它是什么。 替换标题面板中的其他文本元素非常正常,但无法理解为什么我生成的代码没有被添加到面板中(或者更可能的是 - 面板不是以可访问的方式显示)< / p>

            My JPanel output code - 
            private JPanel getOutput() throws BadIdent {

    short[] HDformats = { HDformat, Audformat };
    short[] SDformats = { SDformat, Audformat };
    List poolInfo;
    List freeSpaceHD = null;
    List freeSpaceSD = null;
    System.out.println(man.getZoneNumberName());
    // System.out.println(man.getPoolInfo());
    poolInfo = man.getPoolInfo();
    List poolSpace = man.getPoolSpace();
    if ((Short) HDformat != null) {
        freeSpaceHD = man.getFreeSpace(HDformats);
    }
    if ((Short) SDformat != null) {
        freeSpaceSD = man.getFreeSpace(SDformats);
    }
    JPanel content_panel = new JPanel(new GridLayout(poolInfo.size(), 4));
    JLabel[] PoolInfoLabel = new JLabel[poolInfo.size()];
    JLabel[] PoolSpaceLabel = new JLabel[poolInfo.size()];
    JLabel[] PoolSpaceHDLabel = new JLabel[poolInfo.size()];
    JLabel[] PoolSpaceSDLabel = new JLabel[poolInfo.size()];
    JPanel[] PoolInfo = new JPanel[poolInfo.size()];
    // GridBagConstraints gbc_lblPoolInfo[] = new
    // GridBagConstraints[poolInfo
    // .size()];
    // JLabel[] PoolInfoLabel = new JLabel[poolInfo.size()];
    for (int i = 0; i < poolInfo.size(); i++) {
        PoolInfoLabel[i] = new JLabel();
        PoolSpaceLabel[i] = new JLabel();
        PoolSpaceHDLabel[i] = new JLabel();
        PoolSpaceSDLabel[i] = new JLabel();
        PoolInfoLabel[i].setText((String) poolInfo.get(i));
        System.out.println(poolInfo.get(i));
        PoolSpaceLabel[i].setText((String) poolSpace.get(i));
        PoolSpaceHDLabel[i].setText((String) freeSpaceHD.get(i));
        PoolSpaceSDLabel[i].setText((String) freeSpaceSD.get(i));

        //
        // System.out.println(PoolInfoLabel[i].getText());

    }
    for (int i = 0; i < PoolInfoLabel.length; i++) {
        PoolInfo[i] = new JPanel();
        PoolInfo[i].add(PoolInfoLabel[i]);
        PoolInfo[i].add(PoolSpaceLabel[i]);
        PoolInfo[i].add(PoolSpaceHDLabel[i]);
        PoolInfo[i].add(PoolSpaceSDLabel[i]);
        System.out.println(PoolSpaceLabel[i].getText());
        System.out.println(PoolSpaceSDLabel[i].getText());
        System.out.println(PoolSpaceHDLabel[i].getText());
    }
    for (int i = 0; i < PoolInfo.length; i++) {
        content_panel.add(PoolInfo[i]);
    }
    return content_panel;
}

用于在面板和框架中添加和更改元素的按钮代码(框架是主显示,面板是标题,位于NORTH边框布局中。 三个组合框被正确添加和显示,我正在使用相同的机制来生成和显示它们,因为我在getOutput()函数中使用

    JButton ButtonSubmit = new JButton("Connect");
    ButtonSubmit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                man.ManagerConnect(ISAHostName.getText(), null);
                // System.out.println(man.getZoneNumberName());
                ZoneNumName.setText(man.getZoneNumberName());
                AudFormat = getAudCombo();
                HDVidFormat = getHDCombo();
                SDVidFormat = getSDCombo();
                panel.add(HDVidFormat, gbc_HDVidFormat);
                panel.add(SDVidFormat, gbc_SDVidFormat);
                panel.add(AudFormat, gbc_AudFormat);
                btnFormatButton.setEnabled(true);
                frame.repaint();

            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }

    });
    panel.add(ButtonSubmit, gbc_ButtonSubmit);
    btnFormatButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                JPanel content_panel = getOutput();
                frame.getContentPane().add(content_panel,
                        BorderLayout.CENTER);
            } catch (BadIdent e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }

    });
    panel.add(btnFormatButton, gbc_btnNewButton);

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

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;
}

我确信它很简单,但老实说我不明白为什么没有添加面板 为代码道歉 - 自从我在Java编写任何GUI代码以来已经过了几年

1 个答案:

答案 0 :(得分:0)

使用frame.setLayout(null);

将布局设置为null

做MadProgrammer所说的

  

尝试添加frame.getContentPane()。invalidate();, frame.getContentPane()。validate();在frame.getContentPane()之后的frame.getContentPane()。repaint()。添加(content_panel,BorderLayout.CENTER) - MadProgrammer 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移动到该位置,并根据高度和宽度设置其边界。