2

I've been trying to add a complex set of elements in a JPanel, lay them out according to how many columns are returned - and display it in a new panel which is generated on a button click

I'm doing something wrong, but can't figure out what it is. Replacing other text elements within the header panel works absolutely fine, but can't see why my generated code just isn't being added to the panel (or more likely - the panel just isn't being displayed in a way that's accessible)

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

Button code to add and change elements in the panel and frame (frame is the main display, panel is the header, located in NORTH border layout. The three comboboxes are added and displayed correctly, and I'm using much the same mechanism to generate and display them as I'm using in the getOutput() function

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

For reference - get HD format combobox

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

I'm sure it's simple, but I honestly can't see why the panel isn't being added Apologies for the code - it's been a few years since I wrote any GUI code from scratch in Java

4

1 回答 1

0

将您的布​​局设置为空frame.setLayout(null);

做MadProgrammer说的

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

于 2013-11-01T00:26:15.643 回答