用模态的方式打开自定义JDialog,并获取返回值

JFrame的变量名为jf

JDialog的类名为Fff

[java]  view plain  copy
  1. btnLoadImage.addMouseListener(new MouseAdapter() {  
  2.             @Override  
  3.             public void mouseClicked(MouseEvent e) {  
  4.                 Fff myff = new Fff();  
  5.                 //myff.setModal(false);  
  6.                 myff.setModalityType(ModalityType.APPLICATION_MODAL);  
  7.                 myff.setVisible(true);  
  8.                   
  9.                 //如果是点击确定,则文本框中输入的内容显示出来  
  10.                 String strText;  
  11.                 int iReturn;  
  12.                   
  13.                 iReturn = myff.f_GetCoseType();  
  14.                 if (iReturn == 0) {  
  15.                     lbImage.setText("选择了取消");  
  16.                 }  
  17.                 else  
  18.                 {  
  19.                     strText = myff.f_GetText();  
  20.                     lbImage.setText(strText);  
  21.                 }  
  22.                   
  23.             }                 
  24.         });  



Fff的类


[java]  view plain  copy
  1. package bb;  
  2.   
  3. import java.awt.BorderLayout;  
  4. import java.awt.FlowLayout;  
  5.   
  6. import javax.swing.JButton;  
  7. import javax.swing.JDialog;  
  8. import javax.swing.JOptionPane;  
  9. import javax.swing.JPanel;  
  10. import javax.swing.border.EmptyBorder;  
  11. import javax.swing.JTextField;  
  12. import java.awt.event.MouseAdapter;  
  13. import java.awt.event.MouseEvent;  
  14.   
  15. /**  
  16. * @author 作者 E-mail:  
  17. * @version 创建时间:Sep 18, 2017 2:48:29 PM  
  18. * 类说明  
  19. */  
  20.   
  21. public class Fff extends JDialog {  
  22.   
  23.     private final JPanel contentPanel = new JPanel();  
  24.     private JTextField textOut;  
  25.     private int iCloseType; //点击确定,该值为1,点击取消,该值为0  
  26.       
  27.     /** 
  28.      * Launch the application. 
  29.      */  
  30.     public static void main(String[] args) {  
  31.         try {  
  32.             Fff dialog = new Fff();  
  33.             dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);  
  34.             dialog.setVisible(true);  
  35.         } catch (Exception e) {  
  36.             e.printStackTrace();  
  37.         }  
  38.     }  
  39.   
  40.     /** 
  41.      * Create the dialog. 
  42.      */  
  43.     public Fff() {  
  44.         iCloseType = 0 ;    //默认是点击取消按钮  
  45.           
  46.         setBounds(100100450300);  
  47.         getContentPane().setLayout(new BorderLayout());  
  48.         contentPanel.setBorder(new EmptyBorder(5555));  
  49.         getContentPane().add(contentPanel, BorderLayout.CENTER);  
  50.         contentPanel.setLayout(new BorderLayout(00));  
  51.         {  
  52.             JPanel panel = new JPanel();  
  53.             contentPanel.add(panel);  
  54.             panel.setLayout(new BorderLayout(00));  
  55.             {  
  56.                 textOut = new JTextField();  
  57.                 panel.add(textOut, BorderLayout.NORTH);  
  58.                 textOut.setColumns(10);  
  59.             }  
  60.         }  
  61.         {  
  62.             JPanel buttonPane = new JPanel();  
  63.             buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));  
  64.             getContentPane().add(buttonPane, BorderLayout.SOUTH);  
  65.             {  
  66.                 JButton okButton = new JButton("OK");  
  67.                 okButton.addMouseListener(new MouseAdapter() {  
  68.                     @Override  
  69.                     public void mouseClicked(MouseEvent e) {  
  70.                         //检查用户是否输入了内容  
  71.                         if(f_chkInput()) {  
  72.                             iCloseType = 1//点击了确定性按钮  
  73.                             dispose();  
  74.                         }  
  75.                     }  
  76.                 });  
  77.                   
  78.                 okButton.setActionCommand("OK");  
  79.                 buttonPane.add(okButton);  
  80.                 getRootPane().setDefaultButton(okButton);  
  81.             }  
  82.             {  
  83.                 JButton cancelButton = new JButton("Cancel");  
  84.                 cancelButton.addMouseListener(new MouseAdapter() {  
  85.                     @Override  
  86.                     public void mouseClicked(MouseEvent e) {  
  87.                         iCloseType = 0//点击了取消按钮  
  88.                         dispose();  
  89.                     }  
  90.                 });  
  91.                 cancelButton.setActionCommand("Cancel");  
  92.                 buttonPane.add(cancelButton);  
  93.             }  
  94.         }  
  95.     }  
  96.   
  97.     //检查用户是否输入了文本  
  98.     public boolean f_chkInput() {  
  99.          String strText;  
  100.          boolean bRet;  
  101.         strText = textOut.getText();  
  102.         if ( strText.length()==0 || strText==null) {  
  103.             JOptionPane jMsg = new JOptionPane();  
  104.             jMsg.showMessageDialog(null"请输入内容");  
  105.             bRet = false;  
  106.         }  
  107.         else  
  108.         {  
  109.             bRet = true;  
  110.         }  
  111.           
  112.         return bRet;  
  113.     }  
  114.       
  115.     public int f_GetCoseType() {  
  116.         return iCloseType;    
  117.     }  
  118.     public String f_GetText() {  
  119.         String strOut;  
  120.         strOut = textOut.getText();  
  121.         return strOut;  
  122.     }  
  123. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值