gpt4 book ai didi

java - 将元素添加到 JPanel 但它们不显示

转载 作者:行者123 更新时间:2023-11-29 08:59:16 25 4
gpt4 key购买 nike

我一直在尝试在 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;
}

在面板和框架中添加和更改元素的按钮代码(框架是主要显示,面板是标题,位于 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 代码了

最佳答案

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

按照 MadProgrammer 所说的去做

Try adding frame.getContentPane().invalidate();, frame.getContentPane().validate(); and frame.getContentPane().repaint() after frame.getContentPane().add(content_panel, BorderLayout.CENTER) – MadProgrammer Aug 29 at 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 移动到该位置并根据高度和宽度设置其边界。

关于java - 将元素添加到 JPanel 但它们不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18500537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com