zcsQQ1415 2021-02-07 21:33 采纳率: 0%
浏览 89
已结题

Java Swing 调用了自定义对话框,对话框里面的内容为什么显示不了??

我写了一个自定义对话框的类来接收参数,调试的时候对话框没有任何问题,但放到主程序调用的时候,对话框里面的所有控件都显示不了, 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; }
}
  • 写回答

3条回答 默认 最新

  • 歇歇 2021-02-07 21:50
    关注

    自定义框代码呢?

    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改