我一直試圖在JPanel中添加一組複雜的元素,根據返回的列數將它們展開 - 並將其顯示在生成的新面板中點擊一個按鈕向JPanel添加元素,但它們不顯示
我做錯了什麼,但無法弄清楚它是什麼。 替換標題面板中的其他文本元素工作得很好,但看不出爲什麼我生成的代碼只是沒有被添加到面板(或者更可能 - 面板沒有以可訪問的方式顯示)
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;
}
按鈕碼添加和改變在面板和框架元件(幀是主顯示器面板是標題,位於北邊界佈局。加入並正確顯示 三個組合框和I」 m使用很多相同的機制來生成並顯示它們,因爲我在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;
}
我敢肯定,這是簡單的,但老實說,我看不出爲什麼面板沒有被添加 道歉的代碼 - 它已經有幾年以來我寫任何GUI代碼從零開始Java中
您是否對框架使用了'null'佈局,我的意思是'frame.setLayout(null)'?並且您是否將面板添加到了構架中的框架中 – Azad
考慮爲[更好的幫助製作[SSCCE](http://www.sscce.org),代碼太多 – nachokk
嘗試添加'frame.getContentPane()在frame.getContentPane()。add(content_panel,BorderLayout.CENTER)之後的'。,'和'frame.getContentPane()。repaint()'' – MadProgrammer