Java JDialog显示为空

Java JDialog显示为空,java,swing,Java,Swing,我试图用一个新的自定义对话框来取代JOptionPane的使用,下面是我所做的: package pk; import java.util.Enumeration; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JDialog; import javax.swing.J

我试图用一个新的自定义对话框来取代JOptionPane的使用,下面是我所做的:

    package pk;

    import java.util.Enumeration;

    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JLabel;

    import java.awt.BorderLayout;
    import java.awt.Font;

    import javax.swing.JTextArea;

    import java.awt.SystemColor;

    import javax.swing.JPanel;

    import java.awt.Color;

    import javax.swing.border.LineBorder;

    public class SIMessage extends JDialog implements ActionListener{
        private static final long serialVersionUID = 1L;
        public JButton oui=new JButton("Oui"),btnClose=new JButton(new ImageIcon("images\\logo\\delete.gif")),
        non=new JButton("Non"),annuler=new JButton("Annuler"),ok=new JButton("OK"); 
        public JLabel lblImgErr=new JLabel(new ImageIcon("images\\logo\\msgErreur.png")),
                lblImgConf=new JLabel(new ImageIcon("images\\logo\\msgQuestion.png")),
                lblImgWarning=new JLabel(new ImageIcon("images\\logo\\msgWarning.png")),
                lblImgInfo=new JLabel(new ImageIcon("images\\logo\\msgInformation.png")),
                lblImgQuestion=new JLabel(new ImageIcon("images\\logo\\msgQuestion.png")),
                lblImgIconApp=new JLabel(new ImageIcon("images\\logo\\clntIco.ico"));
        public JLabel title=new JLabel(),message=new JLabel();

        public enum TypeMessage{
            ERROR_MESSAGE,
            CONFIRMATION_MESSAGE,
            WARNING_MESSAGE,
            INFORMATION_MESSAGE,
            VALIDATION_MESSAGE
        }
        public SIMessage(JFrame parent,String title,TypeMessage type,String message) {
                 super(parent,true);
            setUndecorated(true); 
            getContentPane().setLayout(new GridLayout(1, 1));

                JPanel mainDgPanel = new JPanel();
                mainDgPanel.setBorder(new LineBorder(new Color(255, 255, 255), 3, true));
                mainDgPanel.setBounds(0, 0, 444, 156);
                getContentPane().add(mainDgPanel);
                mainDgPanel.setLayout(null);
                mainDgPanel.setBackground(Color.decode(EcranPrincipal.blueThemeCP));

                JTextArea txtrTextarea = new JTextArea(message);
                txtrTextarea.setRows(2);
                txtrTextarea.setBounds(123, 62, 340, 80);
                txtrTextarea.setFont(new Font("Iskoola Pota", Font.PLAIN, 18));
                txtrTextarea.setEditable(false);
                txtrTextarea.setFocusable(false);
                txtrTextarea.setOpaque(false);
                txtrTextarea.setBorder(null);
                txtrTextarea.setWrapStyleWord(true);
                txtrTextarea.setLineWrap(true);
                txtrTextarea.setForeground(Color.decode(EcranPrincipal.blueThemeBT));
                mainDgPanel.add(txtrTextarea);

                JPanel panelButtons = new JPanel();
                panelButtons.setBounds(47, 115, 344, 30);
                mainDgPanel.add(panelButtons);

                switch(type)
                {
                case ERROR_MESSAGE:
                    {
                        JLabel lblNewLabel =lblImgErr;
                        lblNewLabel.setBounds(10, 69, 79, 14);
                        mainDgPanel.add(lblNewLabel);

                        JButton btnOk = ok;
                        panelButtons.add(btnOk);
                    break;
                    }
                case CONFIRMATION_MESSAGE:
                    {
                         JLabel lblNewLabel =lblImgConf;
                            lblNewLabel.setBounds(10, 69, 79, 14);
                            mainDgPanel.add(lblNewLabel);

                            JButton btnOui = oui;
                            panelButtons.add(btnOui);
                    break;
                    }
                case WARNING_MESSAGE:
                    {
                         JLabel lblNewLabel =lblImgWarning;
                            lblNewLabel.setBounds(10, 69, 79, 14);
                            mainDgPanel.add(lblNewLabel);

                            JButton btnOk = ok;
                            panelButtons.add(btnOk);
                    break;
                    }
                case INFORMATION_MESSAGE:
                    {
                         JLabel lblNewLabel =lblImgInfo;
                            lblNewLabel.setBounds(10, 69, 79, 14);
                            mainDgPanel.add(lblNewLabel);

                            JButton btnOk = ok;
                            panelButtons.add(btnOk);
                    break;
                    }
                case VALIDATION_MESSAGE:
                    {
                         JLabel lblNewLabel =lblImgConf;
                            lblNewLabel.setBounds(10, 69, 79, 14);
                            mainDgPanel.add(lblNewLabel);


                            JButton btnOui = oui;
                            panelButtons.add(btnOui);

                            JButton btnNon = non;
                            panelButtons.add(btnNon);

                            JButton btnAnnuler = annuler;
                            panelButtons.add(btnAnnuler);

                    break;
                    }
                    default:
                }
                ok.addActionListener(this);
                oui.addActionListener(this);

                JPanel panel = new JPanel();
                panel.setBounds(0, 0, 444, 27);
                mainDgPanel.add(panel);
                panel.setBackground(Color.WHITE);
                panel.setLayout(null);

                JButton btnCloseDf = btnClose;
                btnCloseDf.setBounds(411, 0, 39, 23);
                panel.add(btnCloseDf);

                JLabel lblIconApp =lblImgIconApp;
                lblIconApp.setBounds(10, 4, 77, 14);
                panel.add(lblIconApp);

                JLabel lblTitle = new JLabel(title);
                lblTitle.setBounds(190, 4, 46, 14);
                panel.add(lblTitle);
                this.pack();
                this.setVisible(true);
        }
public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        Object source=e.getSource();
        if(source==oui||source==ok)
        {
            this.dispose();
        }
    }
