我写了一个自定义对话框的类来接收参数,调试的时候对话框没有任何问题,但放到主程序调用的时候,对话框里面的所有控件都显示不了, setVisible(true); 这个语句放到哪里都没用。
在网上查了很多资料,还是没有解决,望望各位大神指条明路,谢谢
代码如下:
public class ParaRequestDialog extends JDialog {
private Map<String, String> para;
private boolean isReceived = false;
private Map<String, JTextField> paraTexts = new HashMap<>();
public ParaRequestDialog(Map<String, String> para) {
this.para = para;
int num = para.size() - 1;
int height = num * 35 + 80;
setLayout(null);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setTitle("请输入以下参数值:");
setBounds( 200,300,290, height);
int i = -1;
for (String key : para.keySet()) {
if (key.equals("isReceived"))
continue;
i++;
JLabel label = new JLabel(key + ":");
label.setBounds(20, i * 35 + 10, 50, 25);
JTextField text = new JTextField();
text.setBounds(75, i * 35 + 10, 180, 25);
paraTexts.put(key, text);
add(label);
add(text);
}
JButton confirm = new JButton("确定");
confirm.addActionListener(this::ConfirmAction);
JButton cancel = new JButton("取消");
cancel.addActionListener(this::CancelAction);
confirm.setBounds(60, height - 70, 60, 25);
cancel.setBounds(150, height - 70, 60, 25);
add(confirm);
add(cancel);
setVisible(true);
}
private void ConfirmAction(ActionEvent actionEvent) {
for (String key : paraTexts.keySet()) {
para.put(key, paraTexts.get(key).getText());
}
para.put("isReceived", "true");
isReceived = true;
this.dispose();
}
private void CancelAction(ActionEvent actionEvent) {
para.put("isReceived", "false");
isReceived = false;
this.dispose();
}
public Map<String, String> getPara() { return para; }
public boolean isReceived() { return isReceived; }
}