2

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

唯一显示的是一个空白弹出窗口,它是代码 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);
4

1 回答 1

5

您需要将所有组件添加到对话框中,然后才能使对话框可见并且在pack()对话框之前。

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

于 2011-01-04T20:11:43.243 回答