然后我叫:

    SIMessage sm=new SIMessage(this, "Attention", SIMessage.TypeMessage.WARNING_MESSAGE,"You need to change ...");
callMethode2();
问题在于,它在显示任何对话框之前执行对Method2的调用,而它应该在继续之前强制用户响应。
我看到一个空窗口与callMethod2!生成的窗口并排!,那怎么了

您应该为对话框设置模态

模式窗口是从属于应用程序主窗口的图形控制元素。它创建一个模式,该模式禁用主窗口,但将其作为子窗口显示在模态窗口前面。用户必须先与模式窗口交互,然后才能返回到父应用程序

因此,在初始化对话框时设置对话框的模式标志

setModal(True)
编辑: 我不知道您在代码中到底更改了什么,但下面的代码对我来说很好:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Emad
 */
import java.awt.BorderLayout;
import java.util.Enumeration;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;

import java.awt.BorderLayout;
import java.awt.Font;

import javax.swing.JTextArea;

import java.awt.SystemColor;

import javax.swing.JPanel;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import javafx.event.ActionEvent;

import javax.swing.border.LineBorder;

public class SIMessage extends JDialog implements ActionListener {

    private static final long serialVersionUID = 1L;
    public JButton oui = new JButton("Oui"), btnClose = new JButton(new ImageIcon("images\\logo\\delete.gif")),
            non = new JButton("Non"), annuler = new JButton("Annuler"), ok = new JButton("OK");
    public JLabel lblImgErr = new JLabel(new ImageIcon("images\\logo\\msgErreur.png")),
            lblImgConf = new JLabel(new ImageIcon("images\\logo\\msgQuestion.png")),
            lblImgWarning = new JLabel(new ImageIcon("images\\logo\\msgWarning.png")),
            lblImgInfo = new JLabel(new ImageIcon("images\\logo\\msgInformation.png")),
            lblImgQuestion = new JLabel(new ImageIcon("images\\logo\\msgQuestion.png")),
            lblImgIconApp = new JLabel(new ImageIcon("images\\logo\\clntIco.ico"));
    public JLabel title = new JLabel(), message = new JLabel();

