【问题标题】:JDialog modal problem java guiJDialog模态问题java gui
【发布时间】:2011-01-04 19:39:49
【问题描述】:

我有这段代码运行没有任何错误,但它没有按我计划的方式运行。我试图弄清楚为什么当我单击标有“重要!!”的按钮时没有显示我的“继续”按钮。

唯一显示的是一个空白弹出窗口,它是代码 JDialog 的一部分,它设置为模态且可见。我就是想不通。如果有人可以帮助我,我将不胜感激。

JPanel hehePanel = new JPanel(new FlowLayout(FlowLayout.RIGHT,10,20));
JDialog dialog = new JDialog((JFrame)null);
dialog.getContentPane().add(hehePanel,BorderLayout.CENTER);
JButton hButton = new JButton("important!!");
JButton fButton = new JButton(" on construction !!");
JButton exitButton = new JButton("EXIT CAW ");
hehePanel.add(hButton);
hButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        final JPanel hehePanel = new JPanel();
        final JDialog dialog = new JDialog();
        dialog.getContentPane().add(hehePanel,BorderLayout.PAGE_END);
        dialog.toFront();
        dialog.setModal(true);
        dialog.pack();
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
        JButton closebutton = new JButton("Continue");
        closebutton.setActionCommand("continue");
        closebutton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                if (evt.getActionCommand().equals("continue")) {
                    dialog.dispose();
                }
            }
        });
        hehePanel.add(closebutton);
    }
});

hehePanel.add(fButton);
hehePanel.add(exitButton);

【问题讨论】:

    标签: java swing user-interface modal-dialog jdialog


    【解决方案1】:

    您需要将所有组件添加到对话框在使对话框可见之前以及在您pack() 对话框之前。

    所有代码之后 dialog.setVisible(true) 在对话框关闭之前不会执行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-02
      • 2010-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多