JDialog显示为空

时间:2017-04-28 10:33:06

标签: java swing

我试图用新的自定义对话框替换JOptionPane的使用,这就是我所做的:

    SIMessage sm=new SIMessage(this, "Attention", SIMessage.TypeMessage.WARNING_MESSAGE,"You need to change ...");
callMethode2();

然后我打电话给:

$array = array(
          '1'=>"Cat",
          '2' => 'Horse',
          '3' => 'Zebra',
          '4' => 'Bear',
          '5' => 'Dog',
      );

问题是它在显示任何对话之前执行对Methode2的调用,而它应该在继续之前强制用户做出响应。 我看到一个空窗口与callMethod2生成的窗口并排!所以有什么问题?

1 个答案:

答案 0 :(得分:2)

您应该为Dialog设置模态

模态窗口是从属于应用程序主窗口的图形控件元素。它创建一个模式,禁用主窗口,但使用模态窗口作为前面的子窗口使其保持可见。用户必须先与模态窗口进行交互才能返回父应用程序。

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

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");
    }
}