    @Override
    public void actionPerformed(java.awt.event.ActionEvent e) {

        // TODO Auto-generated method stub
        Object source = e.getSource();
        if (source == oui || source == ok) {
            this.dispose();
        }
    }

    public enum TypeMessage {
        ERROR_MESSAGE,
        CONFIRMATION_MESSAGE,
        WARNING_MESSAGE,
        INFORMATION_MESSAGE,
        VALIDATION_MESSAGE
    }

    public SIMessage(JFrame parent, String title, TypeMessage type, String message) {
        super(parent, true);
        setUndecorated(true);
        getContentPane().setLayout(new GridLayout(1, 1));

        JPanel mainDgPanel = new JPanel();
        mainDgPanel.setBorder(new LineBorder(new Color(255, 255, 255), 3, true));
        mainDgPanel.setBounds(0, 0, 444, 156);
        getContentPane().add(mainDgPanel);
//                mainDgPanel.setBackground(Color.decode(EcranPrincipal.blueThemeCP));

        JTextArea txtrTextarea = new JTextArea(message);
        txtrTextarea.setRows(2);
        txtrTextarea.setBounds(123, 62, 340, 80);
        txtrTextarea.setFont(new Font("Iskoola Pota", Font.PLAIN, 18));
        txtrTextarea.setEditable(false);
        txtrTextarea.setFocusable(false);
        txtrTextarea.setOpaque(false);
        txtrTextarea.setBorder(null);
        txtrTextarea.setWrapStyleWord(true);
        txtrTextarea.setLineWrap(true);
//                txtrTextarea.setForeground(Color.decode(EcranPrincipal.blueThemeBT));
        mainDgPanel.add(txtrTextarea);

        JPanel panelButtons = new JPanel();
        panelButtons.setBounds(47, 115, 344, 30);
        mainDgPanel.add(panelButtons);

        switch (type) {
            case ERROR_MESSAGE: {
                JLabel lblNewLabel = lblImgErr;
                lblNewLabel.setBounds(10, 69, 79, 14);
                mainDgPanel.add(lblNewLabel);

                JButton btnOk = ok;
                panelButtons.add(btnOk);
                break;
            }
            case CONFIRMATION_MESSAGE: {
                JLabel lblNewLabel = lblImgConf;
                lblNewLabel.setBounds(10, 69, 79, 14);
                mainDgPanel.add(lblNewLabel);

                JButton btnOui = oui;
                panelButtons.add(btnOui);
                break;
            }
            case WARNING_MESSAGE: {
                JLabel lblNewLabel = lblImgWarning;
                lblNewLabel.setBounds(10, 69, 79, 14);
                mainDgPanel.add(lblNewLabel);

                JButton btnOk = ok;
                panelButtons.add(btnOk);
                break;
            }
            case INFORMATION_MESSAGE: {
                JLabel lblNewLabel = lblImgInfo;
                lblNewLabel.setBounds(10, 69, 79, 14);
                mainDgPanel.add(lblNewLabel);

                JButton btnOk = ok;
                panelButtons.add(btnOk);
                break;
            }
            case VALIDATION_MESSAGE: {
                JLabel lblNewLabel = lblImgConf;
                lblNewLabel.setBounds(10, 69, 79, 14);
                mainDgPanel.add(lblNewLabel);

                JButton btnOui = oui;
                panelButtons.add(btnOui);

                JButton btnNon = non;
                panelButtons.add(btnNon);

                JButton btnAnnuler = annuler;
                panelButtons.add(btnAnnuler);

                break;
            }
            default:
        }
        ok.addActionListener(this);
        oui.addActionListener(this);

        JPanel panel = new JPanel();
        panel.setBounds(0, 0, 444, 27);
        mainDgPanel.add(panel);
        panel.setBackground(Color.WHITE);
        panel.setLayout(null);

        JButton btnCloseDf = btnClose;
        btnCloseDf.setBounds(411, 0, 39, 23);
        panel.add(btnCloseDf);

        JLabel lblIconApp = lblImgIconApp;
        lblIconApp.setBounds(10, 4, 77, 14);
        panel.add(lblIconApp);

        JLabel lblTitle = new JLabel(title);
        lblTitle.setBounds(190, 4, 46, 14);
        panel.add(lblTitle);
        this.pack();
        this.setVisible(true);
    }

