如何自定义一个JDialog,它能够返回一个值。就像JOptionPane中的静态方法,showMessageDialog();

如何自定义一个JDialog,它能够返回一个值。就像JOptionPane中的静态方法,showMessageDialog();

如果一定要是JDialog的话

可以添加一个方法,比如

ret __showDialog(){
setModal(true);
setVisible(true);//阻塞的
return someValue;
}

import java.awt.Component;
import java.awt.Font;

import javax.swing.JDialog;
import javax.swing.JPanel;

public class FontChooser extends JPanel {
private Font retFont = null;

public FontChooser() {
    // ...
}

public Font getSelectedFont() {
    return retFont;
}

public static Font showDialog(Component par, Font initFont, String title) {
    FontChooser fc = new FontChooser();
    // fc初始化
    JDialog dialog = new JDialog();
    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    dialog.setTitle(title);
    // ... dialog 设置
    dialog.setContentPane(fc);
    dialog.setModal(true);
    dialog.setVisible(true);
    return fc.getSelectedFont();
}

}