    public static void main(String[] args)
    {
        SIMessage sm=new SIMessage(null, "Attention", SIMessage.TypeMessage.WARNING_MESSAGE,"You need to change ...");
        System.out.println("hello");
    }
}

您应该为对话框设置模态

模式窗口是从属于应用程序主窗口的图形控制元素。它创建一个模式,该模式禁用主窗口,但将其作为子窗口显示在模态窗口前面。用户必须先与模式窗口交互,然后才能返回到父应用程序

因此,在初始化对话框时设置对话框的模式标志

setModal(True)
编辑: 我不知道您在代码中到底更改了什么,但下面的代码对我来说很好:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Emad
 */
import java.awt.BorderLayout;
import java.util.Enumeration;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;

import java.awt.BorderLayout;
import java.awt.Font;

import javax.swing.JTextArea;

import java.awt.SystemColor;

import javax.swing.JPanel;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import javafx.event.ActionEvent;

import javax.swing.border.LineBorder;

public class SIMessage extends JDialog implements ActionListener {

    private static final long serialVersionUID = 1L;
    public JButton oui = new JButton("Oui"), btnClose = new JButton(new ImageIcon("images\\logo\\delete.gif")),
            non = new JButton("Non"), annuler = new JButton("Annuler"), ok = new JButton("OK");
    public JLabel lblImgErr = new JLabel(new ImageIcon("images\\logo\\msgErreur.png")),
            lblImgConf = new JLabel(new ImageIcon("images\\logo\\msgQuestion.png")),
            lblImgWarning = new JLabel(new ImageIcon("images\\logo\\msgWarning.png")),
            lblImgInfo = new JLabel(new ImageIcon("images\\logo\\msgInformation.png")),
            lblImgQuestion = new JLabel(new ImageIcon("images\\logo\\msgQuestion.png")),
            lblImgIconApp = new JLabel(new ImageIcon("images\\logo\\clntIco.ico"));
    public JLabel title = new JLabel(), message = new JLabel();

    @Override
    public void actionPerformed(java.awt.event.ActionEvent e) {

        // TODO Auto-generated method stub
        Object source = e.getSource();
        if (source == oui || source == ok) {
            this.dispose();
        }
    }

    public enum TypeMessage {
        ERROR_MESSAGE,
        CONFIRMATION_MESSAGE,
        WARNING_MESSAGE,
        INFORMATION_MESSAGE,
        VALIDATION_MESSAGE
    }

    public SIMessage(JFrame parent, String title, TypeMessage type, String message) {
        super(parent, true);
        setUndecorated(true);
        getContentPane().setLayout(new GridLayout(1, 1));

        JPanel mainDgPanel = new JPanel();
        mainDgPanel.setBorder(new LineBorder(new Color(255, 255, 255), 3, true));
        mainDgPanel.setBounds(0, 0, 444, 156);
        getContentPane().add(mainDgPanel);
//                mainDgPanel.setBackground(Color.decode(EcranPrincipal.blueThemeCP));

        JTextArea txtrTextarea = new JTextArea(message);
        txtrTextarea.setRows(2);
        txtrTextarea.setBounds(123, 62, 340, 80);
        txtrTextarea.setFont(new Font("Iskoola Pota", Font.PLAIN, 18));
        txtrTextarea.setEditable(false);
        txtrTextarea.setFocusable(false);
        txtrTextarea.setOpaque(false);
        txtrTextarea.setBorder(null);
        txtrTextarea.setWrapStyleWord(true);
        txtrTextarea.setLineWrap(true);
//                txtrTextarea.setForeground(Color.decode(EcranPrincipal.blueThemeBT));
        mainDgPanel.add(txtrTextarea);

        JPanel panelButtons = new JPanel();
        panelButtons.setBounds(47, 115, 344, 30);
        mainDgPanel.add(panelButtons);

        switch (type) {
            case ERROR_MESSAGE: {
                JLabel lblNewLabel = lblImgErr;
                lblNewLabel.setBounds(10, 69, 79, 14);
                mainDgPanel.add(lblNewLabel);

                JButton btnOk = ok;
                panelButtons.add(btnOk);
                break;
            }
            case CONFIRMATION_MESSAGE: {
                JLabel lblNewLabel = lblImgConf;
                lblNewLabel.setBounds(10, 69, 79, 14);
                mainDgPanel.add(lblNewLabel);

                JButton btnOui = oui;
                panelButtons.add(btnOui);
                break;
            }
            case WARNING_MESSAGE: {
                JLabel lblNewLabel = lblImgWarning;
                lblNewLabel.setBounds(10, 69, 79, 14);
                mainDgPanel.add(lblNewLabel);

                JButton btnOk = ok;
                panelButtons.add(btnOk);
                break;
            }
            case INFORMATION_MESSAGE: {
                JLabel lblNewLabel = lblImgInfo;
                lblNewLabel.setBounds(10, 69, 79, 14);
                mainDgPanel.add(lblNewLabel);

                JButton btnOk = ok;
                panelButtons.add(btnOk);
                break;
            }
            case VALIDATION_MESSAGE: {
                JLabel lblNewLabel = lblImgConf;
                lblNewLabel.setBounds(10, 69, 79, 14);
                mainDgPanel.add(lblNewLabel);

                JButton btnOui = oui;
                panelButtons.add(btnOui);

                JButton btnNon = non;
                panelButtons.add(btnNon);

                JButton btnAnnuler = annuler;
                panelButtons.add(btnAnnuler);

                break;
            }
            default:
        }
        ok.addActionListener(this);
        oui.addActionListener(this);

        JPanel panel = new JPanel();
        panel.setBounds(0, 0, 444, 27);
        mainDgPanel.add(panel);
        panel.setBackground(Color.WHITE);
        panel.setLayout(null);

        JButton btnCloseDf = btnClose;
        btnCloseDf.setBounds(411, 0, 39, 23);
        panel.add(btnCloseDf);

        JLabel lblIconApp = lblImgIconApp;
        lblIconApp.setBounds(10, 4, 77, 14);
        panel.add(lblIconApp);

        JLabel lblTitle = new JLabel(title);
        lblTitle.setBounds(190, 4, 46, 14);
        panel.add(lblTitle);
        this.pack();
        this.setVisible(true);
    }

    public static void main(String[] args)
    {
        SIMessage sm=new SIMessage(null, "Attention", SIMessage.TypeMessage.WARNING_MESSAGE,"You need to change ...");
        System.out.println("hello");
    }
}

setLayout(null)
永远不会有好结果。@trashgod我刚刚将其更改为
getContentPane().setLayout(新的GridLayout(1,1))
但没有更改!,谢谢
setLayout(null)
永远不会有好结果。@trashgood我刚刚将其更改为
getContentPane().setLayout(新的GridLayout(1,1))
但没有更改!,谢谢hanks@Emad,我是用
super(parent,true)实现的但现在对话框作为骨架存在,无法真正访问,但主窗口已锁定!删除行
mainDgPanel.setLayout(null)。对话框应该包含一个布局。默认布局是flowLayout。我用new BorderLayout()替换了所有空值,但它仍然被阻止。您应该向“确定”按钮添加一个操作侦听器,并在用户单击“确定”时处理该对话框。@SEVOIDEM我已经添加了要回答的代码,请检查!谢谢@Emad,我用
super(parent,true)做到了这一点但现在对话框作为骨架存在,无法真正访问,但主窗口已锁定!删除行
mainDgPanel.setLayout(null)。对话框应该包含一个布局。默认布局是flowLayout。我用new BorderLayout()替换了所有空值,但它仍然被阻止。您应该向“确定”按钮添加一个操作侦听器,并在用户单击“确定”时处理该对话框。@SEVOIDEM我已经添加了要回答的代码,请检查!